#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 scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, unsigned char *, size_t);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, unsigned char *, size_t);
int lidirrypt_salt_gen(uint8_t *salt, size_t len) {
  unsigned char buf[len];
  size_t data_read = 0;
  int urandom = open("/dev/urandom", O_RDONLY);
  if (urandom < 0) {
    return -1;
  }
  while (data_read < len) {
    ssize_t result = read(urandom, buf + data_read, len - data_read);
    if (result < 0) {
      if (errno == EINTR || errno == EAGAIN) {
        continue;
      } else {
        (void)close(urandom);
        return -1;
      }
    }
    data_read += result;
  }
  (void)close(urandom);
  memcpy(salt, buf, len);
  return 0;
}


x86 assembly (clang, -O0):
<lidirrypt_salt_gen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11b4 <lidirrypt_salt_gen+0x74>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    127a <lidirrypt_salt_gen+0x13a>
jmp    11b9 <lidirrypt_salt_gen+0x79>
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1253 <lidirrypt_salt_gen+0x113>
mov    -0x48(%rbp),%rsi
mov    -0x34(%rbp),%edi
add    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x30(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jge    1242 <lidirrypt_salt_gen+0x102>
mov    0x2de3(%rip),%rax
mov    (%rax),%rax
mov    0x2dd1(%rip),%rcx
cmp    (%rcx),%rax
je     1222 <lidirrypt_salt_gen+0xe2>
mov    0x2dc9(%rip),%rax
mov    (%rax),%rax
mov    0x2da7(%rip),%rcx
cmp    (%rcx),%rax
jne    1227 <lidirrypt_salt_gen+0xe7>
jmp    11b9 <lidirrypt_salt_gen+0x79>
mov    -0x34(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    127a <lidirrypt_salt_gen+0x13a>
mov    -0x40(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11b9 <lidirrypt_salt_gen+0x79>
mov    -0x34(%rbp),%edi
call   1030 <close@plt>
mov    -0x48(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lidirrypt_salt_gen>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsp,%r15
lea    0xf(%rsi),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0xe8b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%r13d
mov    $0xffffffff,%eax
test   %r13d,%r13d
js     1203 <lidirrypt_salt_gen+0xc3>
mov    %r14,-0x30(%rbp)
test   %rbx,%rbx
je     11ea <lidirrypt_salt_gen+0xaa>
xor    %r14d,%r14d
mov    0x2e36(%rip),%r12
jmp    11a8 <lidirrypt_salt_gen+0x68>
nopl   0x0(%rax)
add    %rax,%r14
cmp    %rbx,%r14
jae    11ea <lidirrypt_salt_gen+0xaa>
lea    (%r15,%r14,1),%rsi
mov    %rbx,%rdx
sub    %r14,%rdx
mov    %r13d,%edi
call   1040 <read@plt>
test   %rax,%rax
jns    11a0 <lidirrypt_salt_gen+0x60>
mov    0x2e12(%rip),%rax
mov    (%rax),%rax
cmp    (%r12),%rax
je     11a3 <lidirrypt_salt_gen+0x63>
mov    0x2dea(%rip),%rcx
cmp    (%rcx),%rax
je     11a3 <lidirrypt_salt_gen+0x63>
mov    %r13d,%edi
call   1030 <close@plt>
mov    $0xffffffff,%eax
jmp    1203 <lidirrypt_salt_gen+0xc3>
mov    %r13d,%edi
call   1030 <close@plt>
mov    -0x30(%rbp),%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
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
@EAGAIN = global i64 0
define i32 @lidirrypt_salt_gen(i32* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
store i32* %0, i32** %4
store i64 %1, i64* %5
%12 = load i64, i64* %5
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
store i64 0, i64* %8
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 -1, i32* %3
store i32 1, i32* %10
br label %57
20: ; preds = %2
br label %21
21: ; preds = %47, %43, %20
%22 = load i64, i64* %8
%23 = load i64, i64* %5
%24 = icmp ult i64 %22, %23
br i1 %24, label %25, label %51
25: ; preds = %21
%26 = load i32, i32* %9
%27 = load i64, i64* %8
%28 = getelementptr inbounds i8, i8* %14, i64 %27
%29 = load i64, i64* %5
%30 = load i64, i64* %8
%31 = sub i64 %29, %30
%32 = call i64 @read(i32 %26, i8* %28, i64 %31)
store i64 %32, i64* %11
%33 = load i64, i64* %11
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %47
35: ; preds = %25
%36 = load i64, i64* @errno
%37 = load i64, i64* @EINTR
%38 = icmp eq i64 %36, %37
br i1 %38, label %43, label %39
39: ; preds = %35
%40 = load i64, i64* @errno
%41 = load i64, i64* @EAGAIN
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %44
43: ; preds = %39, %35
br label %21
44: ; preds = %39
%45 = load i32, i32* %9
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %3
store i32 1, i32* %10
br label %57
47: ; preds = %25
%48 = load i64, i64* %11
%49 = load i64, i64* %8
%50 = add i64 %49, %48
store i64 %50, i64* %8
br label %21
51: ; preds = %21
%52 = load i32, i32* %9
%53 = call i32 @close(i32 %52)
%54 = load i32*, i32** %4
%55 = load i64, i64* %5
%56 = call i32 @memcpy(i32* %54, i8* %14, i64 %55)
store i32 0, i32* %3
store i32 1, i32* %10
br label %57
57: ; preds = %51, %44, %19
%58 = load i8*, i8** %6
%59 = load i32, i32* %3
ret i32 %59
}

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