1/* { dg-do compile } */
2/* { dg-options "-O2 -fdump-ipa-icf"  } */
3
4#include <stdlib.h>
5#include <stdio.h>
6
7int gcd(int x, int y) __attribute__ ((pure));
8
9__attribute__ ((noinline))
10int gcd(int x, int y)
11{
12  int swap;
13
14  if(x <= 0 || y <= 0)
15    return 0;
16
17  if(x < y)
18    {
19      swap = x;
20      x = y;
21      y = swap;
22    }
23
24  while(x != y)
25    {
26      x = x - y;
27
28      if(y > x)
29	{
30	  swap = x;
31	  x = y;
32	  y = swap;
33	}
34    }
35
36  return x;
37}
38
39int nsd(int x, int y) __attribute__ ((pure));
40
41__attribute__ ((noinline))
42int nsd(int x, int y)
43{
44  int swap;
45
46  if(x <= 0 || y <= 0)
47    return 0;
48
49  if(x < y)
50    {
51      swap = x;
52      x = y;
53      y = swap;
54    }
55
56  while(x != y)
57    {
58      x = x - y;
59
60      if(y > x)
61	{
62	  swap = x;
63	  x = y;
64	  y = swap;
65	}
66    }
67
68  return x;
69}
70
71int nsd_different_result(int x, int y) __attribute__ ((pure));
72
73__attribute__ ((noinline))
74int nsd_different_result(int x, int y)
75{
76  int pes;
77
78  if(x <= 0 || y <= 0)
79    return 1;
80
81  if(x < 10)
82    y = 12;
83  else if(x == 44)
84    y = 124;
85  else
86    y = 1111;
87
88  if(x < y)
89    {
90      pes = x;
91      x = y;
92      y = pes;
93    }
94
95  while(x != y)
96    {
97      x = x - y;
98
99      if(y > x)
100	{
101	  pes = x;
102	  x = y;
103	  y = pes;
104	}
105    }
106
107  return x;
108}
109
110int nsd_different_result2(int x, int y) __attribute__ ((pure));
111
112__attribute__ ((noinline))
113int nsd_different_result2(int x, int y)
114{
115  int pes;
116
117  if(x <= 0 || y <= 0)
118    return 1;
119
120  if(x < 10)
121    y = 12;
122  else if(x == 44)
123    y = 124;
124  else
125    y = 1111;
126
127  if(x < y)
128    {
129      pes = x;
130      x = y;
131      y = pes;
132    }
133
134  while(x != y)
135    {
136      x = x - y;
137
138      if(y > x)
139	{
140	  pes = x;
141	  x = y;
142	  y = pes;
143	}
144    }
145
146  return x;
147}
148
149__attribute__ ((noinline))
150int s1(int x)
151{
152  switch (x)
153    {
154    case 10:
155    case 11:
156      return 2;
157    case 12:
158      return 123;
159    default:
160      return x + 2;
161    }
162}
163
164__attribute__ ((noinline))
165int s2(int x)
166{
167  switch (x)
168    {
169    case 10:
170    case 11:
171      return 2;
172    case 12:
173      return 123;
174    default:
175      return x + 2;
176    }
177}
178int main(int argc, char **argv)
179{
180  if(argc < 3)
181    return 1;
182
183  int a = atoi(argv[1]);
184  int b = atoi(argv[2]);
185
186  printf("Test1: %d, %d, gdc: %d\n", a, b, gcd(a, b));
187  printf("Test2: %d, %d, gdc: %d\n", a, b, nsd(a, b));
188}
189
190/* { dg-final { scan-ipa-dump "Semantic equality hit:s2->s1" "icf"  } } */
191/* { dg-final { scan-ipa-dump "Semantic equality hit:nsd_different_result2->nsd_different_result" "icf"  } } */
192/* { dg-final { scan-ipa-dump "Semantic equality hit:nsd->gcd" "icf"  } } */
193/* { dg-final { scan-ipa-dump "Equal symbols: 3" "icf"  } } */
194/* { dg-final { cleanup-ipa-dump "icf" } } */
195