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

/* Variables and functions */
int getpid();
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int rand();
int /*<<< orphan*/ srand(unsigned int);
unsigned short rand16(void) {
  static int been_seeded = 0;
  if (!been_seeded) {
    int fd, n = 0;
    unsigned int c = 0, seed = 0;
    char sbuf[sizeof(seed)];
    char *s;
    struct timeval now;
    gettimeofday(&now, NULL);
    seed = now.tv_sec ^ now.tv_usec ^ (getpid() << 16);
    srand(seed);
    been_seeded = 1;
  }
  return ((unsigned short)(rand() >> 15));
}


x86 assembly (clang, -O0):
<rand16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
cmpl   $0x0,0x2ef5(%rip)
jne    11a6 <rand16+0x66>
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x28(%rbp),%eax
xor    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
shl    $0x10,%ecx
xor    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <srand@plt>
movl   $0x1,0x2e9e(%rip)
mov    $0x0,%al
call   1060 <rand@plt>
sar    $0xf,%eax
movzwl %ax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand16>:
push   %rbx
sub    $0x10,%rsp
cmpb   $0x0,0x2ef8(%rip)
jne    117c <rand16+0x3c>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    0xc(%rsp),%ebx
xor    0x8(%rsp),%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
shl    $0x10,%eax
xor    %eax,%ebx
mov    %ebx,%edi
call   1050 <srand@plt>
movb   $0x1,0x2ec8(%rip)
xor    %eax,%eax
call   1060 <rand@plt>
shr    $0xf,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@rand16.been_seeded = internal global i32 0
define zeroext i16 @rand16() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [4 x i8]
%6 = alloca i8*
%7 = alloca %struct.timeval
%8 = load i32, i32* @rand16.been_seeded
%9 = icmp ne i32 %8, 0
br i1 %9, label %22, label %10
10: ; preds = %0
store i32 0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
%11 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%15 = load i32, i32* %14
%16 = xor i32 %13, %15
%17 = call i32 (...) @getpid()
%18 = shl i32 %17, 16
%19 = xor i32 %16, %18
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = call i32 @srand(i32 %20)
store i32 1, i32* @rand16.been_seeded
br label %22
22: ; preds = %10, %0
%23 = call i32 (...) @rand()
%24 = ashr i32 %23, 15
%25 = trunc i32 %24 to i16
ret i16 %25
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@rand16.been_seeded = internal unnamed_addr global i1 false
define zeroext i16 @rand16() {
%1 = alloca %struct.timeval
%2 = load i1, i1* @rand16.been_seeded
br i1 %2, label %15, label %3
3: ; preds = %0
%4 = bitcast %struct.timeval* %1 to i8*
%5 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%7 = load i32, i32* %6
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%9 = load i32, i32* %8
%10 = xor i32 %9, %7
%11 = tail call i32 (...) @getpid()
%12 = shl i32 %11, 16
%13 = xor i32 %10, %12
%14 = tail call i32 @srand(i32 %13)
store i1 true, i1* @rand16.been_seeded
br label %15
15: ; preds = %3, %0
%16 = tail call i32 (...) @rand()
%17 = lshr i32 %16, 15
%18 = trunc i32 %17 to i16
ret i16 %18
}