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

/* 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) {
    unsigned int seed = 0;
    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    $0x20,%rsp
cmpl   $0x0,0x2ef5(%rip)
jne    1198 <rand16+0x58>
movl   $0x0,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x10(%rbp),%eax
xor    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
shl    $0x10,%ecx
xor    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <srand@plt>
movl   $0x1,0x2eac(%rip)
mov    $0x0,%al
call   1060 <rand@plt>
sar    $0xf,%eax
movzwl %ax,%eax
add    $0x20,%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 %struct.timeval
%3 = load i32, i32* @rand16.been_seeded
%4 = icmp ne i32 %3, 0
br i1 %4, label %17, label %5
5: ; preds = %0
store i32 0, i32* %1
%6 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%8 = load i32, i32* %7
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
%10 = load i32, i32* %9
%11 = xor i32 %8, %10
%12 = call i32 (...) @getpid()
%13 = shl i32 %12, 16
%14 = xor i32 %11, %13
store i32 %14, i32* %1
%15 = load i32, i32* %1
%16 = call i32 @srand(i32 %15)
store i32 1, i32* @rand16.been_seeded
br label %17
17: ; preds = %5, %0
%18 = call i32 (...) @rand()
%19 = ashr i32 %18, 15
%20 = trunc i32 %19 to i16
ret i16 %20
}

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
}