#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 /*<<< orphan*/ FILE;

/* Variables and functions */
double RAND_MAX;
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, int);
double rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(void) {
  int nleft, N, t;
  double r;
  FILE *graf;
  graf = fopen("graf2.dat", "w");
  srand(time(NULL));
  for (N = 8; N <= 10000000; N = N * 2) {
    nleft = N;
    t = 0;
    while (nleft != N / 2) {
      r = rand() * 1.0 / RAND_MAX;
      if (r <= nleft * 1.0 / N) {
        nleft--;
      } else {
        nleft++;
      }
      t++;
    }
    fprintf(graf, "%d\t %d\n", N, t);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea2(%rip),%rdi
lea    0xea5(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
mov    %eax,%edi
call   1030 <srand@plt>
movl   $0x8,-0xc(%rbp)
cmpl   $0x989680,-0xc(%rbp)
jg     125a <main+0x10a>
mov    -0xc(%rbp),%eax
mov    %eax,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
je     1236 <main+0xe6>
mov    $0x0,%al
call   1070 <rand@plt>
movsd  0xe2c(%rip),%xmm1
mulsd  %xmm1,%xmm0
mov    0x2e09(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm1
cvtsi2sdl -0x8(%rbp),%xmm0
movsd  0xe06(%rip),%xmm2
mulsd  %xmm2,%xmm0
cvtsi2sdl -0xc(%rbp),%xmm2
divsd  %xmm2,%xmm0
ucomisd %xmm1,%xmm0
jb     121f <main+0xcf>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
jmp    1228 <main+0xd8>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11a7 <main+0x57>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xdcd(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    118d <main+0x3d>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe9b(%rip),%rdi
lea    0xe9e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r15
xor    %edi,%edi
call   1050 <time@plt>
mov    %eax,%edi
call   1030 <srand@plt>
mov    $0x8,%r12d
mov    0x2e59(%rip),%r13
nop
mov    %r12d,%ebp
shr    %ebp
xorps  %xmm0,%xmm0
cvtsi2sd %r12d,%xmm0
unpcklpd %xmm0,%xmm0
movapd %xmm0,(%rsp)
mov    %r12d,%r14d
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <rand@plt>
xorps  %xmm1,%xmm1
cvtsi2sd %r14d,%xmm1
unpcklpd %xmm1,%xmm0
movapd (%rsp),%xmm1
movlpd 0x0(%r13),%xmm1
divpd  %xmm1,%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
xor    %eax,%eax
ucomisd %xmm0,%xmm1
setb   %al
lea    (%r14,%rax,2),%r14d
add    $0xffffffff,%r14d
add    $0x1,%ebx
cmp    %ebp,%r14d
jne    11b0 <main+0x60>
mov    %r15,%rdi
lea    0xe0f(%rip),%rsi
mov    %r12d,%edx
mov    %ebx,%ecx
call   1040 <fprintf@plt>
add    %r12d,%r12d
cmp    $0x989681,%r12d
jb     1190 <main+0x40>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"graf2.dat\00"
@.str.1 = [2 x i8] c"w\00"
@RAND_MAX = global double 0.000000e+00
@.str.2 = [8 x i8] c"%d\09 %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca double
%6 = alloca i32*
store i32 0, i32* %1
%7 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %6
%8 = call i32 @time(i32* null)
%9 = call i32 @srand(i32 %8)
store i32 8, i32* %3
br label %10
10: ; preds = %47, %0
%11 = load i32, i32* %3
%12 = icmp sle i32 %11, 10000000
br i1 %12, label %13, label %50
13: ; preds = %10
%14 = load i32, i32* %3
store i32 %14, i32* %2
store i32 0, i32* %4
br label %15
15: ; preds = %39, %13
%16 = load i32, i32* %2
%17 = load i32, i32* %3
%18 = sdiv i32 %17, 2
%19 = icmp ne i32 %16, %18
br i1 %19, label %20, label %42
20: ; preds = %15
%21 = call double (...) @rand()
%22 = fmul double %21, 1.000000e+00
%23 = load double, double* @RAND_MAX
%24 = fdiv double %22, %23
store double %24, double* %5
%25 = load double, double* %5
%26 = load i32, i32* %2
%27 = sitofp i32 %26 to double
%28 = fmul double %27, 1.000000e+00
%29 = load i32, i32* %3
%30 = sitofp i32 %29 to double
%31 = fdiv double %28, %30
%32 = fcmp ole double %25, %31
br i1 %32, label %33, label %36
33: ; preds = %20
%34 = load i32, i32* %2
%35 = add nsw i32 %34, -1
store i32 %35, i32* %2
br label %39
36: ; preds = %20
%37 = load i32, i32* %2
%38 = add nsw i32 %37, 1
store i32 %38, i32* %2
br label %39
39: ; preds = %36, %33
%40 = load i32, i32* %4
%41 = add nsw i32 %40, 1
store i32 %41, i32* %4
br label %15
42: ; preds = %15
%43 = load i32*, i32** %6
%44 = load i32, i32* %3
%45 = load i32, i32* %4
%46 = call i32 @fprintf(i32* %43, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %44, i32 %45)
br label %47
47: ; preds = %42
%48 = load i32, i32* %3
%49 = mul nsw i32 %48, 2
store i32 %49, i32* %3
br label %10
50: ; preds = %10
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"graf2.dat\00"
@.str.1 = [2 x i8] c"w\00"
@RAND_MAX = global double 0.000000e+00
@.str.2 = [8 x i8] c"%d\09 %d\0A\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = tail call i32 @time(i32* null)
%3 = tail call i32 @srand(i32 %2)
br label %4
4: ; preds = %0, %26
%5 = phi i32 [ 8, %0 ], [ %28, %26 ]
%6 = lshr exact i32 %5, 1
%7 = sitofp i32 %5 to double
%8 = insertelement <2 x double> poison, double %7, i64 1
br label %9
9: ; preds = %4, %9
%10 = phi i32 [ %5, %4 ], [ %23, %9 ]
%11 = phi i32 [ 0, %4 ], [ %24, %9 ]
%12 = tail call double (...) @rand()
%13 = load double, double* @RAND_MAX
%14 = sitofp i32 %10 to double
%15 = insertelement <2 x double> poison, double %12, i64 0
%16 = insertelement <2 x double> %15, double %14, i64 1
%17 = insertelement <2 x double> %8, double %13, i64 0
%18 = fdiv <2 x double> %16, %17
%19 = extractelement <2 x double> %18, i64 0
%20 = extractelement <2 x double> %18, i64 1
%21 = fcmp ugt double %19, %20
%22 = select i1 %21, i32 1, i32 -1
%23 = add nsw i32 %22, %10
%24 = add nuw nsw i32 %11, 1
%25 = icmp eq i32 %23, %6
br i1 %25, label %26, label %9
26: ; preds = %9
%27 = tail call i32 @fprintf(i32* %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %5, i32 %24)
%28 = shl nsw i32 %5, 1
%29 = icmp ult i32 %28, 10000001
br i1 %29, label %4, label %30
30: ; preds = %26
ret i32 0
}