#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 {
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
float rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int getrand(int max) {
  int j;
  struct timeval tv;
  if (gettimeofday(&tv, NULL) != 0) {
    printf("Error getting time\n");
  }
  srand(tv.tv_sec);
  j = 1 + (int)((float)max * rand() / (23457 + 1.0));
  return j;
}


x86 assembly (clang, -O0):
<getrand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
cmp    $0x0,%rax
je     116e <getrand+0x2e>
lea    0xe9f(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%edi
call   1050 <srand@plt>
cvtsi2ssl -0x4(%rbp),%xmm0
movss  %xmm0,-0x14(%rbp)
mov    $0x0,%al
call   1060 <rand@plt>
movaps %xmm0,%xmm1
movss  -0x14(%rbp),%xmm0
mulss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe61(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getrand>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
test   %rax,%rax
je     1164 <getrand+0x24>
lea    0xea9(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%edi
call   1050 <srand@plt>
cvtsi2ss %ebx,%xmm0
movss  %xmm0,0xc(%rsp)
xor    %eax,%eax
call   1060 <rand@plt>
mulss  0xc(%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
divsd  0xe70(%rip),%xmm0
cvttsd2si %xmm0,%eax
add    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@.str = [20 x i8] c"Error getting time\0A\00"
define i32 @getrand(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
store i32 %0, i32* %2
%5 = call i64 @gettimeofday(%struct.timeval* %4, i32* null)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%11 = load i32, i32* %10
%12 = call i32 @srand(i32 %11)
%13 = load i32, i32* %2
%14 = sitofp i32 %13 to float
%15 = call float (...) @rand()
%16 = fmul float %14, %15
%17 = fpext float %16 to double
%18 = fdiv double %17, 2.345800e+04
%19 = fptosi double %18 to i32
%20 = add nsw i32 1, %19
store i32 %20, i32* %3
%21 = load i32, i32* %3
ret i32 %21
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@str = [19 x i8] c"Error getting time\00"
define i32 @getrand(i32 %0) {
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %2 to i8*
%4 = call i64 @gettimeofday(%struct.timeval* %2, i32* null)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i32 @srand(i32 %10)
%12 = sitofp i32 %0 to float
%13 = tail call float (...) @rand()
%14 = fmul float %13, %12
%15 = fpext float %14 to double
%16 = fdiv double %15, 2.345800e+04
%17 = fptosi double %16 to i32
%18 = add nsw i32 %17, 1
ret i32 %18
}