#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 */
scalar_t__ atoi(char *);
int /*<<< orphan*/ exploit();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, char, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ vuln(char *, char);
int main(int argc, char **argv) {
  char count;
  FILE *f;
  char overflow[64];
  if (argc < 2) {
    printf("Missing first argument (integer).\nUsing number 24 as the first "
           "argument seems interesting.\nExiting...\n");
    return 0;
  }
  count = (char)atoi(argv[1]);
  if (!(count > 0)) {
    printf("Incorrect first argument (%d). Exiting...\n", count);
    return 0;
  }
  exploit();
  printf("Reading %d bytes from file\n", count);
  f = fopen("overflow.txt", "r");
  if (f != NULL) {
    fread(overflow, sizeof(char), count, f);
    fclose(f);
    vuln(overflow, count);
  } else {
    printf("Input file does not exist. Exiting...\n");
    return 0;
  }
  printf("Control has returned to main()\nNormal operation.\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11aa <main+0x3a>
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1282 <main+0x112>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0x0,%eax
jg     11e5 <main+0x75>
movsbl -0x11(%rbp),%esi
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1282 <main+0x112>
mov    $0x0,%al
call   1090 <exploit@plt>
movsbl -0x11(%rbp),%esi
lea    0xe9b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xea9(%rip),%rdi
lea    0xeaf(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1253 <main+0xe3>
lea    -0x60(%rbp),%rdi
mov    -0x11(%rbp),%al
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
movsbl %al,%edx
call   1040 <fread@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
lea    -0x60(%rbp),%rdi
movsbl -0x11(%rbp),%esi
call   1030 <vuln@plt>
jmp    126d <main+0xfd>
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1282 <main+0x112>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
cmp    $0x1,%edi
jg     11a2 <main+0x22>
lea    0xf17(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    1240 <main+0xc0>
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
movsbl %al,%r14d
test   %al,%al
jle    121f <main+0x9f>
xor    %ebx,%ebx
xor    %eax,%eax
call   10a0 <exploit@plt>
lea    0xe68(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe73(%rip),%rdi
lea    0xe79(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1234 <main+0xb4>
mov    %rax,%rbp
mov    %rsp,%r15
mov    $0x1,%ebx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rax,%rcx
call   1050 <fread@plt>
mov    %rbp,%rdi
call   1060 <fclose@plt>
mov    %r15,%rdi
mov    %r14d,%esi
call   1040 <vuln@plt>
lea    0xe5f(%rip),%rdi
jmp    123b <main+0xbb>
lea    0xdda(%rip),%rdi
xor    %ebx,%ebx
mov    %r14d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1240 <main+0xc0>
lea    0xe1b(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [103 x i8] c"Missing first argument (integer).\0AUsing number 24 as the first argument seems interesting.\0AExiting...\0A\00"
@.str.1 = [43 x i8] c"Incorrect first argument (%d). Exiting...\0A\00"
@.str.2 = [28 x i8] c"Reading %d bytes from file\0A\00"
@.str.3 = [13 x i8] c"overflow.txt\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [39 x i8] c"Input file does not exist. Exiting...\0A\00"
@.str.6 = [50 x i8] c"Control has returned to main()\0ANormal operation.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i32*
%8 = alloca [64 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([103 x i8], [103 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %48
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i64 @atoi(i8* %16)
%18 = trunc i64 %17 to i8
store i8 %18, i8* %6
%19 = load i8, i8* %6
%20 = sext i8 %19 to i32
%21 = icmp sgt i32 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %13
%23 = load i8, i8* %6
%24 = sext i8 %23 to i32
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %24)
store i32 0, i32* %3
br label %48
26: ; preds = %13
%27 = call i32 (...) @exploit()
%28 = load i8, i8* %6
%29 = sext i8 %28 to i32
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %31, i32** %7
%32 = load i32*, i32** %7
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %44
34: ; preds = %26
%35 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%36 = load i8, i8* %6
%37 = load i32*, i32** %7
%38 = call i32 @fread(i8* %35, i32 1, i8 signext %36, i32* %37)
%39 = load i32*, i32** %7
%40 = call i32 @fclose(i32* %39)
%41 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%42 = load i8, i8* %6
%43 = call i32 @vuln(i8* %41, i8 signext %42)
br label %46
44: ; preds = %26
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %3
br label %48
46: ; preds = %34
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %48
48: ; preds = %46, %44, %22, %11
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@.str.1 = [43 x i8] c"Incorrect first argument (%d). Exiting...\0A\00"
@.str.2 = [28 x i8] c"Reading %d bytes from file\0A\00"
@.str.3 = [13 x i8] c"overflow.txt\00"
@.str.4 = [2 x i8] c"r\00"
@str = [38 x i8] c"Input file does not exist. Exiting...\00"
@str.7 = [49 x i8] c"Control has returned to main()\0ANormal operation.\00"
@str.8 = [102 x i8] c"Missing first argument (integer).\0AUsing number 24 as the first argument seems interesting.\0AExiting...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([102 x i8], [102 x i8]* @str.8, i64 0, i64 0))
br label %29
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i64 @atoi(i8* %10)
%12 = trunc i64 %11 to i8
%13 = sext i8 %12 to i32
%14 = icmp sgt i8 %12, 0
br i1 %14, label %17, label %15
15: ; preds = %8
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %29
17: ; preds = %8
%18 = tail call i32 (...) @exploit()
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%20 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %27, label %22
22: ; preds = %17
%23 = call i32 @fread(i8* %4, i32 1, i8 signext %12, i32* %20)
%24 = tail call i32 @fclose(i32* %20)
%25 = call i32 @vuln(i8* %4, i8 signext %12)
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str.7, i64 0, i64 0))
br label %29
27: ; preds = %17
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %29
29: ; preds = %22, %27, %15, %6
%30 = phi i32 [ 0, %6 ], [ 1, %22 ], [ 0, %27 ], [ 0, %15 ]
ret i32 %30
}