#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 rand();
int /*<<< orphan*/ srand(unsigned int);
scalar_t__ time(int /*<<< orphan*/ *);
int random_int(int n) {
  static int initialised = 0;
  double r;
  int ri;
  if (!initialised) {
    srand((unsigned)time(NULL));
    initialised = 1;
  }
  ri = rand();
  r = (double)ri * (double)n / ((double)RAND_MAX + (double)1);
  ri = ((int)r) % n;
  return ri < 0 ? (ri + n) : ri;
}


x86 assembly (clang, -O0):
<random_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,0x2efe(%rip)
jne    1162 <random_int+0x32>
xor    %eax,%eax
mov    %eax,%edi
call   1040 <time@plt>
mov    %eax,%edi
call   1030 <srand@plt>
movl   $0x1,0x2ede(%rip)
mov    $0x0,%al
call   1050 <rand@plt>
mov    %eax,-0x14(%rbp)
cvtsi2sdl -0x14(%rbp),%xmm0
cvtsi2sdl -0x4(%rbp),%xmm1
mulsd  %xmm1,%xmm0
mov    0x2e67(%rip),%rax
cvtsi2sdq (%rax),%xmm1
movsd  0xe72(%rip),%xmm2
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
cvttsd2si -0x10(%rbp),%eax
cltd
idivl  -0x4(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11bf <random_int+0x8f>
mov    -0x14(%rbp),%eax
add    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11c5 <random_int+0x95>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_int>:
push   %rbx
mov    %edi,%ebx
cmpb   $0x0,0x2f06(%rip)
jne    1151 <random_int+0x21>
xor    %edi,%edi
call   1040 <time@plt>
mov    %eax,%edi
call   1030 <srand@plt>
movb   $0x1,0x2eef(%rip)
xor    %eax,%eax
call   1050 <rand@plt>
cvtsi2sd %eax,%xmm0
cvtsi2sd %ebx,%xmm1
mulsd  %xmm0,%xmm1
mov    0x2e7d(%rip),%rax
xorps  %xmm0,%xmm0
cvtsi2sdq (%rax),%xmm0
addsd  0xe85(%rip),%xmm0
divsd  %xmm0,%xmm1
cvttsd2si %xmm1,%eax
cltd
idiv   %ebx
mov    %edx,%eax
sar    $0x1f,%eax
and    %ebx,%eax
add    %edx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@random_int.initialised = internal global i32 0
@RAND_MAX = global i64 0
define i32 @random_int(i32 %0) {
%2 = alloca i32
%3 = alloca double
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* @random_int.initialised
%6 = icmp ne i32 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = call i64 @time(i32* null)
%9 = trunc i64 %8 to i32
%10 = call i32 @srand(i32 %9)
store i32 1, i32* @random_int.initialised
br label %11
11: ; preds = %7, %1
%12 = call i32 (...) @rand()
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = sitofp i32 %13 to double
%15 = load i32, i32* %2
%16 = sitofp i32 %15 to double
%17 = fmul double %14, %16
%18 = load i64, i64* @RAND_MAX
%19 = sitofp i64 %18 to double
%20 = fadd double %19, 1.000000e+00
%21 = fdiv double %17, %20
store double %21, double* %3
%22 = load double, double* %3
%23 = fptosi double %22 to i32
%24 = load i32, i32* %2
%25 = srem i32 %23, %24
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %11
%29 = load i32, i32* %4
%30 = load i32, i32* %2
%31 = add nsw i32 %29, %30
br label %34
32: ; preds = %11
%33 = load i32, i32* %4
br label %34
34: ; preds = %32, %28
%35 = phi i32 [ %31, %28 ], [ %33, %32 ]
ret i32 %35
}

LLVM IR (-O2):
@random_int.initialised = internal unnamed_addr global i1 false
@RAND_MAX = global i64 0
define i32 @random_int(i32 %0) {
%2 = load i1, i1* @random_int.initialised
br i1 %2, label %7, label %3
3: ; preds = %1
%4 = tail call i64 @time(i32* null)
%5 = trunc i64 %4 to i32
%6 = tail call i32 @srand(i32 %5)
store i1 true, i1* @random_int.initialised
br label %7
7: ; preds = %3, %1
%8 = tail call i32 (...) @rand()
%9 = sitofp i32 %8 to double
%10 = sitofp i32 %0 to double
%11 = fmul double %10, %9
%12 = load i64, i64* @RAND_MAX
%13 = sitofp i64 %12 to double
%14 = fadd double %13, 1.000000e+00
%15 = fdiv double %11, %14
%16 = fptosi double %15 to i32
%17 = srem i32 %16, %0
%18 = icmp slt i32 %17, 0
%19 = select i1 %18, i32 %0, i32 0
%20 = add nsw i32 %19, %17
ret i32 %20
}