#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 RAND_MAX;
double luaL_opt_number(int, int /*<<< orphan*/);
int /*<<< orphan*/ lua_pushnumber(int);
int rand();
void math_random(void) {
  double r = (double)(rand() % RAND_MAX) / (double)RAND_MAX;
  double l = luaL_opt_number(1, 0);
  if (l == 0)
    lua_pushnumber(r);
  else
    lua_pushnumber((int)(r * l) + 1);
}


x86 assembly (clang, -O0):
<math_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1050 <rand@plt>
mov    0x2ea2(%rip),%rcx
cltd
idivl  (%rcx)
cvtsi2sd %edx,%xmm0
mov    0x2e94(%rip),%rax
cvtsi2sdl (%rax),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
mov    $0x1,%edi
xor    %esi,%esi
call   1030 <luaL_opt_number@plt>
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1199 <math_random+0x69>
jp     1199 <math_random+0x69>
cvttsd2si -0x8(%rbp),%edi
call   1040 <lua_pushnumber@plt>
jmp    11af <math_random+0x7f>
movsd  -0x8(%rbp),%xmm0
mulsd  -0x10(%rbp),%xmm0
cvttsd2si %xmm0,%edi
add    $0x1,%edi
call   1040 <lua_pushnumber@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<math_random>:
push   %rax
xor    %eax,%eax
call   1050 <rand@plt>
mov    0x2ea9(%rip),%rcx
mov    (%rcx),%ecx
cltd
idiv   %ecx
cvtsi2sd %edx,%xmm1
cvtsi2sd %ecx,%xmm0
divsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
mov    $0x1,%edi
xor    %esi,%esi
call   1030 <luaL_opt_number@plt>
movapd %xmm0,%xmm1
movsd  (%rsp),%xmm2
mulsd  %xmm2,%xmm1
cvttsd2si %xmm1,%eax
add    $0x1,%eax
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
cvttsd2si %xmm2,%edi
cmovne %eax,%edi
cmovp  %eax,%edi
pop    %rax
jmp    1040 <lua_pushnumber@plt>

LLVM IR (-O0):
@RAND_MAX = global i32 0
define void @math_random() {
%1 = alloca double
%2 = alloca double
%3 = call i32 (...) @rand()
%4 = load i32, i32* @RAND_MAX
%5 = srem i32 %3, %4
%6 = sitofp i32 %5 to double
%7 = load i32, i32* @RAND_MAX
%8 = sitofp i32 %7 to double
%9 = fdiv double %6, %8
store double %9, double* %1
%10 = call double @luaL_opt_number(i32 1, i32 0)
store double %10, double* %2
%11 = load double, double* %2
%12 = fcmp oeq double %11, 0.000000e+00
br i1 %12, label %13, label %17
13: ; preds = %0
%14 = load double, double* %1
%15 = fptosi double %14 to i32
%16 = call i32 @lua_pushnumber(i32 %15)
br label %24
17: ; preds = %0
%18 = load double, double* %1
%19 = load double, double* %2
%20 = fmul double %18, %19
%21 = fptosi double %20 to i32
%22 = add nsw i32 %21, 1
%23 = call i32 @lua_pushnumber(i32 %22)
br label %24
24: ; preds = %17, %13
ret void
}

LLVM IR (-O2):
@RAND_MAX = global i32 0
define void @math_random() {
%1 = tail call i32 (...) @rand()
%2 = load i32, i32* @RAND_MAX
%3 = srem i32 %1, %2
%4 = sitofp i32 %3 to double
%5 = sitofp i32 %2 to double
%6 = fdiv double %4, %5
%7 = tail call double @luaL_opt_number(i32 1, i32 0)
%8 = fcmp oeq double %7, 0.000000e+00
%9 = fmul double %7, %6
%10 = fptosi double %9 to i32
%11 = add nsw i32 %10, 1
%12 = fptosi double %6 to i32
%13 = select i1 %8, i32 %12, i32 %11
%14 = tail call i32 @lua_pushnumber(i32 %13)
ret void
}