#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*/ printf(char *, char *);
scalar_t__ sprintf(char *, char *);
void print_binary_rep(double n) {
  static size_t max_bits = 32;
  char buff[max_bits + 3];
  size_t cursor = 0;
  cursor += sprintf(buff, "0.");
  while (n != 0 && cursor < 34) {
    double new_n = n * 2;
    int new_n_as_int = new_n;
    buff[cursor++] = (new_n_as_int == 1 ? '1' : '0');
    n = new_n - new_n_as_int;
  }
  if (n != 0) {
    sprintf(buff, "ERROR");
  } else {
    buff[cursor] = '\0';
  }
  printf("%s\n", buff);
}


x86 assembly (clang, -O0):
<print_binary_rep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    0x2efc(%rip),%rcx
mov    %rcx,%rax
add    $0x3,%rax
mov    %rsp,%rdx
mov    %rdx,-0x10(%rbp)
add    $0x12,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe9e(%rip),%rsi
call   1040 <sprintf@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
movsd  -0x8(%rbp),%xmm0
xor    %eax,%eax
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x39(%rbp)
jne    1199 <print_binary_rep+0x79>
jp     1199 <print_binary_rep+0x79>
jmp    11a4 <print_binary_rep+0x84>
cmpq   $0x22,-0x20(%rbp)
setb   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    11b4 <print_binary_rep+0x94>
jmp    1211 <print_binary_rep+0xf1>
mov    -0x38(%rbp),%rax
movsd  0xe40(%rip),%xmm0
mulsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
cvttsd2si -0x28(%rbp),%ecx
mov    %ecx,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
mov    $0x30,%ecx
mov    $0x31,%edx
cmp    $0x1,%esi
cmove  %edx,%ecx
mov    %cl,%dl
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
mov    %dl,(%rax,%rcx,1)
movsd  -0x28(%rbp),%xmm0
cvtsi2sdl -0x2c(%rbp),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1177 <print_binary_rep+0x57>
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    122e <print_binary_rep+0x10e>
jp     122e <print_binary_rep+0x10e>
jmp    1243 <print_binary_rep+0x123>
mov    -0x38(%rbp),%rdi
lea    0xdd2(%rip),%rsi
call   1040 <sprintf@plt>
jmp    124f <print_binary_rep+0x12f>
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rsi
lea    0xdb7(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_binary_rep>:
sub    $0x48,%rsp
movapd %xmm0,0x30(%rsp)
lea    0xecf(%rip),%rsi
mov    %rsp,%rdi
call   1040 <sprintf@plt>
movapd 0x30(%rsp),%xmm2
xorpd  %xmm0,%xmm0
movapd %xmm2,%xmm1
cmpneqsd %xmm0,%xmm1
movq   %xmm1,%rcx
and    $0x1,%ecx
ucomisd %xmm0,%xmm2
jne    115c <print_binary_rep+0x3c>
jnp    11b8 <print_binary_rep+0x98>
cmp    $0x21,%rax
ja     11d0 <print_binary_rep+0xb0>
xorpd  %xmm0,%xmm0
cs nopw 0x0(%rax,%rax,1)
addsd  %xmm2,%xmm2
cvttsd2si %xmm2,%ecx
cmp    $0x1,%ecx
sete   %cl
or     $0x30,%cl
lea    0x1(%rax),%rdx
mov    %cl,(%rsp,%rax,1)
cvttpd2dq %xmm2,%xmm1
cvtdq2pd %xmm1,%xmm1
subsd  %xmm1,%xmm2
movapd %xmm2,%xmm1
cmpneqsd %xmm0,%xmm1
movq   %xmm1,%rcx
and    $0x1,%ecx
ucomisd %xmm0,%xmm2
jne    11ad <print_binary_rep+0x8d>
jnp    11bb <print_binary_rep+0x9b>
cmp    $0x21,%rax
mov    %rdx,%rax
jb     1170 <print_binary_rep+0x50>
jmp    11bb <print_binary_rep+0x9b>
mov    %rax,%rdx
test   %cl,%cl
je     11d7 <print_binary_rep+0xb7>
lea    0xe3d(%rip),%rsi
mov    %rsp,%rdi
call   1040 <sprintf@plt>
jmp    11db <print_binary_rep+0xbb>
mov    %rax,%rdx
test   %cl,%cl
jne    11bf <print_binary_rep+0x9f>
movb   $0x0,(%rsp,%rdx,1)
lea    0xe27(%rip),%rdi
mov    %rsp,%rsi
call   1030 <printf@plt>
add    $0x48,%rsp
ret

LLVM IR (-O0):
@print_binary_rep.max_bits = internal global i64 32
@.str = [3 x i8] c"0.\00"
@.str.1 = [6 x i8] c"ERROR\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define void @print_binary_rep(double %0) {
%2 = alloca double
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca double
%7 = alloca i32
store double %0, double* %2
%8 = load i64, i64* @print_binary_rep.max_bits
%9 = add i64 %8, 3
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %3
%11 = alloca i8, i64 %9
store i64 %9, i64* %4
store i64 0, i64* %5
%12 = call i64 @sprintf(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%13 = load i64, i64* %5
%14 = add i64 %13, %12
store i64 %14, i64* %5
br label %15
15: ; preds = %23, %1
%16 = load double, double* %2
%17 = fcmp une double %16, 0.000000e+00
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i64, i64* %5
%20 = icmp ult i64 %19, 34
br label %21
21: ; preds = %18, %15
%22 = phi i1 [ false, %15 ], [ %20, %18 ]
br i1 %22, label %23, label %40
23: ; preds = %21
%24 = load double, double* %2
%25 = fmul double %24, 2.000000e+00
store double %25, double* %6
%26 = load double, double* %6
%27 = fptosi double %26 to i32
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, 1
%30 = zext i1 %29 to i64
%31 = select i1 %29, i32 49, i32 48
%32 = trunc i32 %31 to i8
%33 = load i64, i64* %5
%34 = add i64 %33, 1
store i64 %34, i64* %5
%35 = getelementptr inbounds i8, i8* %11, i64 %33
store i8 %32, i8* %35
%36 = load double, double* %6
%37 = load i32, i32* %7
%38 = sitofp i32 %37 to double
%39 = fsub double %36, %38
store double %39, double* %2
br label %15
40: ; preds = %21
%41 = load double, double* %2
%42 = fcmp une double %41, 0.000000e+00
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i64 @sprintf(i8* %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %48
45: ; preds = %40
%46 = load i64, i64* %5
%47 = getelementptr inbounds i8, i8* %11, i64 %46
store i8 0, i8* %47
br label %48
48: ; preds = %45, %43
%49 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%50 = load i8*, i8** %3
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"0.\00"
@.str.1 = [6 x i8] c"ERROR\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define void @print_binary_rep(double %0) {
%2 = alloca [35 x i8]
%3 = getelementptr inbounds [35 x i8], [35 x i8]* %2, i64 0, i64 0
%4 = call i64 @sprintf(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = fcmp une double %0, 0.000000e+00
%6 = icmp ult i64 %4, 34
%7 = select i1 %5, i1 %6, i1 false
br i1 %7, label %8, label %22
8: ; preds = %1, %8
%9 = phi double [ %18, %8 ], [ %0, %1 ]
%10 = phi i64 [ %15, %8 ], [ %4, %1 ]
%11 = fmul double %9, 2.000000e+00
%12 = fptosi double %11 to i32
%13 = icmp eq i32 %12, 1
%14 = select i1 %13, i8 49, i8 48
%15 = add nuw nsw i64 %10, 1
%16 = getelementptr inbounds [35 x i8], [35 x i8]* %2, i64 0, i64 %10
store i8 %14, i8* %16
%17 = sitofp i32 %12 to double
%18 = fsub double %11, %17
%19 = fcmp une double %18, 0.000000e+00
%20 = icmp ult i64 %10, 33
%21 = select i1 %19, i1 %20, i1 false
br i1 %21, label %8, label %22
22: ; preds = %8, %1
%23 = phi i64 [ %4, %1 ], [ %15, %8 ]
%24 = phi i1 [ %5, %1 ], [ %19, %8 ]
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i64 @sprintf(i8* %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %29
27: ; preds = %22
%28 = getelementptr inbounds [35 x i8], [35 x i8]* %2, i64 0, i64 %23
store i8 0, i8* %28
br label %29
29: ; preds = %27, %25
%30 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %3)
ret void
}