#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 stat {
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ random;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int rand();
int read(int, int *, int);
int /*<<< orphan*/ srand(int);
scalar_t__ stat(char *, struct stat *);
int time(int /*<<< orphan*/ *);
int new_serial(int dev) {
  static int inited = 0;
  if (!inited) {
    struct stat st;
    int fd, random;
    inited = time(NULL);
    if (stat("/dev"
             "/urandom",
             &st) == 0 &&
        S_ISCHR(st.st_mode) &&
        (fd = open("/dev"
                   "/urandom",
                   O_RDONLY)) > 0 &&
        read(fd, &random, sizeof(random)) == sizeof(random)) {
      close(fd);
      inited ^= random;
    }
    srand(inited);
  }
  dev = dev % 271 + 17;
  while (dev--)
    inited = rand();
  return inited;
}


x86 assembly (clang, -O0):
<new_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,0x2ed2(%rip)
jne    1233 <new_serial+0xb3>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,0x2ebd(%rip)
lea    0xe52(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    1228 <new_serial+0xa8>
mov    -0x8(%rbp),%edi
call   1080 <S_ISCHR@plt>
cmp    $0x0,%rax
je     1228 <new_serial+0xa8>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
lea    0xe1d(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    1228 <new_serial+0xa8>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cltq
cmp    $0x4,%rax
jne    1228 <new_serial+0xa8>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
mov    -0x10(%rbp),%eax
xor    0x2e42(%rip),%eax
mov    %eax,0x2e3c(%rip)
mov    0x2e36(%rip),%edi
call   1050 <srand@plt>
mov    -0x4(%rbp),%eax
mov    $0x10f,%ecx
cltd
idiv   %ecx
add    $0x11,%edx
mov    %edx,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x4(%rbp)
cmp    $0x0,%eax
je     126a <new_serial+0xea>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    %eax,0x2dff(%rip)
jmp    1244 <new_serial+0xc4>
mov    0x2df4(%rip),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<new_serial>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
cmpl   $0x0,0x2ed8(%rip)
jne    11ba <new_serial+0x3a>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,0x2ec9(%rip)
lea    0xe5e(%rip),%rdi
mov    %rsp,%rsi
call   1060 <stat@plt>
test   %rax,%rax
je     1201 <new_serial+0x81>
mov    0x2eaf(%rip),%edi
call   1050 <srand@plt>
movslq %ebx,%rbx
imul   $0xf1d48bd,%rbx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x24,%rax
add    %ecx,%eax
imul   $0x10f,%eax,%eax
sub    %eax,%ebx
add    $0x11,%ebx
je     11f4 <new_serial+0x74>
xchg   %ax,%ax
xor    %eax,%eax
call   10a0 <rand@plt>
mov    %eax,0x2e77(%rip)
add    $0xffffffff,%ebx
jne    11e0 <new_serial+0x60>
jmp    11fa <new_serial+0x7a>
mov    0x2e6a(%rip),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    (%rsp),%edi
call   1080 <S_ISCHR@plt>
test   %rax,%rax
je     11af <new_serial+0x2f>
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
lea    0xde2(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
jle    11af <new_serial+0x2f>
mov    %eax,%ebp
lea    0x4(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0x4,%eax
jne    11af <new_serial+0x2f>
mov    %ebp,%edi
call   1030 <close@plt>
mov    0x4(%rsp),%eax
xor    %eax,0x2e10(%rip)
jmp    11af <new_serial+0x2f>

LLVM IR (-O0):
%struct.stat = type { i32 }
@new_serial.inited = internal global i32 0
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define i32 @new_serial(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.stat
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* @new_serial.inited
%7 = icmp ne i32 %6, 0
br i1 %7, label %35, label %8
8: ; preds = %1
%9 = call i32 @time(i32* null)
store i32 %9, i32* @new_serial.inited
%10 = call i64 @stat(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %struct.stat* %3)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %32
12: ; preds = %8
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i64 @S_ISCHR(i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %32
17: ; preds = %12
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %18)
store i32 %19, i32* %4
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %32
21: ; preds = %17
%22 = load i32, i32* %4
%23 = call i32 @read(i32 %22, i32* %5, i32 4)
%24 = sext i32 %23 to i64
%25 = icmp eq i64 %24, 4
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = load i32, i32* %4
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %5
%30 = load i32, i32* @new_serial.inited
%31 = xor i32 %30, %29
store i32 %31, i32* @new_serial.inited
br label %32
32: ; preds = %26, %21, %17, %12, %8
%33 = load i32, i32* @new_serial.inited
%34 = call i32 @srand(i32 %33)
br label %35
35: ; preds = %32, %1
%36 = load i32, i32* %2
%37 = srem i32 %36, 271
%38 = add nsw i32 %37, 17
store i32 %38, i32* %2
br label %39
39: ; preds = %43, %35
%40 = load i32, i32* %2
%41 = add nsw i32 %40, -1
store i32 %41, i32* %2
%42 = icmp ne i32 %40, 0
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 (...) @rand()
store i32 %44, i32* @new_serial.inited
br label %39
45: ; preds = %39
%46 = load i32, i32* @new_serial.inited
ret i32 %46
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@new_serial.inited = internal unnamed_addr global i32 0
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define i32 @new_serial(i32 %0) {
%2 = alloca %struct.stat
%3 = alloca i32
%4 = load i32, i32* @new_serial.inited
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %32
6: ; preds = %1
%7 = bitcast %struct.stat* %2 to i8*
%8 = bitcast i32* %3 to i8*
%9 = tail call i32 @time(i32* null)
store i32 %9, i32* @new_serial.inited
%10 = call i64 @stat(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %29
12: ; preds = %6
%13 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i64 @S_ISCHR(i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %29, label %17
17: ; preds = %12
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %17
%22 = call i32 @read(i32 %19, i32* %3, i32 4)
%23 = icmp eq i32 %22, 4
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = call i32 @close(i32 %19)
%26 = load i32, i32* %3
%27 = load i32, i32* @new_serial.inited
%28 = xor i32 %27, %26
store i32 %28, i32* @new_serial.inited
br label %29
29: ; preds = %24, %21, %17, %12, %6
%30 = load i32, i32* @new_serial.inited
%31 = call i32 @srand(i32 %30)
br label %32
32: ; preds = %29, %1
%33 = srem i32 %0, 271
%34 = add nsw i32 %33, 17
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = load i32, i32* @new_serial.inited
br label %43
38: ; preds = %32, %38
%39 = phi i32 [ %40, %38 ], [ %34, %32 ]
%40 = add nsw i32 %39, -1
%41 = call i32 (...) @rand()
store i32 %41, i32* @new_serial.inited
%42 = icmp eq i32 %40, 0
br i1 %42, label %43, label %38
43: ; preds = %38, %36
%44 = phi i32 [ %37, %36 ], [ %41, %38 ]
ret i32 %44
}