#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 {
  unsigned int tv_sec;
  unsigned int tv_usec;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ seed;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int _error(char *, int);
int /*<<< orphan*/ close(int);
int getgid();
int getpid();
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int getuid();
int open(char *, int /*<<< orphan*/);
int read(int, unsigned int *, int);
int /*<<< orphan*/ srand(unsigned int);
int _makepasswd_seed(void) {
  struct timeval tv;
  int fd;
  unsigned int seed;
  ssize_t res;
  if (gettimeofday(&tv, NULL) != 0)
    return -_error("gettimeofday", 1);
  if ((fd = open("/dev/random", O_RDONLY)) < 0)
    return -_error("/dev/random", 1);
  res = read(fd, &seed, sizeof(seed));
  close(fd);
  if (res != sizeof(seed))
    return -_error("/dev/random", 1);
  seed ^= tv.tv_sec ^ tv.tv_usec;
  seed ^= (getuid() << 16) ^ getgid();
  seed ^= (getpid() << 16);
  srand(seed);
  return 0;
}


x86 assembly (clang, -O0):
<_makepasswd_seed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0x0,%rax
je     11ce <_makepasswd_seed+0x3e>
lea    0xe4a(%rip),%rdi
mov    $0x1,%esi
call   10a0 <_error@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    12a3 <_makepasswd_seed+0x113>
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
lea    0xe2f(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    120e <_makepasswd_seed+0x7e>
lea    0xe17(%rip),%rdi
mov    $0x1,%esi
call   10a0 <_error@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    12a3 <_makepasswd_seed+0x113>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movslq -0x1c(%rbp),%rax
cmp    $0x4,%rax
je     1257 <_makepasswd_seed+0xc7>
lea    0xdce(%rip),%rdi
mov    $0x1,%esi
call   10a0 <_error@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    12a3 <_makepasswd_seed+0x113>
mov    -0x10(%rbp),%eax
xor    -0xc(%rbp),%eax
xor    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
shl    $0x10,%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1090 <getgid@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
xor    %ecx,%eax
xor    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
shl    $0x10,%eax
xor    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1080 <srand@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_makepasswd_seed>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
test   %rax,%rax
je     11b2 <_makepasswd_seed+0x22>
lea    0xe50(%rip),%rdi
jmp    1231 <_makepasswd_seed+0xa1>
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
lea    0xe4b(%rip),%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     122a <_makepasswd_seed+0x9a>
mov    %eax,%ebx
lea    0x4(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1070 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1060 <close@plt>
cmp    $0x4,%ebp
jne    122a <_makepasswd_seed+0x9a>
mov    0xc(%rsp),%r14d
xor    0x8(%rsp),%r14d
xor    0x4(%rsp),%r14d
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %eax,%ebp
shl    $0x10,%ebp
xor    %eax,%eax
call   1090 <getgid@plt>
xor    %eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
shl    $0x10,%eax
xor    %eax,%ebp
xor    %r14d,%ebp
mov    %ebp,%edi
call   1080 <srand@plt>
jmp    123f <_makepasswd_seed+0xaf>
lea    0xddc(%rip),%rdi
mov    $0x1,%esi
call   10a0 <_error@plt>
mov    %eax,%ebx
neg    %ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [13 x i8] c"gettimeofday\00"
@.str.1 = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
define i32 @_makepasswd_seed() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = call i64 @gettimeofday(%struct.timeval* %2, i32* null)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @_error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 1)
%10 = sub nsw i32 0, %9
store i32 %10, i32* %1
br label %49
11: ; preds = %0
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %12)
store i32 %13, i32* %3
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = call i32 @_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 1)
%17 = sub nsw i32 0, %16
store i32 %17, i32* %1
br label %49
18: ; preds = %11
%19 = load i32, i32* %3
%20 = call i32 @read(i32 %19, i32* %4, i32 4)
store i32 %20, i32* %5
%21 = load i32, i32* %3
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = icmp ne i64 %24, 4
br i1 %25, label %26, label %29
26: ; preds = %18
%27 = call i32 @_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 1)
%28 = sub nsw i32 0, %27
store i32 %28, i32* %1
br label %49
29: ; preds = %18
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
%33 = load i32, i32* %32
%34 = xor i32 %31, %33
%35 = load i32, i32* %4
%36 = xor i32 %35, %34
store i32 %36, i32* %4
%37 = call i32 (...) @getuid()
%38 = shl i32 %37, 16
%39 = call i32 (...) @getgid()
%40 = xor i32 %38, %39
%41 = load i32, i32* %4
%42 = xor i32 %41, %40
store i32 %42, i32* %4
%43 = call i32 (...) @getpid()
%44 = shl i32 %43, 16
%45 = load i32, i32* %4
%46 = xor i32 %45, %44
store i32 %46, i32* %4
%47 = load i32, i32* %4
%48 = call i32 @srand(i32 %47)
store i32 0, i32* %1
br label %49
49: ; preds = %29, %26, %15, %8
%50 = load i32, i32* %1
ret i32 %50
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [13 x i8] c"gettimeofday\00"
@.str.1 = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
define i32 @_makepasswd_seed() {
%1 = alloca %struct.timeval
%2 = alloca i32
%3 = bitcast %struct.timeval* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = call i64 @gettimeofday(%struct.timeval* %1, i32* null)
%6 = icmp eq i64 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %0
%8 = tail call i32 @_error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 1)
%9 = sub nsw i32 0, %8
br label %41
10: ; preds = %0
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = tail call i32 @_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 1)
%16 = sub nsw i32 0, %15
br label %41
17: ; preds = %10
%18 = call i32 @read(i32 %12, i32* %2, i32 4)
%19 = tail call i32 @close(i32 %12)
%20 = icmp eq i32 %18, 4
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = tail call i32 @_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 1)
%23 = sub nsw i32 0, %22
br label %41
24: ; preds = %17
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%28 = load i32, i32* %27
%29 = xor i32 %28, %26
%30 = load i32, i32* %2
%31 = xor i32 %29, %30
%32 = tail call i32 (...) @getuid()
%33 = shl i32 %32, 16
%34 = tail call i32 (...) @getgid()
%35 = xor i32 %34, %31
%36 = xor i32 %35, %33
%37 = tail call i32 (...) @getpid()
%38 = shl i32 %37, 16
%39 = xor i32 %38, %36
%40 = tail call i32 @srand(i32 %39)
br label %41
41: ; preds = %24, %21, %14, %7
%42 = phi i32 [ %9, %7 ], [ %16, %14 ], [ %23, %21 ], [ 0, %24 ]
ret i32 %42
}