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

/* Variables and functions */
int atoi(char const *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int /*<<< orphan*/ free(unsigned char *);
int getrandom(unsigned char *, int, int /*<<< orphan*/);
unsigned char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usage(char const *);
int main(int argc, const char **argv) {
  int reps, size;
  unsigned char *buf;
  if (argc != 3) {
    usage(argv[0]);
    return 1;
  }
  reps = atoi(argv[1]);
  size = atoi(argv[2]);
  buf = malloc(size);
  if (!buf) {
    fprintf(stderr, "failure to allocate buffer of size %d\n", size);
    return 1;
  }
  for (int i = 0; i < reps; i++) {
    ssize_t resp = getrandom(buf, size, 0);
    if (resp != size) {
      fprintf(stderr, "tried to get %d bytes, got %zd\n", size, resp);
      free(buf);
      return 2;
    }
    for (int j = 0; j < size; j++) {
      printf("%02x", buf[j]);
    }
  }
  free(buf);
  printf("\n");
  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)
cmpl   $0x3,-0x8(%rbp)
je     11a8 <main+0x38>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <usage@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d3 <main+0x163>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1205 <main+0x95>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xe0e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d3 <main+0x163>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12b5 <main+0x145>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
xor    %edx,%edx
call   1090 <getrandom@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     1267 <main+0xf7>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0x28(%rbp),%ecx
lea    0xddc(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x2,-0x4(%rbp)
jmp    12d3 <main+0x163>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12a2 <main+0x132>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    126e <main+0xfe>
jmp    12a7 <main+0x137>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    120c <main+0x9c>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    122a <main+0xaa>
mov    0x8(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     124f <main+0xcf>
mov    %rax,%rbx
mov    %ebp,0x4(%rsp)
test   %r14d,%r14d
jle    1239 <main+0xb9>
mov    0x4(%rsp),%ebp
lea    0xe72(%rip),%r12
xor    %r13d,%r13d
jmp    11ec <main+0x6c>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r13d
mov    %r15d,%r14d
cmp    %r15d,%r13d
je     1239 <main+0xb9>
mov    %rbx,%rdi
mov    0x4(%rsp),%r15d
mov    %r15d,%esi
xor    %edx,%edx
call   10a0 <getrandom@plt>
mov    %r15d,%edx
cmp    %r15d,%eax
jne    126f <main+0xef>
mov    %r14d,%r15d
test   %edx,%edx
jle    11e0 <main+0x60>
xor    %r14d,%r14d
movzbl (%rbx,%r14,1),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%r14
cmp    %r14,%rbp
jne    1210 <main+0x90>
jmp    11e0 <main+0x60>
mov    (%rbx),%rdi
call   1080 <usage@plt>
mov    $0x1,%eax
jmp    1295 <main+0x115>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
xor    %eax,%eax
jmp    1295 <main+0x115>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xda1(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%eax
jmp    1295 <main+0x115>
mov    0x2d6a(%rip),%rcx
mov    (%rcx),%edi
lea    0xda8(%rip),%rsi
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x2,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [39 x i8] c"failure to allocate buffer of size %d\0A\00"
@.str.1 = [32 x i8] c"tried to get %d bytes, got %zd\0A\00"
@.str.2 = [5 x i8] c"%02x\00"
@.str.3 = [2 x i8] c"\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
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @usage(i8* %17)
store i32 1, i32* %3
br label %79
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %6
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = call i8* @malloc(i32 %28)
store i8* %29, i8** %8
%30 = load i8*, i8** %8
%31 = icmp ne i8* %30, null
br i1 %31, label %36, label %32
32: ; preds = %19
%33 = load i32, i32* @stderr
%34 = load i32, i32* %7
%35 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %34)
store i32 1, i32* %3
br label %79
36: ; preds = %19
store i32 0, i32* %9
br label %37
37: ; preds = %72, %36
%38 = load i32, i32* %9
%39 = load i32, i32* %6
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %75
41: ; preds = %37
%42 = load i8*, i8** %8
%43 = load i32, i32* %7
%44 = call i32 @getrandom(i8* %42, i32 %43, i32 0)
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = load i32, i32* %7
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %55
48: ; preds = %41
%49 = load i32, i32* @stderr
%50 = load i32, i32* %7
%51 = load i32, i32* %10
%52 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %50, i32 %51)
%53 = load i8*, i8** %8
%54 = call i32 @free(i8* %53)
store i32 2, i32* %3
br label %79
55: ; preds = %41
store i32 0, i32* %11
br label %56
56: ; preds = %68, %55
%57 = load i32, i32* %11
%58 = load i32, i32* %7
%59 = icmp slt i32 %57, %58
br i1 %59, label %60, label %71
60: ; preds = %56
%61 = load i8*, i8** %8
%62 = load i32, i32* %11
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
%65 = load i8, i8* %64
%66 = zext i8 %65 to i32
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %66)
br label %68
68: ; preds = %60
%69 = load i32, i32* %11
%70 = add nsw i32 %69, 1
store i32 %70, i32* %11
br label %56
71: ; preds = %56
br label %72
72: ; preds = %71
%73 = load i32, i32* %9
%74 = add nsw i32 %73, 1
store i32 %74, i32* %9
br label %37
75: ; preds = %37
%76 = load i8*, i8** %8
%77 = call i32 @free(i8* %76)
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %79
79: ; preds = %75, %48, %32, %14
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [39 x i8] c"failure to allocate buffer of size %d\0A\00"
@.str.1 = [32 x i8] c"tried to get %d bytes, got %zd\0A\00"
@.str.2 = [5 x i8] c"%02x\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @usage(i8* %5)
br label %47
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
%14 = tail call i8* @malloc(i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %21, label %16
16: ; preds = %7
%17 = icmp sgt i32 %10, 0
br i1 %17, label %18, label %44
18: ; preds = %16
%19 = icmp sgt i32 %13, 0
%20 = zext i32 %13 to i64
br label %27
21: ; preds = %7
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %47
24: ; preds = %36, %31
%25 = add nuw nsw i32 %28, 1
%26 = icmp eq i32 %25, %10
br i1 %26, label %44, label %27
27: ; preds = %18, %24
%28 = phi i32 [ 0, %18 ], [ %25, %24 ]
%29 = tail call i32 @getrandom(i8* %14, i32 %13, i32 0)
%30 = icmp eq i32 %29, %13
br i1 %30, label %31, label %32
31: ; preds = %27
br i1 %19, label %36, label %24
32: ; preds = %27
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %29)
%35 = tail call i32 @free(i8* %14)
br label %47
36: ; preds = %31, %36
%37 = phi i64 [ %42, %36 ], [ 0, %31 ]
%38 = getelementptr inbounds i8, i8* %14, i64 %37
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %40)
%42 = add nuw nsw i64 %37, 1
%43 = icmp eq i64 %42, %20
br i1 %43, label %24, label %36
44: ; preds = %24, %16
%45 = tail call i32 @free(i8* %14)
%46 = tail call i32 @putchar(i32 10)
br label %47
47: ; preds = %32, %44, %21, %4
%48 = phi i32 [ 1, %4 ], [ 0, %44 ], [ 1, %21 ], [ 2, %32 ]
ret i32 %48
}