#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 */
struct timeval {
  scalar_t__ tv_usec;
};

/* Variables and functions */
int RAND_MAX;
int gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int rand();
int /*<<< orphan*/ srand(unsigned int);
int genball(int base) {
  struct timeval tv;
  int i, j;
  j = RAND_MAX % base;
  int ret = gettimeofday(&tv, NULL);
  if (ret == -1) {
    perror("gtm failed");
    return -1;
  }
  srand((unsigned int)tv.tv_usec);
  do {
    i = rand();
  } while (i >= (RAND_MAX - j));
  return (i % base + 1);
}


x86 assembly (clang, -O0):
<genball>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e96(%rip),%rax
mov    (%rax),%eax
cltd
idivl  -0x8(%rbp)
mov    %edx,-0x18(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    118d <genball+0x4d>
lea    0xe84(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <genball+0x86>
mov    -0x10(%rbp),%rax
mov    %eax,%edi
call   1040 <srand@plt>
mov    $0x0,%al
call   1060 <rand@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e3c(%rip),%rcx
mov    (%rcx),%ecx
sub    -0x18(%rbp),%ecx
cmp    %ecx,%eax
jge    1198 <genball+0x58>
mov    -0x14(%rbp),%eax
cltd
idivl  -0x8(%rbp)
add    $0x1,%edx
mov    %edx,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<genball>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
mov    0x2e96(%rip),%rbx
mov    (%rbx),%eax
cltd
idiv   %edi
mov    %edx,%ebp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
cmp    $0xffffffff,%eax
je     11a1 <genball+0x61>
mov    0x8(%rsp),%edi
call   1040 <srand@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1060 <rand@plt>
mov    (%rbx),%ecx
sub    %ebp,%ecx
cmp    %ecx,%eax
jge    1180 <genball+0x40>
cltd
idiv   %r14d
mov    %edx,%eax
add    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe58(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    1198 <genball+0x58>

LLVM IR (-O0):
%struct.timeval = type { i64 }
@RAND_MAX = global i32 0
@.str = [11 x i8] c"gtm failed\00"
define i32 @genball(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* @RAND_MAX
%9 = load i32, i32* %3
%10 = srem i32 %8, %9
store i32 %10, i32* %6
%11 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %34
16: ; preds = %1
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%18 = load i64, i64* %17
%19 = trunc i64 %18 to i32
%20 = call i32 @srand(i32 %19)
br label %21
21: ; preds = %23, %16
%22 = call i32 (...) @rand()
store i32 %22, i32* %5
br label %23
23: ; preds = %21
%24 = load i32, i32* %5
%25 = load i32, i32* @RAND_MAX
%26 = load i32, i32* %6
%27 = sub nsw i32 %25, %26
%28 = icmp sge i32 %24, %27
br i1 %28, label %21, label %29
29: ; preds = %23
%30 = load i32, i32* %5
%31 = load i32, i32* %3
%32 = srem i32 %30, %31
%33 = add nsw i32 %32, 1
store i32 %33, i32* %2
br label %34
34: ; preds = %29, %14
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
%struct.timeval = type { i64 }
@RAND_MAX = global i32 0
@.str = [11 x i8] c"gtm failed\00"
define i32 @genball(i32 %0) {
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %2 to i8*
%4 = load i32, i32* @RAND_MAX
%5 = srem i32 %4, %0
%6 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %23
10: ; preds = %1
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%12 = load i64, i64* %11
%13 = trunc i64 %12 to i32
%14 = tail call i32 @srand(i32 %13)
br label %15
15: ; preds = %15, %10
%16 = tail call i32 (...) @rand()
%17 = load i32, i32* @RAND_MAX
%18 = sub nsw i32 %17, %5
%19 = icmp slt i32 %16, %18
br i1 %19, label %20, label %15
20: ; preds = %15
%21 = srem i32 %16, %0
%22 = add nsw i32 %21, 1
br label %23
23: ; preds = %20, %8
%24 = phi i32 [ -1, %8 ], [ %22, %20 ]
ret i32 %24
}