#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;
int /*<<< orphan*/ luaL_argerror(int, char *);
scalar_t__ 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;
  int l = ((int)luaL_opt_number(1, 0));
  if (l == 0)
    lua_pushnumber(r);
  else {
    int u = ((int)luaL_opt_number(2, 0));
    if (u == 0) {
      u = l;
      l = 1;
    }
    if (!(l <= u))
      luaL_argerror(1, "interval is empty");
    lua_pushnumber((int)(r * (u - l + 1)) + l);
  }
}


x86 assembly (clang, -O0):
<math_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1060 <rand@plt>
mov    0x2e92(%rip),%rcx
cltd
idivl  (%rcx)
cvtsi2sd %edx,%xmm0
mov    0x2e84(%rip),%rax
cvtsi2sdl (%rax),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
mov    $0x1,%edi
xor    %esi,%esi
call   1030 <luaL_opt_number@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1199 <math_random+0x59>
cvttsd2si -0x8(%rbp),%edi
call   1050 <lua_pushnumber@plt>
jmp    11fe <math_random+0xbe>
mov    $0x2,%edi
xor    %esi,%esi
call   1030 <luaL_opt_number@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    11bf <math_random+0x7f>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
movl   $0x1,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    11dc <math_random+0x9c>
mov    $0x1,%edi
lea    0xe29(%rip),%rsi
call   1040 <luaL_argerror@plt>
movsd  -0x8(%rbp),%xmm0
mov    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
add    $0x1,%eax
cvtsi2sd %eax,%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
add    -0xc(%rbp),%edi
call   1050 <lua_pushnumber@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<math_random>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1060 <rand@plt>
mov    0x2e92(%rip),%rcx
mov    (%rcx),%ecx
cltd
idiv   %ecx
cvtsi2sd %edx,%xmm1
cvtsi2sd %ecx,%xmm0
divsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
mov    $0x1,%r14d
mov    $0x1,%edi
xor    %esi,%esi
call   1030 <luaL_opt_number@plt>
mov    %rax,%rbp
test   %ebp,%ebp
je     11d1 <math_random+0x91>
mov    $0x2,%edi
xor    %esi,%esi
call   1030 <luaL_opt_number@plt>
mov    %rax,%rbx
test   %ebx,%ebx
cmovne %ebp,%r14d
cmove  %ebp,%ebx
sub    %r14d,%ebx
jge    11b4 <math_random+0x74>
lea    0xe56(%rip),%rsi
mov    $0x1,%edi
call   1040 <luaL_argerror@plt>
add    $0x1,%ebx
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
movsd  0x8(%rsp),%xmm1
mulsd  %xmm0,%xmm1
cvttsd2si %xmm1,%eax
add    %eax,%r14d
jmp    11d8 <math_random+0x98>
cvttsd2si 0x8(%rsp),%r14d
mov    %r14d,%edi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <lua_pushnumber@plt>

LLVM IR (-O0):
@RAND_MAX = global i32 0
@.str = [18 x i8] c"interval is empty\00"
define void @math_random() {
%1 = alloca double
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @rand()
%5 = load i32, i32* @RAND_MAX
%6 = srem i32 %4, %5
%7 = sitofp i32 %6 to double
%8 = load i32, i32* @RAND_MAX
%9 = sitofp i32 %8 to double
%10 = fdiv double %7, %9
store double %10, double* %1
%11 = call i64 @luaL_opt_number(i32 1, i32 0)
%12 = trunc i64 %11 to i32
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %0
%16 = load double, double* %1
%17 = fptosi double %16 to i32
%18 = call i32 @lua_pushnumber(i32 %17)
br label %44
19: ; preds = %0
%20 = call i64 @luaL_opt_number(i32 2, i32 0)
%21 = trunc i64 %20 to i32
store i32 %21, i32* %3
%22 = load i32, i32* %3
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = load i32, i32* %2
store i32 %25, i32* %3
store i32 1, i32* %2
br label %26
26: ; preds = %24, %19
%27 = load i32, i32* %2
%28 = load i32, i32* %3
%29 = icmp sle i32 %27, %28
br i1 %29, label %32, label %30
30: ; preds = %26
%31 = call i32 @luaL_argerror(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
%33 = load double, double* %1
%34 = load i32, i32* %3
%35 = load i32, i32* %2
%36 = sub nsw i32 %34, %35
%37 = add nsw i32 %36, 1
%38 = sitofp i32 %37 to double
%39 = fmul double %33, %38
%40 = fptosi double %39 to i32
%41 = load i32, i32* %2
%42 = add nsw i32 %40, %41
%43 = call i32 @lua_pushnumber(i32 %42)
br label %44
44: ; preds = %32, %15
ret void
}

LLVM IR (-O2):
@RAND_MAX = global i32 0
@.str = [18 x i8] c"interval is empty\00"
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 i64 @luaL_opt_number(i32 1, i32 0)
%8 = trunc i64 %7 to i32
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = fptosi double %6 to i32
br label %28
12: ; preds = %0
%13 = tail call i64 @luaL_opt_number(i32 2, i32 0)
%14 = trunc i64 %13 to i32
%15 = icmp eq i32 %14, 0
%16 = select i1 %15, i32 1, i32 %8
%17 = select i1 %15, i32 %8, i32 %14
%18 = icmp slt i32 %17, %16
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = tail call i32 @luaL_argerror(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %12
%22 = sub i32 1, %16
%23 = add i32 %22, %17
%24 = sitofp i32 %23 to double
%25 = fmul double %6, %24
%26 = fptosi double %25 to i32
%27 = add nsw i32 %16, %26
br label %28
28: ; preds = %21, %10
%29 = phi i32 [ %27, %21 ], [ %11, %10 ]
%30 = tail call i32 @lua_pushnumber(i32 %29)
ret void
}