#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 */
float RAND_MAX;
int rand();
int generate(float probs[]) {
  int random = rand();
  if ((random -= (probs[0] * RAND_MAX)) <= 0) {
    return 0;
  }
  if ((random -= (probs[1] * RAND_MAX)) <= 0) {
    return 1;
  }
  if ((random -= (probs[2] * RAND_MAX)) <= 0) {
    return 2;
  }
  if ((random -= (probs[3] * RAND_MAX)) <= 0) {
    return 3;
  }
  return -1;
}


x86 assembly (clang, -O0):
<generate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <rand@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movss  (%rax),%xmm0
mov    0x2eb3(%rip),%rax
movss  (%rax),%xmm2
cvtsi2ssl -0x14(%rbp),%xmm1
movd   %xmm0,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm0
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jg     116f <generate+0x5f>
movl   $0x0,-0x4(%rbp)
jmp    1254 <generate+0x144>
mov    -0x10(%rbp),%rax
movss  0x4(%rax),%xmm0
mov    0x2e69(%rip),%rax
movss  (%rax),%xmm2
cvtsi2ssl -0x14(%rbp),%xmm1
movd   %xmm0,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm0
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jg     11b9 <generate+0xa9>
movl   $0x1,-0x4(%rbp)
jmp    1254 <generate+0x144>
mov    -0x10(%rbp),%rax
movss  0x8(%rax),%xmm0
mov    0x2e1f(%rip),%rax
movss  (%rax),%xmm2
cvtsi2ssl -0x14(%rbp),%xmm1
movd   %xmm0,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm0
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jg     1203 <generate+0xf3>
movl   $0x2,-0x4(%rbp)
jmp    1254 <generate+0x144>
mov    -0x10(%rbp),%rax
movss  0xc(%rax),%xmm0
mov    0x2dd5(%rip),%rax
movss  (%rax),%xmm2
cvtsi2ssl -0x14(%rbp),%xmm1
movd   %xmm0,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm0
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jg     124d <generate+0x13d>
movl   $0x3,-0x4(%rbp)
jmp    1254 <generate+0x144>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <rand@plt>
cvtsi2ss %eax,%xmm1
mov    0x2ebe(%rip),%rax
movss  (%rax),%xmm0
movss  (%rbx),%xmm2
mulss  %xmm0,%xmm2
subss  %xmm2,%xmm1
cvttss2si %xmm1,%eax
test   %eax,%eax
jle    11aa <generate+0x9a>
xorps  %xmm1,%xmm1
cvtsi2ss %eax,%xmm1
movss  0x4(%rbx),%xmm2
mulss  %xmm0,%xmm2
subss  %xmm2,%xmm1
cvttss2si %xmm1,%eax
test   %eax,%eax
jle    119e <generate+0x8e>
xorps  %xmm1,%xmm1
cvtsi2ss %eax,%xmm1
movss  0x8(%rbx),%xmm2
mulss  %xmm0,%xmm2
subss  %xmm2,%xmm1
cvttss2si %xmm1,%eax
test   %eax,%eax
jle    11a5 <generate+0x95>
xorps  %xmm1,%xmm1
cvtsi2ss %eax,%xmm1
mulss  0xc(%rbx),%xmm0
subss  %xmm0,%xmm1
cvttss2si %xmm1,%eax
xor    %ecx,%ecx
test   %eax,%eax
setle  %cl
lea    -0x1(,%rcx,4),%ebp
jmp    11aa <generate+0x9a>
mov    $0x1,%ebp
jmp    11aa <generate+0x9a>
mov    $0x2,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@RAND_MAX = global float 0.000000e+00
define i32 @generate(float* %0) {
%2 = alloca i32
%3 = alloca float*
%4 = alloca i32
store float* %0, float** %3
%5 = call i32 (...) @rand()
store i32 %5, i32* %4
%6 = load float*, float** %3
%7 = getelementptr inbounds float, float* %6, i64 0
%8 = load float, float* %7
%9 = load float, float* @RAND_MAX
%10 = load i32, i32* %4
%11 = sitofp i32 %10 to float
%12 = fneg float %8
%13 = call float @llvm.fmuladd.f32(float %12, float %9, float %11)
%14 = fptosi float %13 to i32
store i32 %14, i32* %4
%15 = icmp sle i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 0, i32* %2
br label %54
17: ; preds = %1
%18 = load float*, float** %3
%19 = getelementptr inbounds float, float* %18, i64 1
%20 = load float, float* %19
%21 = load float, float* @RAND_MAX
%22 = load i32, i32* %4
%23 = sitofp i32 %22 to float
%24 = fneg float %20
%25 = call float @llvm.fmuladd.f32(float %24, float %21, float %23)
%26 = fptosi float %25 to i32
store i32 %26, i32* %4
%27 = icmp sle i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %17
store i32 1, i32* %2
br label %54
29: ; preds = %17
%30 = load float*, float** %3
%31 = getelementptr inbounds float, float* %30, i64 2
%32 = load float, float* %31
%33 = load float, float* @RAND_MAX
%34 = load i32, i32* %4
%35 = sitofp i32 %34 to float
%36 = fneg float %32
%37 = call float @llvm.fmuladd.f32(float %36, float %33, float %35)
%38 = fptosi float %37 to i32
store i32 %38, i32* %4
%39 = icmp sle i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %29
store i32 2, i32* %2
br label %54
41: ; preds = %29
%42 = load float*, float** %3
%43 = getelementptr inbounds float, float* %42, i64 3
%44 = load float, float* %43
%45 = load float, float* @RAND_MAX
%46 = load i32, i32* %4
%47 = sitofp i32 %46 to float
%48 = fneg float %44
%49 = call float @llvm.fmuladd.f32(float %48, float %45, float %47)
%50 = fptosi float %49 to i32
store i32 %50, i32* %4
%51 = icmp sle i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %41
store i32 3, i32* %2
br label %54
53: ; preds = %41
store i32 -1, i32* %2
br label %54
54: ; preds = %53, %52, %40, %28, %16
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
@RAND_MAX = global float 0.000000e+00
define i32 @generate(float* nocapture %0) {
%2 = tail call i32 (...) @rand()
%3 = load float, float* %0
%4 = load float, float* @RAND_MAX
%5 = sitofp i32 %2 to float
%6 = fneg float %3
%7 = tail call float @llvm.fmuladd.f32(float %6, float %4, float %5)
%8 = fptosi float %7 to i32
%9 = icmp slt i32 %8, 1
br i1 %9, label %35, label %10
10: ; preds = %1
%11 = getelementptr inbounds float, float* %0, i64 1
%12 = load float, float* %11
%13 = sitofp i32 %8 to float
%14 = fneg float %12
%15 = tail call float @llvm.fmuladd.f32(float %14, float %4, float %13)
%16 = fptosi float %15 to i32
%17 = icmp slt i32 %16, 1
br i1 %17, label %35, label %18
18: ; preds = %10
%19 = getelementptr inbounds float, float* %0, i64 2
%20 = load float, float* %19
%21 = sitofp i32 %16 to float
%22 = fneg float %20
%23 = tail call float @llvm.fmuladd.f32(float %22, float %4, float %21)
%24 = fptosi float %23 to i32
%25 = icmp slt i32 %24, 1
br i1 %25, label %35, label %26
26: ; preds = %18
%27 = getelementptr inbounds float, float* %0, i64 3
%28 = load float, float* %27
%29 = sitofp i32 %24 to float
%30 = fneg float %28
%31 = tail call float @llvm.fmuladd.f32(float %30, float %4, float %29)
%32 = fptosi float %31 to i32
%33 = icmp slt i32 %32, 1
%34 = select i1 %33, i32 3, i32 -1
br label %35
35: ; preds = %26, %18, %10, %1
%36 = phi i32 [ 0, %1 ], [ 1, %10 ], [ 2, %18 ], [ %34, %26 ]
ret i32 %36
}