#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 */

/* Variables and functions */
int errno;
int get_urandom_fd();
int read(int, void *, size_t);
int u1db__random_bytes(void *buf, size_t count) {
  int fd, n;
  fd = get_urandom_fd();
  if (fd == -1) {
    return errno;
  }
  n = read(fd, buf, count);
  if (n < count) {
    return errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<u1db__random_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1030 <get_urandom_fd@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1155 <u1db__random_bytes+0x35>
mov    0x2e95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    118e <u1db__random_bytes+0x6e>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
movslq -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1187 <u1db__random_bytes+0x67>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    118e <u1db__random_bytes+0x6e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<u1db__random_bytes>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <get_urandom_fd@plt>
cmp    $0xffffffff,%eax
je     114c <u1db__random_bytes+0x2c>
mov    %eax,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <read@plt>
cltq
cmp    %rbx,%rax
jae    1155 <u1db__random_bytes+0x35>
mov    0x2e8d(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
define i32 @u1db__random_bytes(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = call i32 (...) @get_urandom_fd()
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i32, i32* @errno
store i32 %12, i32* %3
br label %25
13: ; preds = %2
%14 = load i32, i32* %6
%15 = load i8*, i8** %4
%16 = load i64, i64* %5
%17 = call i32 @read(i32 %14, i8* %15, i64 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = load i64, i64* %5
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %13
%23 = load i32, i32* @errno
store i32 %23, i32* %3
br label %25
24: ; preds = %13
store i32 0, i32* %3
br label %25
25: ; preds = %24, %22, %11
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@errno = global i32 0
define i32 @u1db__random_bytes(i8* nocapture %0, i64 %1) {
%3 = tail call i32 (...) @get_urandom_fd()
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = load i32, i32* @errno
br label %13
7: ; preds = %2
%8 = tail call i32 @read(i32 %3, i8* %0, i64 %1)
%9 = sext i32 %8 to i64
%10 = icmp ult i64 %9, %1
%11 = load i32, i32* @errno
%12 = select i1 %10, i32 %11, i32 0
br label %13
13: ; preds = %7, %5
%14 = phi i32 [ %6, %5 ], [ %12, %7 ]
ret i32 %14
}