#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 timezone {
  int dummy;
};
struct timeval {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
unsigned int RAND_MAX;
unsigned int getpid();
scalar_t__ gettimeofday(struct timeval *, struct timezone *);
int /*<<< orphan*/ srand(unsigned int);
int ubiutils_srand(void) {
  struct timeval tv;
  struct timezone tz;
  unsigned int seed;
  if (gettimeofday(&tv, &tz))
    return -1;
  seed = (unsigned int)tv.tv_sec;
  seed += (unsigned int)tv.tv_usec;
  seed *= getpid();
  seed %= RAND_MAX;
  srand(seed);
  return 0;
}


x86 assembly (clang, -O0):
<ubiutils_srand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <gettimeofday@plt>
cmp    $0x0,%rax
je     115b <ubiutils_srand+0x2b>
movl   $0xffffffff,-0x4(%rbp)
jmp    119c <ubiutils_srand+0x6c>
mov    -0x10(%rbp),%rax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
imul   -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%eax
xor    %edx,%edx
div    %ecx
mov    %edx,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <srand@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ubiutils_srand>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
lea    0x8(%rsp),%rdi
mov    %rsp,%rsi
call   1040 <gettimeofday@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    1173 <ubiutils_srand+0x43>
mov    0x8(%rsp),%ebp
add    0x10(%rsp),%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
imul   %ebp,%eax
mov    0x2e80(%rip),%rcx
xor    %edx,%edx
divl   (%rcx)
mov    %edx,%edi
call   1050 <srand@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
%struct.timezone = type { i32 }
@RAND_MAX = global i32 0
define i32 @ubiutils_srand() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca %struct.timezone
%4 = alloca i32
%5 = call i64 @gettimeofday(%struct.timeval* %2, %struct.timezone* %3)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 -1, i32* %1
br label %25
8: ; preds = %0
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
%10 = load i64, i64* %9
%11 = trunc i64 %10 to i32
store i32 %11, i32* %4
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%13 = load i64, i64* %12
%14 = trunc i64 %13 to i32
%15 = load i32, i32* %4
%16 = add i32 %15, %14
store i32 %16, i32* %4
%17 = call i32 (...) @getpid()
%18 = load i32, i32* %4
%19 = mul i32 %18, %17
store i32 %19, i32* %4
%20 = load i32, i32* @RAND_MAX
%21 = load i32, i32* %4
%22 = urem i32 %21, %20
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = call i32 @srand(i32 %23)
store i32 0, i32* %1
br label %25
25: ; preds = %8, %7
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
%struct.timezone = type { i32 }
@RAND_MAX = global i32 0
define i32 @ubiutils_srand() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timezone
%3 = bitcast %struct.timeval* %1 to i8*
%4 = bitcast %struct.timezone* %2 to i8*
%5 = call i64 @gettimeofday(%struct.timeval* %1, %struct.timezone* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %20
7: ; preds = %0
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%9 = load i64, i64* %8
%10 = trunc i64 %9 to i32
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%12 = load i64, i64* %11
%13 = trunc i64 %12 to i32
%14 = add i32 %13, %10
%15 = tail call i32 (...) @getpid()
%16 = mul i32 %14, %15
%17 = load i32, i32* @RAND_MAX
%18 = urem i32 %16, %17
%19 = tail call i32 @srand(i32 %18)
br label %20
20: ; preds = %0, %7
%21 = phi i32 [ 0, %7 ], [ -1, %0 ]
ret i32 %21
}