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

/* Variables and functions */
unsigned char *calloc(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ memset(unsigned char *, int, int);
int /*<<< orphan*/ print_hex(unsigned char *, int);
int sscanf(char *, char *, int *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int len = 32;
  int res;
  unsigned char *buf;
  FILE *rand;
  if (argc >= 2) {
    if (sscanf(argv[1], "%d", &len) != 1) {
      fprintf(stderr, "Error: len must be an integer\n");
      return 1;
    }
  }
  buf = calloc(len, sizeof(char));
  memset(buf, 9, len);
  rand = fopen("/dev/random", "r");
  res = fread(buf, sizeof(char), len, rand);
  if (res != len) {
    fprintf(stderr, "Error reading /dev/random: %d != %d, \n", res, len);
    fclose(rand);
    free(buf);
    return 2;
  }
  fclose(rand);
  print_hex(buf, len);
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x20,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     1200 <main+0x70>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe3a(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   10a0 <sscanf@plt>
cmp    $0x1,%eax
je     11fb <main+0x6b>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bd <main+0x12d>
jmp    1200 <main+0x70>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    $0x9,%esi
call   1060 <memset@plt>
lea    0xdf9(%rip),%rdi
lea    0xdfe(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     1298 <main+0x108>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xdbd(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x2,-0x4(%rbp)
jmp    12bd <main+0x12d>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1090 <print_hex@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
movl   $0x20,0x4(%rsp)
mov    $0x20,%ebx
cmp    $0x2,%edi
jl     11c8 <main+0x38>
mov    0x8(%rsi),%rdi
lea    0xe4f(%rip),%rsi
lea    0x4(%rsp),%rdx
call   10a0 <sscanf@plt>
cmp    $0x1,%eax
jne    1268 <main+0xd8>
mov    0x4(%rsp),%ebx
mov    %ebx,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x9,%esi
mov    %ebx,%edx
call   1060 <memset@plt>
lea    0xe35(%rip),%rdi
lea    0xe3a(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
mov    0x4(%rsp),%edx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rax,%rcx
call   1040 <fread@plt>
mov    0x4(%rsp),%ecx
cmp    %ecx,%eax
jne    1238 <main+0xa8>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x4(%rsp),%esi
mov    %r14,%rdi
call   1090 <print_hex@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1284 <main+0xf4>
mov    0x2da1(%rip),%rdx
mov    (%rdx),%edi
lea    0xde8(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0x2,%eax
jmp    1284 <main+0xf4>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
lea    0xd8b(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"Error: len must be an integer\0A\00"
@.str.2 = [12 x i8] c"/dev/random\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [39 x i8] c"Error reading /dev/random: %d != %d, \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 32, i32* %6
%10 = load i32, i32* %4
%11 = icmp sge i32 %10, 2
br i1 %11, label %12, label %22
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @sscanf(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
%17 = icmp ne i32 %16, 1
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %53
21: ; preds = %12
br label %22
22: ; preds = %21, %2
%23 = load i32, i32* %6
%24 = call i8* @calloc(i32 %23, i32 1)
store i8* %24, i8** %8
%25 = load i8*, i8** %8
%26 = load i32, i32* %6
%27 = call i32 @memset(i8* %25, i32 9, i32 %26)
%28 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %28, i32** %9
%29 = load i8*, i8** %8
%30 = load i32, i32* %6
%31 = load i32*, i32** %9
%32 = call i32 @fread(i8* %29, i32 1, i32 %30, i32* %31)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = load i32, i32* %6
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %45
36: ; preds = %22
%37 = load i32, i32* @stderr
%38 = load i32, i32* %7
%39 = load i32, i32* %6
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 %38, i32 %39)
%41 = load i32*, i32** %9
%42 = call i32 @fclose(i32* %41)
%43 = load i8*, i8** %8
%44 = call i32 @free(i8* %43)
store i32 2, i32* %3
br label %53
45: ; preds = %22
%46 = load i32*, i32** %9
%47 = call i32 @fclose(i32* %46)
%48 = load i8*, i8** %8
%49 = load i32, i32* %6
%50 = call i32 @print_hex(i8* %48, i32 %49)
%51 = load i8*, i8** %8
%52 = call i32 @free(i8* %51)
store i32 0, i32* %3
br label %53
53: ; preds = %45, %36, %18
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"Error: len must be an integer\0A\00"
@.str.2 = [12 x i8] c"/dev/random\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [39 x i8] c"Error reading /dev/random: %d != %d, \0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 32, i32* %3
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %16
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = load i32, i32* %3
br label %16
13: ; preds = %6
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %35
16: ; preds = %11, %2
%17 = phi i32 [ %12, %11 ], [ 32, %2 ]
%18 = call i8* @calloc(i32 %17, i32 1)
%19 = call i32 @memset(i8* %18, i32 9, i32 %17)
%20 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32, i32* %3
%22 = call i32 @fread(i8* %18, i32 1, i32 %21, i32* %20)
%23 = load i32, i32* %3
%24 = icmp eq i32 %22, %23
br i1 %24, label %30, label %25
25: ; preds = %16
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 %22, i32 %23)
%28 = call i32 @fclose(i32* %20)
%29 = call i32 @free(i8* %18)
br label %35
30: ; preds = %16
%31 = call i32 @fclose(i32* %20)
%32 = load i32, i32* %3
%33 = call i32 @print_hex(i8* %18, i32 %32)
%34 = call i32 @free(i8* %18)
br label %35
35: ; preds = %30, %25, %13
%36 = phi i32 [ 1, %13 ], [ 2, %25 ], [ 0, %30 ]
ret i32 %36
}