#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 */
typedef int int32_t;

/* Variables and functions */
int floor(float);
double frandom();
void float_to_32bit_integer_samples(float *samples, int num_samples) {
  int isample, imin = 0x8000000, imax = 0x7fffffff;
  float scalar = 2147483648.0;
  while (num_samples--) {
    if (*samples >= 1.0)
      isample = imax;
    else if (*samples <= -1.0)
      isample = imin;
    else
      isample = floor(*samples * scalar);
    if (isample && !(isample & 1)) {
      int tzeros = 1;
      while (!((isample >>= 1) & 1))
        tzeros++;
      while (tzeros--)
        isample = ((unsigned int)isample << 1) + ((frandom() > 0.5) ? 1 : 0);
    }
    *(int32_t *)samples = isample;
    samples++;
  }
}


x86 assembly (clang, -O0):
<float_to_32bit_integer_samples>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x8000000,-0x14(%rbp)
movl   $0x7fffffff,-0x18(%rbp)
movss  0xebb(%rip),%xmm0
movss  %xmm0,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
cmp    $0x0,%eax
je     1284 <float_to_32bit_integer_samples+0x164>
mov    -0x8(%rbp),%rax
movss  (%rax),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe96(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     1187 <float_to_32bit_integer_samples+0x67>
mov    -0x18(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11ca <float_to_32bit_integer_samples+0xaa>
mov    -0x8(%rbp),%rax
movss  (%rax),%xmm0
cvtss2sd %xmm0,%xmm1
movsd  0xe75(%rip),%xmm0
ucomisd %xmm1,%xmm0
jb     11b0 <float_to_32bit_integer_samples+0x90>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11c5 <float_to_32bit_integer_samples+0xa5>
mov    -0x8(%rbp),%rax
movss  (%rax),%xmm0
mulss  -0x1c(%rbp),%xmm0
call   1040 <floor@plt>
mov    %eax,-0x10(%rbp)
jmp    11ca <float_to_32bit_integer_samples+0xaa>
cmpl   $0x0,-0x10(%rbp)
je     126a <float_to_32bit_integer_samples+0x14a>
mov    -0x10(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    126a <float_to_32bit_integer_samples+0x14a>
movl   $0x1,-0x20(%rbp)
mov    -0x10(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x10(%rbp)
and    $0x1,%eax
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    120b <float_to_32bit_integer_samples+0xeb>
jmp    1219 <float_to_32bit_integer_samples+0xf9>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11ea <float_to_32bit_integer_samples+0xca>
jmp    121e <float_to_32bit_integer_samples+0xfe>
mov    -0x20(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x20(%rbp)
cmp    $0x0,%eax
je     1265 <float_to_32bit_integer_samples+0x145>
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <frandom@plt>
mov    -0x24(%rbp),%eax
movsd  0xdcb(%rip),%xmm1
xor    %ecx,%ecx
mov    $0x1,%edx
ucomisd %xmm1,%xmm0
cmova  %edx,%ecx
add    %ecx,%eax
mov    %eax,-0x10(%rbp)
jmp    121e <float_to_32bit_integer_samples+0xfe>
jmp    126a <float_to_32bit_integer_samples+0x14a>
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x8(%rbp)
jmp    114a <float_to_32bit_integer_samples+0x2a>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<float_to_32bit_integer_samples>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
je     11bd <float_to_32bit_integer_samples+0x9d>
mov    %esi,%r14d
mov    %rdi,%r15
jmp    114c <float_to_32bit_integer_samples+0x2c>
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %ebp,(%r15)
add    $0x4,%r15
test   %r14d,%r14d
je     11bd <float_to_32bit_integer_samples+0x9d>
add    $0xffffffff,%r14d
movss  (%r15),%xmm0
mov    $0x7fffffff,%ebp
ucomiss 0xe9f(%rip),%xmm0
jae    1140 <float_to_32bit_integer_samples+0x20>
mov    $0x8000000,%ebp
movss  0xe94(%rip),%xmm1
ucomiss %xmm0,%xmm1
jae    118e <float_to_32bit_integer_samples+0x6e>
mulss  0xe8b(%rip),%xmm0
call   1040 <floor@plt>
test   %eax,%eax
je     1137 <float_to_32bit_integer_samples+0x17>
mov    %eax,%ebp
test   $0x1,%bpl
jne    1140 <float_to_32bit_integer_samples+0x20>
xor    %eax,%eax
mov    %ebp,%ecx
mov    %eax,%ebx
sar    %ebp
lea    0x1(%rbx),%eax
test   $0x2,%cl
je     1190 <float_to_32bit_integer_samples+0x70>
not    %ebx
xor    %eax,%eax
call   1030 <frandom@plt>
xor    %eax,%eax
ucomisd 0xe5f(%rip),%xmm0
seta   %al
lea    (%rax,%rbp,2),%ebp
inc    %ebx
jne    11a0 <float_to_32bit_integer_samples+0x80>
jmp    1140 <float_to_32bit_integer_samples+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @float_to_32bit_integer_samples(float* %0, i32 %1) {
%3 = alloca float*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca float
%9 = alloca i32
store float* %0, float** %3
store i32 %1, i32* %4
store i32 134217728, i32* %6
store i32 2147483647, i32* %7
store float 0x41E0000000000000, float* %8
br label %10
10: ; preds = %66, %2
%11 = load i32, i32* %4
%12 = add nsw i32 %11, -1
store i32 %12, i32* %4
%13 = icmp ne i32 %11, 0
br i1 %13, label %14, label %72
14: ; preds = %10
%15 = load float*, float** %3
%16 = load float, float* %15
%17 = fpext float %16 to double
%18 = fcmp oge double %17, 1.000000e+00
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = load i32, i32* %7
store i32 %20, i32* %5
br label %35
21: ; preds = %14
%22 = load float*, float** %3
%23 = load float, float* %22
%24 = fpext float %23 to double
%25 = fcmp ole double %24, -1.000000e+00
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = load i32, i32* %6
store i32 %27, i32* %5
br label %34
28: ; preds = %21
%29 = load float*, float** %3
%30 = load float, float* %29
%31 = load float, float* %8
%32 = fmul float %30, %31
%33 = call i32 @floor(float %32)
store i32 %33, i32* %5
br label %34
34: ; preds = %28, %26
br label %35
35: ; preds = %34, %19
%36 = load i32, i32* %5
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %66
38: ; preds = %35
%39 = load i32, i32* %5
%40 = and i32 %39, 1
%41 = icmp ne i32 %40, 0
br i1 %41, label %66, label %42
42: ; preds = %38
store i32 1, i32* %9
br label %43
43: ; preds = %49, %42
%44 = load i32, i32* %5
%45 = ashr i32 %44, 1
store i32 %45, i32* %5
%46 = and i32 %45, 1
%47 = icmp ne i32 %46, 0
%48 = xor i1 %47, true
br i1 %48, label %49, label %52
49: ; preds = %43
%50 = load i32, i32* %9
%51 = add nsw i32 %50, 1
store i32 %51, i32* %9
br label %43
52: ; preds = %43
br label %53
53: ; preds = %57, %52
%54 = load i32, i32* %9
%55 = add nsw i32 %54, -1
store i32 %55, i32* %9
%56 = icmp ne i32 %54, 0
br i1 %56, label %57, label %65
57: ; preds = %53
%58 = load i32, i32* %5
%59 = shl i32 %58, 1
%60 = call double (...) @frandom()
%61 = fcmp ogt double %60, 5.000000e-01
%62 = zext i1 %61 to i64
%63 = select i1 %61, i32 1, i32 0
%64 = add i32 %59, %63
store i32 %64, i32* %5
br label %53
65: ; preds = %53
br label %66
66: ; preds = %65, %38, %35
%67 = load i32, i32* %5
%68 = load float*, float** %3
%69 = bitcast float* %68 to i32*
store i32 %67, i32* %69
%70 = load float*, float** %3
%71 = getelementptr inbounds float, float* %70, i32 1
store float* %71, float** %3
br label %10
72: ; preds = %10
ret void
}

LLVM IR (-O2):
define void @float_to_32bit_integer_samples(float* nocapture %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %43, label %4
4: ; preds = %2, %38
%5 = phi i32 [ %7, %38 ], [ %1, %2 ]
%6 = phi float* [ %41, %38 ], [ %0, %2 ]
%7 = add nsw i32 %5, -1
%8 = load float, float* %6
%9 = fcmp ult float %8, 1.000000e+00
br i1 %9, label %10, label %38
10: ; preds = %4
%11 = fcmp ugt float %8, -1.000000e+00
br i1 %11, label %12, label %19
12: ; preds = %10
%13 = fmul float %8, 0x41E0000000000000
%14 = tail call i32 @floor(float %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %38, label %16
16: ; preds = %12
%17 = and i32 %14, 1
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %38
19: ; preds = %10, %16
%20 = phi i32 [ 134217728, %10 ], [ %14, %16 ]
br label %21
21: ; preds = %19, %21
%22 = phi i32 [ %24, %21 ], [ %20, %19 ]
%23 = phi i32 [ %27, %21 ], [ 1, %19 ]
%24 = ashr i32 %22, 1
%25 = and i32 %22, 2
%26 = icmp eq i32 %25, 0
%27 = add nuw nsw i32 %23, 1
br i1 %26, label %21, label %28
28: ; preds = %21, %28
%29 = phi i32 [ %31, %28 ], [ %23, %21 ]
%30 = phi i32 [ %36, %28 ], [ %24, %21 ]
%31 = add nsw i32 %29, -1
%32 = shl i32 %30, 1
%33 = tail call double (...) @frandom()
%34 = fcmp ogt double %33, 5.000000e-01
%35 = zext i1 %34 to i32
%36 = or i32 %32, %35
%37 = icmp eq i32 %31, 0
br i1 %37, label %38, label %28
38: ; preds = %28, %4, %16, %12
%39 = phi i32 [ %14, %16 ], [ 0, %12 ], [ 2147483647, %4 ], [ %36, %28 ]
%40 = bitcast float* %6 to i32*
store i32 %39, i32* %40
%41 = getelementptr inbounds float, float* %6, i64 1
%42 = icmp eq i32 %7, 0
br i1 %42, label %43, label %4
43: ; preds = %38, %2
ret void
}