#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ flteq(double, double);
double fmod(double, double);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
void do_fraction(double val) {
  int n, m, k = 0;
  double d, test;
  for (n = 2; n < 17; n++) {
    d = 1.0 / (double)n;
    if ((test = fmod(val, d)) < 0.000002 || d - test < 0.000002) {
      m = (int)(val / d);
      if (!flteq((double)m * d, val))
        m++;
      if (!flteq((double)m * d, val)) {
        m -= 2;
        if (!flteq((double)m * d, val)) {
          fprintf(stderr, "Problem in fractions\n");
          return;
        }
      }
      if (m > n) {
        k = m / n;
        m -= k * n;
      }
      printf("(");
      if (k)
        printf("%d & ", k);
      printf("%d/%d) ", m, n);
      break;
    }
  }
}


x86 assembly (clang, -O0):
<do_fraction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x2,-0xc(%rbp)
cmpl   $0x11,-0xc(%rbp)
jge    12d3 <do_fraction+0x193>
cvtsi2sdl -0xc(%rbp),%xmm1
movsd  0xe96(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
call   1040 <fmod@plt>
movaps %xmm0,%xmm1
movsd  %xmm1,-0x28(%rbp)
movsd  0xe66(%rip),%xmm0
ucomisd %xmm1,%xmm0
ja     11c0 <do_fraction+0x80>
movsd  -0x20(%rbp),%xmm1
subsd  -0x28(%rbp),%xmm1
movsd  0xe4a(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    12c0 <do_fraction+0x180>
movsd  -0x8(%rbp),%xmm0
divsd  -0x20(%rbp),%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x10(%rbp)
cvtsi2sdl -0x10(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm1
call   1060 <flteq@plt>
cmp    $0x0,%eax
jne    11f7 <do_fraction+0xb7>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
cvtsi2sdl -0x10(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm1
call   1060 <flteq@plt>
cmp    $0x0,%eax
jne    1259 <do_fraction+0x119>
mov    -0x10(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x10(%rbp)
cvtsi2sdl -0x10(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm1
call   1060 <flteq@plt>
cmp    $0x0,%eax
jne    1254 <do_fraction+0x114>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
lea    0xdc6(%rip),%rsi
call   1050 <fprintf@plt>
jmp    12d3 <do_fraction+0x193>
jmp    1259 <do_fraction+0x119>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    127e <do_fraction+0x13e>
mov    -0x10(%rbp),%eax
cltd
idivl  -0xc(%rbp)
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
imul   -0xc(%rbp),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
je     12a7 <do_fraction+0x167>
mov    -0x14(%rbp),%esi
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12d3 <do_fraction+0x193>
jmp    12c5 <do_fraction+0x185>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    115b <do_fraction+0x1b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_fraction>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0x2,%ebx
movsd  %xmm0,(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xorps  %xmm2,%xmm2
cvtsi2sd %ebx,%xmm2
movsd  0xe81(%rip),%xmm1
divsd  %xmm2,%xmm1
movsd  %xmm1,0x8(%rsp)
call   1050 <fmod@plt>
movsd  0xe72(%rip),%xmm2
ucomisd %xmm0,%xmm2
ja     11c2 <do_fraction+0x72>
movsd  0x8(%rsp),%xmm1
subsd  %xmm0,%xmm1
ucomisd %xmm1,%xmm2
ja     11c2 <do_fraction+0x72>
add    $0x1,%ebx
cmp    $0x11,%ebx
movsd  (%rsp),%xmm0
jne    1170 <do_fraction+0x20>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
movsd  (%rsp),%xmm1
movapd %xmm1,%xmm0
movsd  0x8(%rsp),%xmm2
divsd  %xmm2,%xmm0
cvttsd2si %xmm0,%ebp
cvttpd2dq %xmm0,%xmm0
cvtdq2pd %xmm0,%xmm0
mulsd  %xmm2,%xmm0
call   1070 <flteq@plt>
cmp    $0x1,%eax
adc    $0x0,%ebp
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
mulsd  0x8(%rsp),%xmm0
movsd  (%rsp),%xmm1
call   1070 <flteq@plt>
test   %eax,%eax
jne    122d <do_fraction+0xdd>
add    $0xfffffffe,%ebp
xorps  %xmm1,%xmm1
cvtsi2sd %ebp,%xmm1
movsd  0x8(%rsp),%xmm0
mulsd  %xmm1,%xmm0
movsd  (%rsp),%xmm1
call   1070 <flteq@plt>
test   %eax,%eax
je     1281 <do_fraction+0x131>
cmp    %ebx,%ebp
jle    125d <do_fraction+0x10d>
mov    %ebp,%eax
cltd
idiv   %ebx
mov    %eax,%r14d
mov    %edx,%ebp
mov    $0x28,%edi
call   1030 <putchar@plt>
test   %r14d,%r14d
je     1267 <do_fraction+0x117>
lea    0xdd5(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1267 <do_fraction+0x117>
mov    $0x28,%edi
call   1030 <putchar@plt>
lea    0xdbe(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <printf@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
lea    0xd7f(%rip),%rsi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <fprintf@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"Problem in fractions\0A\00"
@.str.1 = [2 x i8] c"(\00"
@.str.2 = [6 x i8] c"%d & \00"
@.str.3 = [8 x i8] c"%d/%d) \00"
define void @do_fraction(double %0) {
%2 = alloca double
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca double
%7 = alloca double
store double %0, double* %2
store i32 0, i32* %5
store i32 2, i32* %3
br label %8
8: ; preds = %86, %1
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 17
br i1 %10, label %11, label %89
11: ; preds = %8
%12 = load i32, i32* %3
%13 = sitofp i32 %12 to double
%14 = fdiv double 1.000000e+00, %13
store double %14, double* %6
%15 = load double, double* %2
%16 = load double, double* %6
%17 = call double @fmod(double %15, double %16)
store double %17, double* %7
%18 = fcmp olt double %17, 2.000000e-06
br i1 %18, label %24, label %19
19: ; preds = %11
%20 = load double, double* %6
%21 = load double, double* %7
%22 = fsub double %20, %21
%23 = fcmp olt double %22, 2.000000e-06
br i1 %23, label %24, label %85
24: ; preds = %19, %11
%25 = load double, double* %2
%26 = load double, double* %6
%27 = fdiv double %25, %26
%28 = fptosi double %27 to i32
store i32 %28, i32* %4
%29 = load i32, i32* %4
%30 = sitofp i32 %29 to double
%31 = load double, double* %6
%32 = fmul double %30, %31
%33 = load double, double* %2
%34 = call i32 @flteq(double %32, double %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %24
%37 = load i32, i32* %4
%38 = add nsw i32 %37, 1
store i32 %38, i32* %4
br label %39
39: ; preds = %36, %24
%40 = load i32, i32* %4
%41 = sitofp i32 %40 to double
%42 = load double, double* %6
%43 = fmul double %41, %42
%44 = load double, double* %2
%45 = call i32 @flteq(double %43, double %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %61, label %47
47: ; preds = %39
%48 = load i32, i32* %4
%49 = sub nsw i32 %48, 2
store i32 %49, i32* %4
%50 = load i32, i32* %4
%51 = sitofp i32 %50 to double
%52 = load double, double* %6
%53 = fmul double %51, %52
%54 = load double, double* %2
%55 = call i32 @flteq(double %53, double %54)
%56 = icmp ne i32 %55, 0
br i1 %56, label %60, label %57
57: ; preds = %47
%58 = load i32, i32* @stderr
%59 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %89
60: ; preds = %47
br label %61
61: ; preds = %60, %39
%62 = load i32, i32* %4
%63 = load i32, i32* %3
%64 = icmp sgt i32 %62, %63
br i1 %64, label %65, label %74
65: ; preds = %61
%66 = load i32, i32* %4
%67 = load i32, i32* %3
%68 = sdiv i32 %66, %67
store i32 %68, i32* %5
%69 = load i32, i32* %5
%70 = load i32, i32* %3
%71 = mul nsw i32 %69, %70
%72 = load i32, i32* %4
%73 = sub nsw i32 %72, %71
store i32 %73, i32* %4
br label %74
74: ; preds = %65, %61
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%76 = load i32, i32* %5
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %74
%79 = load i32, i32* %5
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %79)
br label %81
81: ; preds = %78, %74
%82 = load i32, i32* %4
%83 = load i32, i32* %3
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %82, i32 %83)
br label %89
85: ; preds = %19
br label %86
86: ; preds = %85
%87 = load i32, i32* %3
%88 = add nsw i32 %87, 1
store i32 %88, i32* %3
br label %8
89: ; preds = %57, %81, %8
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"Problem in fractions\0A\00"
@.str.2 = [6 x i8] c"%d & \00"
@.str.3 = [8 x i8] c"%d/%d) \00"
define void @do_fraction(double %0) {
br label %2
2: ; preds = %1, %49
%3 = phi i32 [ 2, %1 ], [ %50, %49 ]
%4 = sitofp i32 %3 to double
%5 = fdiv double 1.000000e+00, %4
%6 = tail call double @fmod(double %0, double %5)
%7 = fcmp olt double %6, 2.000000e-06
%8 = fsub double %5, %6
%9 = fcmp olt double %8, 2.000000e-06
%10 = or i1 %7, %9
br i1 %10, label %11, label %49
11: ; preds = %2
%12 = fdiv double %0, %5
%13 = fptosi double %12 to i32
%14 = sitofp i32 %13 to double
%15 = fmul double %5, %14
%16 = tail call i32 @flteq(double %15, double %0)
%17 = icmp eq i32 %16, 0
%18 = zext i1 %17 to i32
%19 = add nsw i32 %18, %13
%20 = sitofp i32 %19 to double
%21 = fmul double %5, %20
%22 = tail call i32 @flteq(double %21, double %0)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %11
%25 = add nsw i32 %19, -2
%26 = sitofp i32 %25 to double
%27 = fmul double %5, %26
%28 = tail call i32 @flteq(double %27, double %0)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %52
33: ; preds = %24, %11
%34 = phi i32 [ %19, %11 ], [ %25, %24 ]
%35 = icmp sgt i32 %34, %3
br i1 %35, label %38, label %36
36: ; preds = %33
%37 = tail call i32 @putchar(i32 40)
br label %46
38: ; preds = %33
%39 = sdiv i32 %34, %3
%40 = mul nsw i32 %39, %3
%41 = srem i32 %34, %3
%42 = tail call i32 @putchar(i32 40)
%43 = icmp eq i32 %39, 0
br i1 %43, label %46, label %44
44: ; preds = %38
%45 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %39)
br label %46
46: ; preds = %36, %44, %38
%47 = phi i32 [ %34, %36 ], [ %41, %44 ], [ %41, %38 ]
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %47, i32 %3)
br label %52
49: ; preds = %2
%50 = add nuw nsw i32 %3, 1
%51 = icmp eq i32 %50, 17
br i1 %51, label %52, label %2
52: ; preds = %49, %46, %30
ret void
}