#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 */
typedef int /*<<< orphan*/ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, int /*<<< orphan*/ *, int);
int urandom16(uint16_t *r) {
  int urand_fd = open("/dev/urandom", O_RDONLY);
  if (urand_fd == 0) {
    printf("Critical error: couldn't open /dev/urandom.\n");
    return 0;
  }
  read(urand_fd, r, sizeof(uint16_t));
  close(urand_fd);
  return 1;
}


x86 assembly (clang, -O0):
<urandom16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
lea    0xea4(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1186 <urandom16+0x46>
lea    0xe98(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11a6 <urandom16+0x66>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<urandom16>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
lea    0xeaa(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
je     117e <urandom16+0x3e>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0x1,%eax
jmp    118c <urandom16+0x4c>
lea    0xe88(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [45 x i8] c"Critical error: couldn't open /dev/urandom.\0A\00"
define i32 @urandom16(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
store i32* %0, i32** %3
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %17
11: ; preds = %1
%12 = load i32, i32* %4
%13 = load i32*, i32** %3
%14 = call i32 @read(i32 %12, i32* %13, i32 4)
%15 = load i32, i32* %4
%16 = call i32 @close(i32 %15)
store i32 1, i32* %2
br label %17
17: ; preds = %11, %9
%18 = load i32, i32* %2
ret i32 %18
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@str = [44 x i8] c"Critical error: couldn't open /dev/urandom.\00"
define i32 @urandom16(i32* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
br label %10
7: ; preds = %1
%8 = tail call i32 @read(i32 %3, i32* %0, i32 4)
%9 = tail call i32 @close(i32 %3)
br label %10
10: ; preds = %7, %5
%11 = phi i32 [ 0, %5 ], [ 1, %7 ]
ret i32 %11
}