#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_usec;
};

/* Variables and functions */
scalar_t__ RAND_MAX;
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
double rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int generate_random_address(int limit) {
  int result;
  struct timeval reference_time;
  gettimeofday(&reference_time, NULL);
  srand(reference_time.tv_usec);
  result = (int)((int)((rand() / (double)RAND_MAX) * limit) / 512) * 512;
  return (result);
}


x86 assembly (clang, -O0):
<generate_random_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x10(%rbp),%edi
call   1040 <srand@plt>
mov    $0x0,%al
call   1050 <rand@plt>
mov    0x2e8a(%rip),%rax
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
cvtsi2sdl -0x4(%rbp),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    $0x200,%ecx
cltd
idiv   %ecx
shl    $0x9,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_random_address>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x8(%rsp),%edi
call   1040 <srand@plt>
xor    %eax,%eax
call   1050 <rand@plt>
mov    0x2e8e(%rip),%rax
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %ebx,%xmm1
mulsd  %xmm0,%xmm1
cvttsd2si %xmm1,%ecx
lea    0x1ff(%rcx),%eax
test   %ecx,%ecx
cmovns %ecx,%eax
and    $0xfffffe00,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@RAND_MAX = global i64 0
define i32 @generate_random_address(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
store i32 %0, i32* %2
%5 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%7 = load i32, i32* %6
%8 = call i32 @srand(i32 %7)
%9 = call double (...) @rand()
%10 = load i64, i64* @RAND_MAX
%11 = sitofp i64 %10 to double
%12 = fdiv double %9, %11
%13 = load i32, i32* %2
%14 = sitofp i32 %13 to double
%15 = fmul double %12, %14
%16 = fptosi double %15 to i32
%17 = sdiv i32 %16, 512
%18 = mul nsw i32 %17, 512
store i32 %18, i32* %3
%19 = load i32, i32* %3
ret i32 %19
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@RAND_MAX = global i64 0
define i32 @generate_random_address(i32 %0) {
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %2 to i8*
%4 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%6 = load i32, i32* %5
%7 = tail call i32 @srand(i32 %6)
%8 = tail call double (...) @rand()
%9 = load i64, i64* @RAND_MAX
%10 = sitofp i64 %9 to double
%11 = fdiv double %8, %10
%12 = sitofp i32 %0 to double
%13 = fmul double %11, %12
%14 = fptosi double %13 to i32
%15 = sdiv i32 %14, 512
%16 = shl nsw i32 %15, 9
ret i32 %16
}