#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*/ uint8_t;
typedef int ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ O_RDONLY;
int RAND_bytes(int /*<<< orphan*/ *, int);
int close(int);
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
int read(int, int /*<<< orphan*/ *, size_t);
int getsalt(uint8_t salt[], size_t saltlen) {
  int fd;
  ssize_t lenread;
  if ((fd = open("/dev/urandom", O_RDONLY)) == -1)
    goto err0;
  while (saltlen > 0) {
    if ((lenread = read(fd, salt, saltlen)) == -1)
      goto err1;
    if (lenread == 0)
      goto err1;
    salt += lenread;
    saltlen -= lenread;
  }
  while (close(fd) == -1) {
    if (errno != EINTR)
      goto err0;
  }
  return (0);
err1:
  close(fd);
err0:
  if (RAND_bytes(salt, (int)saltlen) != 1) {
    return (4);
  } else {
    return (0);
  }
}


x86 assembly (clang, -O0):
<getsalt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
lea    0xea0(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1176 <getsalt+0x36>
jmp    122d <getsalt+0xed>
jmp    117b <getsalt+0x3b>
cmpq   $0x0,-0x18(%rbp)
jbe    11df <getsalt+0x9f>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    11a7 <getsalt+0x67>
jmp    1225 <getsalt+0xe5>
cmpl   $0x0,-0x20(%rbp)
jne    11b6 <getsalt+0x76>
jmp    1225 <getsalt+0xe5>
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movslq -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    117b <getsalt+0x3b>
jmp    11e4 <getsalt+0xa4>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1219 <getsalt+0xd9>
mov    0x2ddc(%rip),%rax
mov    (%rax),%rax
mov    0x2dca(%rip),%rcx
cmp    (%rcx),%rax
je     1214 <getsalt+0xd4>
jmp    122d <getsalt+0xed>
jmp    11e4 <getsalt+0xa4>
movl   $0x0,-0x4(%rbp)
jmp    1258 <getsalt+0x118>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    %eax,%esi
call   1030 <RAND_bytes@plt>
cmp    $0x1,%eax
je     1251 <getsalt+0x111>
movl   $0x4,-0x4(%rbp)
jmp    1258 <getsalt+0x118>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getsalt>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
lea    0xea2(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11be <getsalt+0x7e>
mov    %eax,%ebp
test   %rbx,%rbx
je     1190 <getsalt+0x50>
nop
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <read@plt>
lea    0x1(%rax),%ecx
cmp    $0x2,%ecx
jb     11b7 <getsalt+0x77>
cltq
lea    (%r14,%rax,4),%r14
sub    %rax,%rbx
jne    1170 <getsalt+0x30>
mov    0x2e41(%rip),%r15
mov    0x2e32(%rip),%r12
xor    %ebx,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    11d3 <getsalt+0x93>
mov    (%r15),%rax
cmp    (%r12),%rax
je     11a0 <getsalt+0x60>
jmp    11be <getsalt+0x7e>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   1030 <RAND_bytes@plt>
xor    %ebx,%ebx
cmp    $0x1,%eax
setne  %bl
shl    $0x2,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @getsalt(i32* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %4
store i64 %1, i64* %5
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %6
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %2
br label %51
12: ; preds = %2
br label %13
13: ; preds = %27, %12
%14 = load i64, i64* %5
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %36
16: ; preds = %13
%17 = load i32, i32* %6
%18 = load i32*, i32** %4
%19 = load i64, i64* %5
%20 = call i32 @read(i32 %17, i32* %18, i64 %19)
store i32 %20, i32* %7
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %16
br label %48
23: ; preds = %16
%24 = load i32, i32* %7
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %23
br label %48
27: ; preds = %23
%28 = load i32, i32* %7
%29 = load i32*, i32** %4
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds i32, i32* %29, i64 %30
store i32* %31, i32** %4
%32 = load i32, i32* %7
%33 = sext i32 %32 to i64
%34 = load i64, i64* %5
%35 = sub i64 %34, %33
store i64 %35, i64* %5
br label %13
36: ; preds = %13
br label %37
37: ; preds = %46, %36
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %47
41: ; preds = %37
%42 = load i64, i64* @errno
%43 = load i64, i64* @EINTR
%44 = icmp ne i64 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %41
br label %51
46: ; preds = %41
br label %37
47: ; preds = %37
store i32 0, i32* %3
br label %59
48: ; preds = %26, %22
%49 = load i32, i32* %6
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %48, %45, %11
%52 = load i32*, i32** %4
%53 = load i64, i64* %5
%54 = trunc i64 %53 to i32
%55 = call i32 @RAND_bytes(i32* %52, i32 %54)
%56 = icmp ne i32 %55, 1
br i1 %56, label %57, label %58
57: ; preds = %51
store i32 4, i32* %3
br label %59
58: ; preds = %51
store i32 0, i32* %3
br label %59
59: ; preds = %58, %57, %47
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @getsalt(i32* %0, i64 %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %30, label %6
6: ; preds = %2
%7 = icmp eq i64 %1, 0
br i1 %7, label %8, label %10
8: ; preds = %16, %6
%9 = phi i32* [ %0, %6 ], [ %18, %16 ]
br label %21
10: ; preds = %6, %16
%11 = phi i32* [ %18, %16 ], [ %0, %6 ]
%12 = phi i64 [ %19, %16 ], [ %1, %6 ]
%13 = tail call i32 @read(i32 %4, i32* %11, i64 %12)
%14 = add i32 %13, 1
%15 = icmp ult i32 %14, 2
br i1 %15, label %28, label %16
16: ; preds = %10
%17 = sext i32 %13 to i64
%18 = getelementptr inbounds i32, i32* %11, i64 %17
%19 = sub i64 %12, %17
%20 = icmp eq i64 %19, 0
br i1 %20, label %8, label %10
21: ; preds = %8, %24
%22 = tail call i32 @close(i32 %4)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %37
24: ; preds = %21
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br i1 %27, label %21, label %30
28: ; preds = %10
%29 = tail call i32 @close(i32 %4)
br label %30
30: ; preds = %24, %2, %28
%31 = phi i64 [ %1, %2 ], [ %12, %28 ], [ 0, %24 ]
%32 = phi i32* [ %0, %2 ], [ %11, %28 ], [ %9, %24 ]
%33 = trunc i64 %31 to i32
%34 = tail call i32 @RAND_bytes(i32* %32, i32 %33)
%35 = icmp eq i32 %34, 1
%36 = select i1 %35, i32 0, i32 4
br label %37
37: ; preds = %21, %30
%38 = phi i32 [ %36, %30 ], [ 0, %21 ]
ret i32 %38
}