#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*/ assert(int);
double pow(double, double);
double rand_val(int /*<<< orphan*/);
int zipf(double alpha, int n) {
  static int first = 1;
  static double c = 0;
  double z;
  double sum_prob;
  double zipf_value;
  int i;
  if (first == 1) {
    for (i = 1; i <= n; i++)
      c = c + (1.0 / pow((double)i, alpha));
    c = 1.0 / c;
    first = 0;
  }
  do {
    z = rand_val(0);
  } while ((z == 0) || (z == 1));
  sum_prob = 0;
  for (i = 1; i <= n; i++) {
    sum_prob = sum_prob + c / pow((double)i, alpha);
    if (sum_prob >= z) {
      zipf_value = i;
      break;
    }
  }
  assert((zipf_value >= 1) && (zipf_value <= n));
  return (zipf_value);
}


x86 assembly (clang, -O0):
<zipf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
cmpl   $0x1,0x2ef1(%rip)
jne    11cc <zipf+0x9c>
movl   $0x1,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     11aa <zipf+0x7a>
movsd  0x2ee0(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
cvtsi2sdl -0x2c(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm1
call   1050 <pow@plt>
movaps %xmm0,%xmm2
movsd  -0x38(%rbp),%xmm0
movsd  0xe74(%rip),%xmm1
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,0x2eac(%rip)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1154 <zipf+0x24>
movsd  0xe4e(%rip),%xmm0
divsd  0x2e8e(%rip),%xmm0
movsd  %xmm0,0x2e86(%rip)
movl   $0x0,0x2e6c(%rip)
jmp    11d1 <zipf+0xa1>
xor    %edi,%edi
call   1030 <rand_val@plt>
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
mov    $0x1,%al
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x39(%rbp)
jne    11ff <zipf+0xcf>
jp     11ff <zipf+0xcf>
jmp    121b <zipf+0xeb>
movsd  -0x18(%rbp),%xmm0
movsd  0xdf4(%rip),%xmm1
ucomisd %xmm1,%xmm0
sete   %al
setnp  %cl
and    %cl,%al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    11d1 <zipf+0xa1>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     12b3 <zipf+0x183>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  0x2df5(%rip),%xmm0
movsd  %xmm0,-0x50(%rbp)
cvtsi2sdl -0x2c(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm1
call   1050 <pow@plt>
movsd  -0x50(%rbp),%xmm1
movaps %xmm0,%xmm2
movsd  -0x48(%rbp),%xmm0
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
ucomisd -0x18(%rbp),%xmm0
jb     12a0 <zipf+0x170>
cvtsi2sdl -0x2c(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    12b3 <zipf+0x183>
jmp    12a5 <zipf+0x175>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1235 <zipf+0x105>
movsd  -0x28(%rbp),%xmm0
xor    %eax,%eax
movsd  0xd3e(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x51(%rbp)
jb     12e3 <zipf+0x1b3>
movsd  -0x28(%rbp),%xmm1
cvtsi2sdl -0xc(%rbp),%xmm0
ucomisd %xmm1,%xmm0
setae  %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cvttsd2si -0x28(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zipf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%r15d
movsd  %xmm0,0x8(%rsp)
cmpb   $0x0,0x2ef6(%rip)
jne    1250 <zipf+0x120>
movsd  0x2ef0(%rip),%xmm1
test   %r15d,%r15d
jle    1227 <zipf+0xf7>
cmp    $0x1,%r15d
jne    117e <zipf+0x4e>
movsd  0xe91(%rip),%xmm0
test   $0x1,%r15b
jne    11f8 <zipf+0xc8>
jmp    1227 <zipf+0xf7>
mov    %r15d,%r14d
and    $0xfffffffe,%r14d
mov    $0x1,%ebp
nopw   0x0(%rax,%rax,1)
movsd  %xmm1,(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
movsd  0x8(%rsp),%xmm1
call   1050 <pow@plt>
movsd  0xe51(%rip),%xmm1
divsd  %xmm0,%xmm1
addsd  (%rsp),%xmm1
movsd  %xmm1,(%rsp)
lea    0x1(%rbp),%ebx
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
movsd  0x8(%rsp),%xmm1
call   1050 <pow@plt>
movsd  0xe26(%rip),%xmm1
divsd  %xmm0,%xmm1
addsd  (%rsp),%xmm1
add    $0x2,%ebp
cmp    %r14d,%ebx
jne    1190 <zipf+0x60>
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
test   $0x1,%r15b
je     1227 <zipf+0xf7>
movsd  %xmm1,(%rsp)
movsd  0x8(%rsp),%xmm1
call   1050 <pow@plt>
movsd  0xdf0(%rip),%xmm1
divsd  %xmm0,%xmm1
movsd  (%rsp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rsp)
movsd  (%rsp),%xmm1
movsd  0xdd1(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,0x2e0d(%rip)
movb   $0x1,0x2dfe(%rip)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %edi,%edi
call   1030 <rand_val@plt>
ucomisd 0xda9(%rip),%xmm0
jne    1263 <zipf+0x133>
jnp    1250 <zipf+0x120>
ucomisd 0xd95(%rip),%xmm0
jne    126f <zipf+0x13f>
jnp    1250 <zipf+0x120>
test   %r15d,%r15d
jle    12e2 <zipf+0x1b2>
xorpd  %xmm1,%xmm1
mov    $0x1,%ebp
movsd  %xmm0,0x18(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  %xmm1,(%rsp)
movsd  0x2dab(%rip),%xmm0
movsd  %xmm0,0x20(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
movsd  %xmm0,0x10(%rsp)
movsd  0x8(%rsp),%xmm1
call   1050 <pow@plt>
movsd  0x20(%rsp),%xmm1
divsd  %xmm0,%xmm1
movsd  0x18(%rsp),%xmm0
addsd  (%rsp),%xmm1
ucomisd %xmm0,%xmm1
jae    12e2 <zipf+0x1b2>
lea    0x1(%rbp),%eax
cmp    %ebp,%r15d
mov    %eax,%ebp
jne    1290 <zipf+0x160>
jmp    12e2 <zipf+0x1b2>
movsd  0x10(%rsp),%xmm1
ucomisd 0xd10(%rip),%xmm1
setae  %al
xorps  %xmm0,%xmm0
cvtsi2sd %r15d,%xmm0
ucomisd %xmm1,%xmm0
setae  %cl
and    %al,%cl
movzbl %cl,%edi
call   1040 <assert@plt>
cvttsd2si 0x10(%rsp),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@zipf.first = internal global i32 1
@zipf.c = internal global double 0.000000e+00
define i32 @zipf(double %0, i32 %1) {
%3 = alloca double
%4 = alloca i32
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca i32
store double %0, double* %3
store i32 %1, i32* %4
%9 = load i32, i32* @zipf.first
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %30
11: ; preds = %2
store i32 1, i32* %8
br label %12
12: ; preds = %24, %11
%13 = load i32, i32* %8
%14 = load i32, i32* %4
%15 = icmp sle i32 %13, %14
br i1 %15, label %16, label %27
16: ; preds = %12
%17 = load double, double* @zipf.c
%18 = load i32, i32* %8
%19 = sitofp i32 %18 to double
%20 = load double, double* %3
%21 = call double @pow(double %19, double %20)
%22 = fdiv double 1.000000e+00, %21
%23 = fadd double %17, %22
store double %23, double* @zipf.c
br label %24
24: ; preds = %16
%25 = load i32, i32* %8
%26 = add nsw i32 %25, 1
store i32 %26, i32* %8
br label %12
27: ; preds = %12
%28 = load double, double* @zipf.c
%29 = fdiv double 1.000000e+00, %28
store double %29, double* @zipf.c
store i32 0, i32* @zipf.first
br label %30
30: ; preds = %27, %2
br label %31
31: ; preds = %39, %30
%32 = call double @rand_val(i32 0)
store double %32, double* %5
br label %33
33: ; preds = %31
%34 = load double, double* %5
%35 = fcmp oeq double %34, 0.000000e+00
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = load double, double* %5
%38 = fcmp oeq double %37, 1.000000e+00
br label %39
39: ; preds = %36, %33
%40 = phi i1 [ true, %33 ], [ %38, %36 ]
br i1 %40, label %31, label %41
41: ; preds = %39
store double 0.000000e+00, double* %6
store i32 1, i32* %8
br label %42
42: ; preds = %62, %41
%43 = load i32, i32* %8
%44 = load i32, i32* %4
%45 = icmp sle i32 %43, %44
br i1 %45, label %46, label %65
46: ; preds = %42
%47 = load double, double* %6
%48 = load double, double* @zipf.c
%49 = load i32, i32* %8
%50 = sitofp i32 %49 to double
%51 = load double, double* %3
%52 = call double @pow(double %50, double %51)
%53 = fdiv double %48, %52
%54 = fadd double %47, %53
store double %54, double* %6
%55 = load double, double* %6
%56 = load double, double* %5
%57 = fcmp oge double %55, %56
br i1 %57, label %58, label %61
58: ; preds = %46
%59 = load i32, i32* %8
%60 = sitofp i32 %59 to double
store double %60, double* %7
br label %65
61: ; preds = %46
br label %62
62: ; preds = %61
%63 = load i32, i32* %8
%64 = add nsw i32 %63, 1
store i32 %64, i32* %8
br label %42
65: ; preds = %58, %42
%66 = load double, double* %7
%67 = fcmp oge double %66, 1.000000e+00
br i1 %67, label %68, label %73
68: ; preds = %65
%69 = load double, double* %7
%70 = load i32, i32* %4
%71 = sitofp i32 %70 to double
%72 = fcmp ole double %69, %71
br label %73
73: ; preds = %68, %65
%74 = phi i1 [ false, %65 ], [ %72, %68 ]
%75 = zext i1 %74 to i32
%76 = call i32 @assert(i32 %75)
%77 = load double, double* %7
%78 = fptosi double %77 to i32
ret i32 %78
}

LLVM IR (-O2):
@zipf.first = internal unnamed_addr global i1 false
@zipf.c = internal unnamed_addr global double 0.000000e+00
define i32 @zipf(double %0, i32 %1) {
%3 = load i1, i1* @zipf.first
br i1 %3, label %42, label %4
4: ; preds = %2
%5 = icmp slt i32 %1, 1
%6 = load double, double* @zipf.c
br i1 %5, label %39, label %7
7: ; preds = %4
%8 = and i32 %1, 1
%9 = icmp eq i32 %1, 1
br i1 %9, label %30, label %10
10: ; preds = %7
%11 = and i32 %1, -2
br label %12
12: ; preds = %12, %10
%13 = phi double [ %6, %10 ], [ %24, %12 ]
%14 = phi i32 [ 1, %10 ], [ %25, %12 ]
%15 = phi i32 [ 0, %10 ], [ %26, %12 ]
%16 = sitofp i32 %14 to double
%17 = tail call double @pow(double %16, double %0)
%18 = fdiv double 1.000000e+00, %17
%19 = fadd double %13, %18
%20 = add nuw i32 %14, 1
%21 = sitofp i32 %20 to double
%22 = tail call double @pow(double %21, double %0)
%23 = fdiv double 1.000000e+00, %22
%24 = fadd double %19, %23
%25 = add nuw i32 %14, 2
%26 = add i32 %15, 2
%27 = icmp eq i32 %26, %11
br i1 %27, label %28, label %12
28: ; preds = %12
%29 = sitofp i32 %25 to double
br label %30
30: ; preds = %28, %7
%31 = phi double [ undef, %7 ], [ %24, %28 ]
%32 = phi double [ %6, %7 ], [ %24, %28 ]
%33 = phi double [ 1.000000e+00, %7 ], [ %29, %28 ]
%34 = icmp eq i32 %8, 0
br i1 %34, label %39, label %35
35: ; preds = %30
%36 = tail call double @pow(double %33, double %0)
%37 = fdiv double 1.000000e+00, %36
%38 = fadd double %32, %37
br label %39
39: ; preds = %35, %30, %4
%40 = phi double [ %6, %4 ], [ %31, %30 ], [ %38, %35 ]
%41 = fdiv double 1.000000e+00, %40
store double %41, double* @zipf.c
store i1 true, i1* @zipf.first
br label %42
42: ; preds = %39, %2
br label %43
43: ; preds = %42, %43
%44 = tail call double @rand_val(i32 0)
%45 = fcmp oeq double %44, 0.000000e+00
%46 = fcmp oeq double %44, 1.000000e+00
%47 = or i1 %45, %46
br i1 %47, label %43, label %48
48: ; preds = %43
%49 = icmp slt i32 %1, 1
br i1 %49, label %62, label %50
50: ; preds = %48, %50
%51 = phi i32 [ %59, %50 ], [ 1, %48 ]
%52 = phi double [ %57, %50 ], [ 0.000000e+00, %48 ]
%53 = load double, double* @zipf.c
%54 = sitofp i32 %51 to double
%55 = tail call double @pow(double %54, double %0)
%56 = fdiv double %53, %55
%57 = fadd double %52, %56
%58 = fcmp oge double %57, %44
%59 = add nuw i32 %51, 1
%60 = icmp eq i32 %51, %1
%61 = select i1 %58, i1 true, i1 %60
br i1 %61, label %62, label %50
62: ; preds = %50, %48
%63 = phi double [ undef, %48 ], [ %54, %50 ]
%64 = fcmp oge double %63, 1.000000e+00
%65 = sitofp i32 %1 to double
%66 = fcmp ole double %63, %65
%67 = and i1 %64, %66
%68 = zext i1 %67 to i32
%69 = tail call i32 @assert(i32 %68)
%70 = fptosi double %63 to i32
ret i32 %70
}