#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 li_rand_device_bytes(unsigned char *, int);
int li_rand_pseudo_bytes();
int li_rand_bytes(unsigned char *buf, int num) {
  if (1 == li_rand_device_bytes(buf, num)) {
    return 1;
  } else {
    for (int i = 0; i < num; ++i)
      buf[i] = li_rand_pseudo_bytes() & 0xFF;
    return 0;
  }
}


x86 assembly (clang, -O0):
<li_rand_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <li_rand_device_bytes@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    1156 <li_rand_bytes+0x36>
movl   $0x1,-0x4(%rbp)
jmp    1197 <li_rand_bytes+0x77>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1190 <li_rand_bytes+0x70>
mov    $0x0,%al
call   1040 <li_rand_pseudo_bytes@plt>
and    $0xff,%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    115d <li_rand_bytes+0x3d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<li_rand_bytes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r15
call   1030 <li_rand_device_bytes@plt>
mov    $0x1,%r14d
cmp    $0x1,%eax
je     1169 <li_rand_bytes+0x49>
test   %ebp,%ebp
jle    1166 <li_rand_bytes+0x46>
mov    %ebp,%ebp
xor    %r14d,%r14d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <li_rand_pseudo_bytes@plt>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1150 <li_rand_bytes+0x30>
jmp    1169 <li_rand_bytes+0x49>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @li_rand_bytes(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = load i32, i32* %5
%9 = call i32 @li_rand_device_bytes(i8* %7, i32 %8)
%10 = icmp eq i32 1, %9
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 1, i32* %3
br label %29
12: ; preds = %2
store i32 0, i32* %6
br label %13
13: ; preds = %25, %12
%14 = load i32, i32* %6
%15 = load i32, i32* %5
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %28
17: ; preds = %13
%18 = call i32 (...) @li_rand_pseudo_bytes()
%19 = and i32 %18, 255
%20 = trunc i32 %19 to i8
%21 = load i8*, i8** %4
%22 = load i32, i32* %6
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
store i8 %20, i8* %24
br label %25
25: ; preds = %17
%26 = load i32, i32* %6
%27 = add nsw i32 %26, 1
store i32 %27, i32* %6
br label %13
28: ; preds = %13
store i32 0, i32* %3
br label %29
29: ; preds = %28, %11
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
define i32 @li_rand_bytes(i8* %0, i32 %1) {
%3 = tail call i32 @li_rand_device_bytes(i8* %0, i32 %1)
%4 = icmp eq i32 %3, 1
br i1 %4, label %16, label %5
5: ; preds = %2
%6 = icmp sgt i32 %1, 0
br i1 %6, label %7, label %16
7: ; preds = %5
%8 = zext i32 %1 to i64
br label %9
9: ; preds = %7, %9
%10 = phi i64 [ 0, %7 ], [ %14, %9 ]
%11 = tail call i32 (...) @li_rand_pseudo_bytes()
%12 = trunc i32 %11 to i8
%13 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 %12, i8* %13
%14 = add nuw nsw i64 %10, 1
%15 = icmp eq i64 %14, %8
br i1 %15, label %16, label %9
16: ; preds = %9, %5, %2
%17 = phi i32 [ 1, %2 ], [ 0, %5 ], [ 0, %9 ]
ret i32 %17
}