#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 uint32_t;

/* Variables and functions */
int os_random();
int os_get_random(unsigned char *buf, size_t len) {
  uint32_t rnd = 0;
  for (size_t i = 0; i < len; ++i) {
    if (rnd == 0) {
      rnd = os_random();
    }
    *buf++ = rnd & 0xff;
    rnd >>= 8;
  }
  return len;
}


x86 assembly (clang, -O0):
<os_get_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1186 <os_get_random+0x76>
cmpl   $0x0,-0x14(%rbp)
jne    1151 <os_get_random+0x41>
mov    $0x0,%al
call   1030 <os_random@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x14(%rbp),%eax
sar    $0x8,%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    112f <os_get_random+0x1f>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_get_random>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
test   %rsi,%rsi
je     114d <os_get_random+0x3d>
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
jmp    1140 <os_get_random+0x30>
cs nopw 0x0(%rax,%rax,1)
mov    %al,(%r14,%rbx,1)
sar    $0x8,%eax
add    $0x1,%rbx
cmp    %rbx,%r15
je     114d <os_get_random+0x3d>
test   %eax,%eax
jne    1130 <os_get_random+0x20>
xor    %eax,%eax
call   1030 <os_random@plt>
jmp    1130 <os_get_random+0x20>
mov    %r15d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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