#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 */
scalar_t__ RAND_MAX;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
double pow(double, double);
scalar_t__ rand();
int get_zipf_key(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 = (double)rand() / ((double)RAND_MAX);
  } 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;
    }
  }
  if ((zipf_value < 1) || (zipf_value > n)) {
    perror("zipf result invalid");
    exit(-1);
  }
  return zipf_value;
}


x86 assembly (clang, -O0):
<get_zipf_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
cmpl   $0x1,0x2ee9(%rip)
jne    11dc <get_zipf_key+0x9c>
movl   $0x1,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     11ba <get_zipf_key+0x7a>
movsd  0x2ed8(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
cvtsi2sdl -0x2c(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm1
call   1030 <pow@plt>
movaps %xmm0,%xmm2
movsd  -0x38(%rbp),%xmm0
movsd  0xe64(%rip),%xmm1
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,0x2ea4(%rip)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1164 <get_zipf_key+0x24>
movsd  0xe3e(%rip),%xmm0
divsd  0x2e86(%rip),%xmm0
movsd  %xmm0,0x2e7e(%rip)
movl   $0x0,0x2e64(%rip)
jmp    11e1 <get_zipf_key+0xa1>
mov    $0x0,%al
call   1060 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2df4(%rip),%rax
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
mov    $0x1,%al
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x39(%rbp)
jne    1224 <get_zipf_key+0xe4>
jp     1224 <get_zipf_key+0xe4>
jmp    1240 <get_zipf_key+0x100>
movsd  -0x18(%rbp),%xmm0
movsd  0xdcf(%rip),%xmm1
ucomisd %xmm1,%xmm0
sete   %al
setnp  %cl
and    %cl,%al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    11e1 <get_zipf_key+0xa1>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     12d8 <get_zipf_key+0x198>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  0x2dd8(%rip),%xmm0
movsd  %xmm0,-0x50(%rbp)
cvtsi2sdl -0x2c(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm1
call   1030 <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     12c5 <get_zipf_key+0x185>
cvtsi2sdl -0x2c(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    12d8 <get_zipf_key+0x198>
jmp    12ca <get_zipf_key+0x18a>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    125a <get_zipf_key+0x11a>
movsd  0xd20(%rip),%xmm0
ucomisd -0x28(%rbp),%xmm0
ja     12ff <get_zipf_key+0x1bf>
movsd  -0x28(%rbp),%xmm0
cvtsi2sdl -0xc(%rbp),%xmm1
ucomisd %xmm1,%xmm0
jbe    1315 <get_zipf_key+0x1d5>
lea    0xd02(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
cvttsd2si -0x28(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_zipf_key>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%r15d
movsd  %xmm0,0x8(%rsp)
cmpb   $0x0,0x2eee(%rip)
jne    1252 <get_zipf_key+0x112>
movsd  0x2ee8(%rip),%xmm1
test   %r15d,%r15d
jle    1237 <get_zipf_key+0xf7>
cmp    $0x1,%r15d
jne    118e <get_zipf_key+0x4e>
movsd  0xe81(%rip),%xmm0
test   $0x1,%r15b
jne    1208 <get_zipf_key+0xc8>
jmp    1237 <get_zipf_key+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   1030 <pow@plt>
movsd  0xe41(%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   1030 <pow@plt>
movsd  0xe16(%rip),%xmm1
divsd  %xmm0,%xmm1
addsd  (%rsp),%xmm1
add    $0x2,%ebp
cmp    %r14d,%ebx
jne    11a0 <get_zipf_key+0x60>
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
test   $0x1,%r15b
je     1237 <get_zipf_key+0xf7>
movsd  %xmm1,(%rsp)
movsd  0x8(%rsp),%xmm1
call   1030 <pow@plt>
movsd  0xde0(%rip),%xmm1
divsd  %xmm0,%xmm1
movsd  (%rsp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rsp)
movsd  (%rsp),%xmm1
movsd  0xdc1(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,0x2e05(%rip)
movb   $0x1,0x2df6(%rip)
mov    0x2d8f(%rip),%rbp
nopl   0x0(%rax)
xor    %eax,%eax
call   1060 <rand@plt>
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
xorps  %xmm0,%xmm0
cvtsi2sdq 0x0(%rbp),%xmm0
divsd  %xmm0,%xmm1
ucomisd 0xd84(%rip),%xmm1
jne    1288 <get_zipf_key+0x148>
jnp    1260 <get_zipf_key+0x120>
ucomisd 0xd70(%rip),%xmm1
jne    1294 <get_zipf_key+0x154>
jnp    1260 <get_zipf_key+0x120>
test   %r15d,%r15d
jle    1306 <get_zipf_key+0x1c6>
xorpd  %xmm0,%xmm0
mov    $0x1,%ebp
movsd  %xmm1,0x10(%rsp)
nopl   0x0(%rax,%rax,1)
movsd  %xmm0,(%rsp)
movsd  0x2d93(%rip),%xmm0
movsd  %xmm0,0x20(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
movsd  %xmm0,0x18(%rsp)
movsd  0x8(%rsp),%xmm1
call   1030 <pow@plt>
movsd  0x10(%rsp),%xmm1
movsd  0x20(%rsp),%xmm2
divsd  %xmm0,%xmm2
movapd %xmm2,%xmm0
addsd  (%rsp),%xmm0
ucomisd %xmm1,%xmm0
jae    1306 <get_zipf_key+0x1c6>
lea    0x1(%rbp),%eax
cmp    %ebp,%r15d
mov    %eax,%ebp
jne    12b0 <get_zipf_key+0x170>
jmp    1306 <get_zipf_key+0x1c6>
movsd  0xcf2(%rip),%xmm0
movsd  0x18(%rsp),%xmm1
ucomisd %xmm1,%xmm0
ja     1337 <get_zipf_key+0x1f7>
xorps  %xmm0,%xmm0
cvtsi2sd %r15d,%xmm0
ucomisd %xmm0,%xmm1
ja     1337 <get_zipf_key+0x1f7>
cvttsd2si %xmm1,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcd2(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@get_zipf_key.first = internal global i32 1
@get_zipf_key.c = internal global double 0.000000e+00
@RAND_MAX = global i64 0
@.str = [20 x i8] c"zipf result invalid\00"
define i32 @get_zipf_key(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* @get_zipf_key.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* @get_zipf_key.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* @get_zipf_key.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* @get_zipf_key.c
%29 = fdiv double 1.000000e+00, %28
store double %29, double* @get_zipf_key.c
store i32 0, i32* @get_zipf_key.first
br label %30
30: ; preds = %27, %2
br label %31
31: ; preds = %43, %30
%32 = call i64 (...) @rand()
%33 = sitofp i64 %32 to double
%34 = load i64, i64* @RAND_MAX
%35 = sitofp i64 %34 to double
%36 = fdiv double %33, %35
store double %36, double* %5
br label %37
37: ; preds = %31
%38 = load double, double* %5
%39 = fcmp oeq double %38, 0.000000e+00
br i1 %39, label %43, label %40
40: ; preds = %37
%41 = load double, double* %5
%42 = fcmp oeq double %41, 1.000000e+00
br label %43
43: ; preds = %40, %37
%44 = phi i1 [ true, %37 ], [ %42, %40 ]
br i1 %44, label %31, label %45
45: ; preds = %43
store double 0.000000e+00, double* %6
store i32 1, i32* %8
br label %46
46: ; preds = %66, %45
%47 = load i32, i32* %8
%48 = load i32, i32* %4
%49 = icmp sle i32 %47, %48
br i1 %49, label %50, label %69
50: ; preds = %46
%51 = load double, double* %6
%52 = load double, double* @get_zipf_key.c
%53 = load i32, i32* %8
%54 = sitofp i32 %53 to double
%55 = load double, double* %3
%56 = call double @pow(double %54, double %55)
%57 = fdiv double %52, %56
%58 = fadd double %51, %57
store double %58, double* %6
%59 = load double, double* %6
%60 = load double, double* %5
%61 = fcmp oge double %59, %60
br i1 %61, label %62, label %65
62: ; preds = %50
%63 = load i32, i32* %8
%64 = sitofp i32 %63 to double
store double %64, double* %7
br label %69
65: ; preds = %50
br label %66
66: ; preds = %65
%67 = load i32, i32* %8
%68 = add nsw i32 %67, 1
store i32 %68, i32* %8
br label %46
69: ; preds = %62, %46
%70 = load double, double* %7
%71 = fcmp olt double %70, 1.000000e+00
br i1 %71, label %77, label %72
72: ; preds = %69
%73 = load double, double* %7
%74 = load i32, i32* %4
%75 = sitofp i32 %74 to double
%76 = fcmp ogt double %73, %75
br i1 %76, label %77, label %80
77: ; preds = %72, %69
%78 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%79 = call i32 @exit(i32 -1)
unreachable
80: ; preds = %72
%81 = load double, double* %7
%82 = fptosi double %81 to i32
ret i32 %82
}

LLVM IR (-O2):
@get_zipf_key.first = internal unnamed_addr global i1 false
@get_zipf_key.c = internal unnamed_addr global double 0.000000e+00
@RAND_MAX = global i64 0
@.str = [20 x i8] c"zipf result invalid\00"
define i32 @get_zipf_key(double %0, i32 %1) {
%3 = load i1, i1* @get_zipf_key.first
br i1 %3, label %42, label %4
4: ; preds = %2
%5 = icmp slt i32 %1, 1
%6 = load double, double* @get_zipf_key.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* @get_zipf_key.c
store i1 true, i1* @get_zipf_key.first
br label %42
42: ; preds = %39, %2
br label %43
43: ; preds = %42, %43
%44 = tail call i64 (...) @rand()
%45 = sitofp i64 %44 to double
%46 = load i64, i64* @RAND_MAX
%47 = sitofp i64 %46 to double
%48 = fdiv double %45, %47
%49 = fcmp oeq double %48, 0.000000e+00
%50 = fcmp oeq double %48, 1.000000e+00
%51 = or i1 %49, %50
br i1 %51, label %43, label %52
52: ; preds = %43
%53 = icmp slt i32 %1, 1
br i1 %53, label %66, label %54
54: ; preds = %52, %54
%55 = phi i32 [ %63, %54 ], [ 1, %52 ]
%56 = phi double [ %61, %54 ], [ 0.000000e+00, %52 ]
%57 = load double, double* @get_zipf_key.c
%58 = sitofp i32 %55 to double
%59 = tail call double @pow(double %58, double %0)
%60 = fdiv double %57, %59
%61 = fadd double %56, %60
%62 = fcmp oge double %61, %48
%63 = add nuw i32 %55, 1
%64 = icmp eq i32 %55, %1
%65 = select i1 %62, i1 true, i1 %64
br i1 %65, label %66, label %54
66: ; preds = %54, %52
%67 = phi double [ undef, %52 ], [ %58, %54 ]
%68 = fcmp olt double %67, 1.000000e+00
%69 = sitofp i32 %1 to double
%70 = fcmp ogt double %67, %69
%71 = or i1 %68, %70
br i1 %71, label %72, label %75
72: ; preds = %66
%73 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%74 = tail call i32 @exit(i32 -1)
unreachable
75: ; preds = %66
%76 = fptosi double %67 to i32
ret i32 %76
}