#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 */
long double fabs(long double);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
long double modfl(long double, long double *);
int sprintf(char *, char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
void __inttobinfloatpart(long double value, char *buffer, int precision) {
  char *bin_str_saved = malloc(96);
  memset(bin_str_saved, '\0', 96);
  int c = 0;
  if (value < 0) {
    value = fabs(value);
  }
  while (c < precision) {
    long double res_int;
    long double res_float;
    res_float = modfl(value * 2.0, &res_int);
    if (res_int == 0) {
      strcat(bin_str_saved, "0");
    } else if (res_int == 1) {
      strcat(bin_str_saved, "1");
    } else {
      return;
    }
    if (res_float == 0.0) {
      break;
    }
    value = res_float;
    c++;
  }
  c = sprintf(buffer, "%s", bin_str_saved);
  buffer[c] = '\0';
  free(bin_str_saved);
}


x86 assembly (clang, -O0):
<__inttobinfloatpart>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
fldt   0x10(%rbp)
fstpt  -0x10(%rbp)
mov    %rdi,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
mov    $0x60,%edi
mov    %edi,-0x54(%rbp)
call   1070 <malloc@plt>
mov    -0x54(%rbp),%edx
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
call   1040 <memset@plt>
movl   $0x0,-0x2c(%rbp)
fldt   -0x10(%rbp)
fldz
fucomip %st(1),%st
fstp   %st(0)
jbe    11cf <__inttobinfloatpart+0x5f>
jmp    11bf <__inttobinfloatpart+0x4f>
fldt   -0x10(%rbp)
mov    %rsp,%rax
fstpt  (%rax)
call   1060 <fabs@plt>
fstpt  -0x10(%rbp)
jmp    11d4 <__inttobinfloatpart+0x64>
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1297 <__inttobinfloatpart+0x127>
fldt   -0x10(%rbp)
fadd   %st,%st(0)
mov    %rsp,%rax
fstpt  (%rax)
lea    -0x40(%rbp),%rdi
call   1050 <modfl@plt>
fstpt  -0x50(%rbp)
fldt   -0x40(%rbp)
fldz
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
jne    1227 <__inttobinfloatpart+0xb7>
jp     1227 <__inttobinfloatpart+0xb7>
jmp    1212 <__inttobinfloatpart+0xa2>
mov    -0x28(%rbp),%rdi
lea    0xde3(%rip),%rsi
call   1080 <strcat@plt>
jmp    1262 <__inttobinfloatpart+0xf2>
fldt   -0x40(%rbp)
fld1
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
jne    1258 <__inttobinfloatpart+0xe8>
jp     1258 <__inttobinfloatpart+0xe8>
jmp    1243 <__inttobinfloatpart+0xd3>
mov    -0x28(%rbp),%rdi
lea    0xdb4(%rip),%rsi
call   1080 <strcat@plt>
jmp    125d <__inttobinfloatpart+0xed>
jmp    12c3 <__inttobinfloatpart+0x153>
jmp    1262 <__inttobinfloatpart+0xf2>
fldt   -0x50(%rbp)
fldz
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
jne    1283 <__inttobinfloatpart+0x113>
jp     1283 <__inttobinfloatpart+0x113>
jmp    127e <__inttobinfloatpart+0x10e>
jmp    1297 <__inttobinfloatpart+0x127>
fldt   -0x50(%rbp)
fstpt  -0x10(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11d4 <__inttobinfloatpart+0x64>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xd5e(%rip),%rsi
call   1090 <sprintf@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__inttobinfloatpart>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
fldt   0x90(%rsp)
fstpt  0x1c(%rsp)
mov    $0x60,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x60,%edx
call   1050 <memset@plt>
fldt   0x1c(%rsp)
test   %ebp,%ebp
jle    123e <__inttobinfloatpart+0xde>
fldz
fucomi %st(1),%st
fld    %st(1)
fabs
fxch   %st(2)
fcmovnbe %st(2),%st
fstp   %st(2)
lea    0x40(%rsp),%r15
lea    0xe40(%rip),%r12
lea    0xe3b(%rip),%r13
fld1
fstpt  0x34(%rsp)
fstpt  0x28(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
fadd   %st,%st(0)
fstpt  (%rsp)
mov    %r15,%rdi
call   1060 <modfl@plt>
fldt   0x40(%rsp)
fldt   0x28(%rsp)
fxch   %st(1)
fucomi %st(1),%st
fstp   %st(1)
mov    %r12,%rsi
jne    1202 <__inttobinfloatpart+0xa2>
jnp    1215 <__inttobinfloatpart+0xb5>
fldt   0x34(%rsp)
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
mov    %r13,%rsi
fldz
jne    126b <__inttobinfloatpart+0x10b>
jp     126b <__inttobinfloatpart+0x10b>
fstp   %st(0)
mov    %rbx,%rdi
fstpt  0x1c(%rsp)
call   1080 <strcat@plt>
fldt   0x28(%rsp)
fldt   0x1c(%rsp)
fucomi %st(1),%st
fstp   %st(1)
jne    1233 <__inttobinfloatpart+0xd3>
jnp    123a <__inttobinfloatpart+0xda>
add    $0xffffffff,%ebp
jne    11e0 <__inttobinfloatpart+0x80>
jmp    123e <__inttobinfloatpart+0xde>
fstp   %st(0)
fldz
fstp   %st(0)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <stpcpy@plt>
sub    %r14d,%eax
cltq
movb   $0x0,(%r14,%rax,1)
mov    %rbx,%rdi
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
fstp   %st(1)
fstp   %st(0)
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"0\00"
@.str.1 = [2 x i8] c"1\00"
@.str.2 = [3 x i8] c"%s\00"
define void @__inttobinfloatpart(x86_fp80 %0, i8* %1, i32 %2) {
%4 = alloca x86_fp80
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca x86_fp80
%10 = alloca x86_fp80
store x86_fp80 %0, x86_fp80* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = call i8* @malloc(i32 96)
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = call i32 @memset(i8* %12, i8 signext 0, i32 96)
store i32 0, i32* %8
%14 = load x86_fp80, x86_fp80* %4
%15 = fcmp olt x86_fp80 %14, 0xK00000000000000000000
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = load x86_fp80, x86_fp80* %4
%18 = call x86_fp80 @fabs(x86_fp80 %17)
store x86_fp80 %18, x86_fp80* %4
br label %19
19: ; preds = %16, %3
br label %20
20: ; preds = %45, %19
%21 = load i32, i32* %8
%22 = load i32, i32* %6
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %49
24: ; preds = %20
%25 = load x86_fp80, x86_fp80* %4
%26 = fmul x86_fp80 %25, 0xK40008000000000000000
%27 = call x86_fp80 @modfl(x86_fp80 %26, x86_fp80* %9)
store x86_fp80 %27, x86_fp80* %10
%28 = load x86_fp80, x86_fp80* %9
%29 = fcmp oeq x86_fp80 %28, 0xK00000000000000000000
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = load i8*, i8** %7
%32 = call i32 @strcat(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %41
33: ; preds = %24
%34 = load x86_fp80, x86_fp80* %9
%35 = fcmp oeq x86_fp80 %34, 0xK3FFF8000000000000000
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i8*, i8** %7
%38 = call i32 @strcat(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %40
39: ; preds = %33
br label %59
40: ; preds = %36
br label %41
41: ; preds = %40, %30
%42 = load x86_fp80, x86_fp80* %10
%43 = fcmp oeq x86_fp80 %42, 0xK00000000000000000000
br i1 %43, label %44, label %45
44: ; preds = %41
br label %49
45: ; preds = %41
%46 = load x86_fp80, x86_fp80* %10
store x86_fp80 %46, x86_fp80* %4
%47 = load i32, i32* %8
%48 = add nsw i32 %47, 1
store i32 %48, i32* %8
br label %20
49: ; preds = %44, %20
%50 = load i8*, i8** %5
%51 = load i8*, i8** %7
%52 = call i32 @sprintf(i8* %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %51)
store i32 %52, i32* %8
%53 = load i8*, i8** %5
%54 = load i32, i32* %8
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
store i8 0, i8* %56
%57 = load i8*, i8** %7
%58 = call i32 @free(i8* %57)
br label %59
59: ; preds = %49, %39
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"0\00"
@.str.1 = [2 x i8] c"1\00"
define void @__inttobinfloatpart(x86_fp80 %0, i8* %1, i32 %2) {
%4 = alloca x86_fp80
%5 = tail call dereferenceable_or_null(96) i8* @malloc(i32 96)
%6 = tail call i32 @memset(i8* %5, i8 signext 0, i32 96)
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %31
8: ; preds = %3
%9 = fcmp olt x86_fp80 %0, 0xK00000000000000000000
%10 = tail call x86_fp80 @llvm.fabs.f80(x86_fp80 %0)
%11 = select i1 %9, x86_fp80 %10, x86_fp80 %0
%12 = bitcast x86_fp80* %4 to i8*
br label %13
13: ; preds = %8, %28
%14 = phi x86_fp80 [ %11, %8 ], [ %17, %28 ]
%15 = phi i32 [ 0, %8 ], [ %29, %28 ]
%16 = fmul x86_fp80 %14, 0xK40008000000000000000
%17 = call x86_fp80 @modfl(x86_fp80 %16, x86_fp80* %4)
%18 = load x86_fp80, x86_fp80* %4
%19 = fcmp oeq x86_fp80 %18, 0xK00000000000000000000
br i1 %19, label %23, label %20
20: ; preds = %13
%21 = fcmp oeq x86_fp80 %18, 0xK3FFF8000000000000000
br i1 %21, label %23, label %22
22: ; preds = %20
br label %40
23: ; preds = %20, %13
%24 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %13 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %20 ]
%25 = tail call i32 @strcat(i8* %5, i8* %24)
%26 = fcmp oeq x86_fp80 %17, 0xK00000000000000000000
br i1 %26, label %27, label %28
27: ; preds = %23
br label %31
28: ; preds = %23
%29 = add nuw nsw i32 %15, 1
%30 = icmp eq i32 %29, %2
br i1 %30, label %31, label %13
31: ; preds = %28, %3, %27
%32 = tail call i8* @stpcpy(i8* %1, i8* %5)
%33 = ptrtoint i8* %32 to i64
%34 = ptrtoint i8* %1 to i64
%35 = sub i64 %33, %34
%36 = shl i64 %35, 32
%37 = ashr exact i64 %36, 32
%38 = getelementptr inbounds i8, i8* %1, i64 %37
store i8 0, i8* %38
%39 = tail call i32 @free(i8* %5)
br label %40
40: ; preds = %22, %31
ret void
}