#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 */
long atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
scalar_t__ rand();
int /*<<< orphan*/ *stderr;
size_t strlen(char *);
int main(int argc, char *argv[]) {
  long fsize;
  size_t sl;
  FILE *jfile;
  long i;
  if (argc != 3) {
    printf("Usage: genjunk filename size[k,M]\n");
    return 1;
  }
  fsize = atoi(argv[2]);
  if (fsize < 0) {
    fprintf(stderr, "Error: invalid file size: %ld\n", fsize);
    return -1;
  }
  sl = strlen(argv[2]);
  if (argv[2][sl - 1] == 'k') {
    fsize = fsize * 1024;
  } else if (argv[2][sl - 1] == 'M') {
    fsize = fsize * 1048576;
  }
  jfile = fopen(argv[1], "wb");
  if (jfile == NULL) {
    fprintf(stderr, "fopen (called by genjunk): %s: ", argv[1]);
    perror(NULL);
    return -1;
  }
  for (i = 0; i < fsize; i++)
    fprintf(jfile, "%c", (unsigned char)rand());
  fclose(jfile);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11b8 <main+0x38>
lea    0xe59(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131c <main+0x19c>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11fc <main+0x7c>
mov    0x2e05(%rip),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe3a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131c <main+0x19c>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x6b,%eax
jne    123b <main+0xbb>
mov    -0x18(%rbp),%rax
shl    $0xa,%rax
mov    %rax,-0x18(%rbp)
jmp    1269 <main+0xe9>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x4d,%eax
jne    1264 <main+0xe4>
mov    -0x18(%rbp),%rax
shl    $0x14,%rax
mov    %rax,-0x18(%rbp)
jmp    1269 <main+0xe9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdca(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12c1 <main+0x141>
mov    0x2d4d(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xda0(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131c <main+0x19c>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    130c <main+0x18c>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   10a0 <rand@plt>
mov    -0x38(%rbp),%rdi
movzbl %al,%edx
lea    0xd71(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    12c9 <main+0x149>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
cmp    $0x3,%edi
jne    11cc <main+0x3c>
mov    %rsi,%r14
mov    0x10(%rsi),%r15
mov    %r15,%rdi
call   10a0 <atoi@plt>
mov    %rax,%rbx
test   %rax,%rax
js     11df <main+0x4f>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    -0x1(%rax,%r15,1),%al
cmp    $0x4d,%al
je     1201 <main+0x71>
cmp    $0x6b,%al
jne    1205 <main+0x75>
shl    $0xa,%rbx
jmp    1205 <main+0x75>
lea    0xe6f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    1252 <main+0xc2>
mov    0x2dfa(%rip),%rax
mov    (%rax),%rdi
lea    0xe10(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1252 <main+0xc2>
shl    $0x14,%rbx
mov    0x8(%r14),%rdi
lea    0xe0f(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1258 <main+0xc8>
mov    %rax,%r15
test   %rbx,%rbx
jle    1248 <main+0xb8>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   10b0 <rand@plt>
movzbl %al,%edi
mov    %r15,%rsi
call   1060 <fputc@plt>
add    $0xffffffffffffffff,%rbx
jne    1230 <main+0xa0>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d81(%rip),%rax
mov    (%rax),%rdi
mov    0x8(%r14),%rdx
lea    0xdb5(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %edi,%edi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    1252 <main+0xc2>

LLVM IR (-O0):
@.str = [35 x i8] c"Usage: genjunk filename size[k,M]\0A\00"
@stderr = global i32* null
@.str.1 = [31 x i8] c"Error: invalid file size: %ld\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [32 x i8] c"fopen (called by genjunk): %s: \00"
@.str.4 = [3 x i8] c"%c\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %87
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 2
%17 = load i8*, i8** %16
%18 = call i64 @atoi(i8* %17)
store i64 %18, i64* %6
%19 = load i64, i64* %6
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %14
%22 = load i32*, i32** @stderr
%23 = load i64, i64* %6
%24 = call i32 (i32*, i8*, ...) @fprintf(i32* %22, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i64 %23)
store i32 -1, i32* %3
br label %87
25: ; preds = %14
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i64 @strlen(i8* %28)
store i64 %29, i64* %7
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
%33 = load i64, i64* %7
%34 = sub i64 %33, 1
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 107
br i1 %38, label %39, label %42
39: ; preds = %25
%40 = load i64, i64* %6
%41 = mul nsw i64 %40, 1024
store i64 %41, i64* %6
br label %56
42: ; preds = %25
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 2
%45 = load i8*, i8** %44
%46 = load i64, i64* %7
%47 = sub i64 %46, 1
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %50, 77
br i1 %51, label %52, label %55
52: ; preds = %42
%53 = load i64, i64* %6
%54 = mul nsw i64 %53, 1048576
store i64 %54, i64* %6
br label %55
55: ; preds = %52, %42
br label %56
56: ; preds = %55, %39
%57 = load i8**, i8*** %5
%58 = getelementptr inbounds i8*, i8** %57, i64 1
%59 = load i8*, i8** %58
%60 = call i32* @fopen(i8* %59, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %60, i32** %8
%61 = load i32*, i32** %8
%62 = icmp eq i32* %61, null
br i1 %62, label %63, label %70
63: ; preds = %56
%64 = load i32*, i32** @stderr
%65 = load i8**, i8*** %5
%66 = getelementptr inbounds i8*, i8** %65, i64 1
%67 = load i8*, i8** %66
%68 = call i32 (i32*, i8*, ...) @fprintf(i32* %64, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %67)
%69 = call i32 @perror(i32* null)
store i32 -1, i32* %3
br label %87
70: ; preds = %56
store i64 0, i64* %9
br label %71
71: ; preds = %81, %70
%72 = load i64, i64* %9
%73 = load i64, i64* %6
%74 = icmp slt i64 %72, %73
br i1 %74, label %75, label %84
75: ; preds = %71
%76 = load i32*, i32** %8
%77 = call i64 (...) @rand()
%78 = trunc i64 %77 to i8
%79 = zext i8 %78 to i32
%80 = call i32 (i32*, i8*, ...) @fprintf(i32* %76, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %79)
br label %81
81: ; preds = %75
%82 = load i64, i64* %9
%83 = add nsw i64 %82, 1
store i64 %83, i64* %9
br label %71
84: ; preds = %71
%85 = load i32*, i32** %8
%86 = call i32 @fclose(i32* %85)
store i32 0, i32* %3
br label %87
87: ; preds = %84, %63, %21, %12
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
@stderr = global i32* null
@.str.1 = [31 x i8] c"Error: invalid file size: %ld\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [32 x i8] c"fopen (called by genjunk): %s: \00"
@str = [34 x i8] c"Usage: genjunk filename size[k,M]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
br label %46
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 2
%8 = load i8*, i8** %7
%9 = tail call i64 @atoi(i8* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = load i32*, i32** @stderr
%13 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i64 %9)
br label %46
14: ; preds = %6
%15 = tail call i64 @strlen(i8* dereferenceable(1) %8)
%16 = add i64 %15, -1
%17 = getelementptr inbounds i8, i8* %8, i64 %16
%18 = load i8, i8* %17
switch i8 %18, label %23 [
i8 107, label %19
i8 77, label %21
]
19: ; preds = %14
%20 = shl nsw i64 %9, 10
br label %23
21: ; preds = %14
%22 = shl nsw i64 %9, 20
br label %23
23: ; preds = %14, %21, %19
%24 = phi i64 [ %20, %19 ], [ %22, %21 ], [ %9, %14 ]
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = tail call i32* @fopen(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %31, label %29
29: ; preds = %23
%30 = icmp sgt i64 %24, 0
br i1 %30, label %36, label %44
31: ; preds = %23
%32 = load i32*, i32** @stderr
%33 = load i8*, i8** %25
%34 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %32, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %33)
%35 = tail call i32 @perror(i32* null)
br label %46
36: ; preds = %29, %36
%37 = phi i64 [ %42, %36 ], [ 0, %29 ]
%38 = tail call i64 (...) @rand()
%39 = trunc i64 %38 to i32
%40 = and i32 %39, 255
%41 = tail call i32 @fputc(i32 %40, i32* %27)
%42 = add nuw nsw i64 %37, 1
%43 = icmp eq i64 %42, %24
br i1 %43, label %44, label %36
44: ; preds = %36, %29
%45 = tail call i32 @fclose(i32* %27)
br label %46
46: ; preds = %44, %31, %11, %4
%47 = phi i32 [ 1, %4 ], [ -1, %11 ], [ -1, %31 ], [ 0, %44 ]
ret i32 %47
}