#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 INT_MAX;
int exp(double);
double loghprob(int, int, int, int);
int modehprob(int, int, int);
int nnint(double);
int random();
double ranexp();
scalar_t__ ranmod(int);
int ranhprob(int n, int a, int m) {
  double y;
  double pm, logpm, w, ru, rw, rat;
  int mode, k, x, zans;
  mode = modehprob(n, a, m);
  logpm = loghprob(n, a, m, mode);
  pm = exp(logpm);
  w = 1 + pm;
  for (;;) {
    ru = ((double)(random() % INT_MAX) / (double)(INT_MAX));
    rw = ((double)(random() % INT_MAX) / (double)(INT_MAX));
    if (ru <= w / (1 + w))
      y = ((double)(random() % INT_MAX) / (double)(INT_MAX)) * w / pm;
    else
      y = (w + ranexp()) / pm;
    x = nnint(y);
    if (ranmod(2) == 0)
      x = -x;
    zans = mode + x;
    if (zans < 0)
      continue;
    if (zans > a)
      continue;
    rat = exp(loghprob(n, a, m, zans) - logpm);
    rw *= ((1) < (exp(w - pm * y)) ? (1) : (exp(w - pm * y)));
    if (rw <= rat)
      break;
  }
  return zans;
}


x86 assembly (clang, -O0):
<ranhprob>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1080 <modehprob@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x4c(%rbp),%ecx
call   1030 <loghprob@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
call   1070 <exp@plt>
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xe3d(%rip),%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    $0x0,%al
call   1050 <random@plt>
mov    0x2e0d(%rip),%rcx
cltd
idivl  (%rcx)
cvtsi2sd %edx,%xmm0
mov    0x2dff(%rip),%rax
cvtsi2sdl (%rax),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    $0x0,%al
call   1050 <random@plt>
mov    0x2de4(%rip),%rcx
cltd
idivl  (%rcx)
cvtsi2sd %edx,%xmm0
mov    0x2dd6(%rip),%rax
cvtsi2sdl (%rax),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x38(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
movsd  0xdcf(%rip),%xmm2
addsd  -0x30(%rbp),%xmm2
divsd  %xmm2,%xmm0
ucomisd %xmm1,%xmm0
jb     127c <ranhprob+0x10c>
mov    $0x0,%al
call   1050 <random@plt>
mov    0x2d96(%rip),%rcx
cltd
idivl  (%rcx)
cvtsi2sd %edx,%xmm0
mov    0x2d88(%rip),%rax
cvtsi2sdl (%rax),%xmm1
divsd  %xmm1,%xmm0
mulsd  -0x30(%rbp),%xmm0
divsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    12a3 <ranhprob+0x133>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
mov    $0x0,%al
call   1060 <ranexp@plt>
movaps %xmm0,%xmm1
movsd  -0x60(%rbp),%xmm0
addsd  %xmm1,%xmm0
divsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1090 <nnint@plt>
mov    %eax,-0x54(%rbp)
mov    $0x2,%edi
call   1040 <ranmod@plt>
cmp    $0x0,%rax
jne    12cc <ranhprob+0x15c>
xor    %eax,%eax
sub    -0x54(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x4c(%rbp),%eax
add    -0x54(%rbp),%eax
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
jge    12e4 <ranhprob+0x174>
jmp    11cd <ranhprob+0x5d>
mov    -0x58(%rbp),%eax
cmp    -0x8(%rbp),%eax
jle    12f5 <ranhprob+0x185>
jmp    11cd <ranhprob+0x5d>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x58(%rbp),%ecx
call   1030 <loghprob@plt>
subsd  -0x28(%rbp),%xmm0
call   1070 <exp@plt>
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x30(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm2
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1070 <exp@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jge    1368 <ranhprob+0x1f8>
mov    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    139e <ranhprob+0x22e>
movsd  -0x30(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm2
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1070 <exp@plt>
mov    %eax,-0x64(%rbp)
mov    -0x64(%rbp),%eax
cvtsi2sd %eax,%xmm0
mulsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm1
movsd  -0x48(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     13c8 <ranhprob+0x258>
jmp    13cd <ranhprob+0x25d>
jmp    11cd <ranhprob+0x5d>
mov    -0x58(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ranhprob>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    %edx,%ebx
mov    %esi,%r14d
mov    %edi,%ebp
call   1080 <modehprob@plt>
mov    %ebp,0x14(%rsp)
mov    %ebp,%edi
mov    %r14d,%esi
mov    %ebx,0x18(%rsp)
mov    %ebx,%edx
mov    %eax,0x1c(%rsp)
mov    %eax,%ecx
call   1030 <loghprob@plt>
movsd  %xmm0,0x38(%rsp)
call   1070 <exp@plt>
cvtsi2sd %eax,%xmm1
movsd  0xe45(%rip),%xmm0
movapd %xmm1,%xmm2
addsd  %xmm0,%xmm2
addsd  %xmm2,%xmm0
movapd %xmm2,0x20(%rsp)
divsd  %xmm0,%xmm2
movapd %xmm2,0x50(%rsp)
movapd 0xe31(%rip),%xmm0
movapd %xmm1,0x60(%rsp)
xorpd  %xmm1,%xmm0
movapd %xmm0,0x40(%rsp)
mov    0x2df2(%rip),%r12
jmp    1234 <ranhprob+0xc4>
nopl   0x0(%rax,%rax,1)
xorps  %xmm0,%xmm0
cvtsi2sd %r15d,%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %r13d,%xmm1
xorps  %xmm2,%xmm2
cvtsi2sd %r14d,%xmm2
xorps  %xmm3,%xmm3
cvtsi2sd %eax,%xmm3
divsd  %xmm1,%xmm0
mulsd  %xmm0,%xmm3
ucomisd %xmm3,%xmm2
mov    %ebx,%r14d
jae    1350 <ranhprob+0x1e0>
xor    %eax,%eax
call   1050 <random@plt>
mov    (%r12),%ecx
cltd
idiv   %ecx
xorps  %xmm1,%xmm1
cvtsi2sd %edx,%xmm1
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
divsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
xor    %eax,%eax
call   1050 <random@plt>
mov    (%r12),%r13d
cltd
idiv   %r13d
mov    %edx,%r15d
movapd 0x50(%rsp),%xmm0
ucomisd 0x8(%rsp),%xmm0
jae    1290 <ranhprob+0x120>
xor    %eax,%eax
call   1060 <ranexp@plt>
addsd  0x20(%rsp),%xmm0
jmp    12b6 <ranhprob+0x146>
nopl   0x0(%rax)
xor    %eax,%eax
call   1050 <random@plt>
mov    (%r12),%ecx
cltd
idiv   %ecx
xorps  %xmm0,%xmm0
cvtsi2sd %edx,%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %ecx,%xmm1
divsd  %xmm1,%xmm0
mulsd  0x20(%rsp),%xmm0
divsd  0x60(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
call   1090 <nnint@plt>
mov    %eax,%ebx
mov    $0x2,%edi
call   1040 <ranmod@plt>
mov    %ebx,%ebp
neg    %ebp
test   %rax,%rax
cmovne %ebx,%ebp
add    0x1c(%rsp),%ebp
js     1234 <ranhprob+0xc4>
cmp    %r14d,%ebp
jg     1234 <ranhprob+0xc4>
mov    0x14(%rsp),%edi
mov    %r14d,%ebx
mov    %r14d,%esi
mov    0x18(%rsp),%edx
mov    %ebp,%ecx
call   1030 <loghprob@plt>
subsd  0x38(%rsp),%xmm0
call   1070 <exp@plt>
mov    %eax,%r14d
movsd  0x8(%rsp),%xmm0
mulsd  0x40(%rsp),%xmm0
addsd  0x20(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
call   1070 <exp@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    $0x1,%ecx
jg     1200 <ranhprob+0x90>
movsd  0x8(%rsp),%xmm0
call   1070 <exp@plt>
jmp    1200 <ranhprob+0x90>
mov    %ebp,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@INT_MAX = global i32 0
define i32 @ranhprob(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%18 = load i32, i32* %4
%19 = load i32, i32* %5
%20 = load i32, i32* %6
%21 = call i32 @modehprob(i32 %18, i32 %19, i32 %20)
store i32 %21, i32* %14
%22 = load i32, i32* %4
%23 = load i32, i32* %5
%24 = load i32, i32* %6
%25 = load i32, i32* %14
%26 = call double @loghprob(i32 %22, i32 %23, i32 %24, i32 %25)
store double %26, double* %9
%27 = load double, double* %9
%28 = call i32 @exp(double %27)
%29 = sitofp i32 %28 to double
store double %29, double* %8
%30 = load double, double* %8
%31 = fadd double 1.000000e+00, %30
store double %31, double* %10
br label %32
32: ; preds = %125, %90, %85, %3
%33 = call i32 (...) @random()
%34 = load i32, i32* @INT_MAX
%35 = srem i32 %33, %34
%36 = sitofp i32 %35 to double
%37 = load i32, i32* @INT_MAX
%38 = sitofp i32 %37 to double
%39 = fdiv double %36, %38
store double %39, double* %11
%40 = call i32 (...) @random()
%41 = load i32, i32* @INT_MAX
%42 = srem i32 %40, %41
%43 = sitofp i32 %42 to double
%44 = load i32, i32* @INT_MAX
%45 = sitofp i32 %44 to double
%46 = fdiv double %43, %45
store double %46, double* %12
%47 = load double, double* %11
%48 = load double, double* %10
%49 = load double, double* %10
%50 = fadd double 1.000000e+00, %49
%51 = fdiv double %48, %50
%52 = fcmp ole double %47, %51
br i1 %52, label %53, label %65
53: ; preds = %32
%54 = call i32 (...) @random()
%55 = load i32, i32* @INT_MAX
%56 = srem i32 %54, %55
%57 = sitofp i32 %56 to double
%58 = load i32, i32* @INT_MAX
%59 = sitofp i32 %58 to double
%60 = fdiv double %57, %59
%61 = load double, double* %10
%62 = fmul double %60, %61
%63 = load double, double* %8
%64 = fdiv double %62, %63
store double %64, double* %7
br label %71
65: ; preds = %32
%66 = load double, double* %10
%67 = call double (...) @ranexp()
%68 = fadd double %66, %67
%69 = load double, double* %8
%70 = fdiv double %68, %69
store double %70, double* %7
br label %71
71: ; preds = %65, %53
%72 = load double, double* %7
%73 = call i32 @nnint(double %72)
store i32 %73, i32* %16
%74 = call i64 @ranmod(i32 2)
%75 = icmp eq i64 %74, 0
br i1 %75, label %76, label %79
76: ; preds = %71
%77 = load i32, i32* %16
%78 = sub nsw i32 0, %77
store i32 %78, i32* %16
br label %79
79: ; preds = %76, %71
%80 = load i32, i32* %14
%81 = load i32, i32* %16
%82 = add nsw i32 %80, %81
store i32 %82, i32* %17
%83 = load i32, i32* %17
%84 = icmp slt i32 %83, 0
br i1 %84, label %85, label %86
85: ; preds = %79
br label %32
86: ; preds = %79
%87 = load i32, i32* %17
%88 = load i32, i32* %5
%89 = icmp sgt i32 %87, %88
br i1 %89, label %90, label %91
90: ; preds = %86
br label %32
91: ; preds = %86
%92 = load i32, i32* %4
%93 = load i32, i32* %5
%94 = load i32, i32* %6
%95 = load i32, i32* %17
%96 = call double @loghprob(i32 %92, i32 %93, i32 %94, i32 %95)
%97 = load double, double* %9
%98 = fsub double %96, %97
%99 = call i32 @exp(double %98)
%100 = sitofp i32 %99 to double
store double %100, double* %13
%101 = load double, double* %10
%102 = load double, double* %8
%103 = load double, double* %7
%104 = fneg double %102
%105 = call double @llvm.fmuladd.f64(double %104, double %103, double %101)
%106 = call i32 @exp(double %105)
%107 = icmp slt i32 1, %106
br i1 %107, label %108, label %109
108: ; preds = %91
br label %116
109: ; preds = %91
%110 = load double, double* %10
%111 = load double, double* %8
%112 = load double, double* %7
%113 = fneg double %111
%114 = call double @llvm.fmuladd.f64(double %113, double %112, double %110)
%115 = call i32 @exp(double %114)
br label %116
116: ; preds = %109, %108
%117 = phi i32 [ 1, %108 ], [ %115, %109 ]
%118 = sitofp i32 %117 to double
%119 = load double, double* %12
%120 = fmul double %119, %118
store double %120, double* %12
%121 = load double, double* %12
%122 = load double, double* %13
%123 = fcmp ole double %121, %122
br i1 %123, label %124, label %125
124: ; preds = %116
br label %126
125: ; preds = %116
br label %32
126: ; preds = %124
%127 = load i32, i32* %17
ret i32 %127
}

LLVM IR (-O2):
@INT_MAX = global i32 0
define i32 @ranhprob(i32 %0, i32 %1, i32 %2) {
%4 = tail call i32 @modehprob(i32 %0, i32 %1, i32 %2)
%5 = tail call double @loghprob(i32 %0, i32 %1, i32 %2, i32 %4)
%6 = tail call i32 @exp(double %5)
%7 = sitofp i32 %6 to double
%8 = fadd double %7, 1.000000e+00
%9 = fadd double %8, 1.000000e+00
%10 = fdiv double %8, %9
%11 = fneg double %7
br label %12
12: ; preds = %49, %3
%13 = tail call i32 (...) @random()
%14 = load i32, i32* @INT_MAX
%15 = srem i32 %13, %14
%16 = sitofp i32 %15 to double
%17 = sitofp i32 %14 to double
%18 = fdiv double %16, %17
%19 = tail call i32 (...) @random()
%20 = load i32, i32* @INT_MAX
%21 = srem i32 %19, %20
%22 = sitofp i32 %21 to double
%23 = sitofp i32 %20 to double
%24 = fdiv double %22, %23
%25 = fcmp ugt double %18, %10
br i1 %25, label %34, label %26
26: ; preds = %12
%27 = tail call i32 (...) @random()
%28 = load i32, i32* @INT_MAX
%29 = srem i32 %27, %28
%30 = sitofp i32 %29 to double
%31 = sitofp i32 %28 to double
%32 = fdiv double %30, %31
%33 = fmul double %8, %32
br label %37
34: ; preds = %12
%35 = tail call double (...) @ranexp()
%36 = fadd double %8, %35
br label %37
37: ; preds = %34, %26
%38 = phi double [ %33, %26 ], [ %36, %34 ]
%39 = fdiv double %38, %7
%40 = tail call i32 @nnint(double %39)
%41 = tail call i64 @ranmod(i32 2)
%42 = icmp eq i64 %41, 0
%43 = sub nsw i32 0, %40
%44 = select i1 %42, i32 %43, i32 %40
%45 = add nsw i32 %44, %4
%46 = icmp slt i32 %45, 0
%47 = icmp sgt i32 %45, %1
%48 = or i1 %46, %47
br i1 %48, label %49, label %50
49: ; preds = %37, %60
br label %12
50: ; preds = %37
%51 = tail call double @loghprob(i32 %0, i32 %1, i32 %2, i32 %45)
%52 = fsub double %51, %5
%53 = tail call i32 @exp(double %52)
%54 = sitofp i32 %53 to double
%55 = tail call double @llvm.fmuladd.f64(double %11, double %39, double %8)
%56 = tail call i32 @exp(double %55)
%57 = icmp sgt i32 %56, 1
br i1 %57, label %60, label %58
58: ; preds = %50
%59 = tail call i32 @exp(double %55)
br label %60
60: ; preds = %50, %58
%61 = phi i32 [ %59, %58 ], [ 1, %50 ]
%62 = sitofp i32 %61 to double
%63 = fmul double %24, %62
%64 = fcmp ugt double %63, %54
br i1 %64, label %49, label %65
65: ; preds = %60
ret i32 %45
}