#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[32];
  if (argc < 2) {
    printf("Missing first argument (integer).\nUsing number 20 as the first "
           "argument seems interesting.\n Exiting...\n");
    return 0;
  }
  count = (char)atoi(argv[1]);
  if (!(count > 0)) {
    printf("Incorrect first argument (%d). Exiting...\n", count);
    return 0;
  }
  if ((count > 20)) {
    printf("First argument (%d) is too large. Exiting...\n", count);
    return 0;
  }
  exploit();
  printf("Reading %d bytes from file\n", count);
  f = fopen("ptr.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    $0x40,%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    12ad <main+0x13d>
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    0xe96(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ad <main+0x13d>
movsbl -0x11(%rbp),%eax
cmp    $0x14,%eax
jle    1210 <main+0xa0>
movsbl -0x11(%rbp),%esi
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ad <main+0x13d>
mov    $0x0,%al
call   1090 <exploit@plt>
movsbl -0x11(%rbp),%esi
lea    0xe9f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xead(%rip),%rdi
lea    0xeae(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     127e <main+0x10e>
lea    -0x40(%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    -0x40(%rbp),%rdi
movsbl -0x11(%rbp),%esi
call   1030 <vuln@plt>
jmp    1298 <main+0x128>
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ad <main+0x13d>
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
cmp    $0x1,%edi
jg     11a2 <main+0x22>
lea    0xf40(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    1248 <main+0xc8>
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
movsbl %al,%ebp
test   %al,%al
jle    11bf <main+0x3f>
cmp    $0x15,%al
jb     11d3 <main+0x53>
lea    0xe6e(%rip),%rdi
jmp    11c6 <main+0x46>
lea    0xe3a(%rip),%rdi
xor    %ebx,%ebx
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1248 <main+0xc8>
xor    %ebx,%ebx
xor    %eax,%eax
call   10a0 <exploit@plt>
lea    0xe76(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe82(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     123c <main+0xbc>
mov    %rax,%r14
mov    %rsp,%r15
mov    $0x1,%ebx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1050 <fread@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
mov    %r15,%rdi
mov    %ebp,%esi
call   1040 <vuln@plt>
lea    0xe6b(%rip),%rdi
jmp    1243 <main+0xc3>
lea    0xe3c(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [104 x i8] c"Missing first argument (integer).\0AUsing number 20 as the first argument seems interesting.\0A Exiting...\0A\00"
@.str.1 = [43 x i8] c"Incorrect first argument (%d). Exiting...\0A\00"
@.str.2 = [46 x i8] c"First argument (%d) is too large. Exiting...\0A\00"
@.str.3 = [28 x i8] c"Reading %d bytes from file\0A\00"
@.str.4 = [8 x i8] c"ptr.txt\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [39 x i8] c"Input file does not exist. Exiting...\0A\00"
@.str.7 = [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 [32 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 ([104 x i8], [104 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %56
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 %56
26: ; preds = %13
%27 = load i8, i8* %6
%28 = sext i8 %27 to i32
%29 = icmp sgt i32 %28, 20
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load i8, i8* %6
%32 = sext i8 %31 to i32
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %32)
store i32 0, i32* %3
br label %56
34: ; preds = %26
%35 = call i32 (...) @exploit()
%36 = load i8, i8* %6
%37 = sext i8 %36 to i32
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %37)
%39 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %39, i32** %7
%40 = load i32*, i32** %7
%41 = icmp ne i32* %40, null
br i1 %41, label %42, label %52
42: ; preds = %34
%43 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%44 = load i8, i8* %6
%45 = load i32*, i32** %7
%46 = call i32 @fread(i8* %43, i32 1, i8 signext %44, i32* %45)
%47 = load i32*, i32** %7
%48 = call i32 @fclose(i32* %47)
%49 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%50 = load i8, i8* %6
%51 = call i32 @vuln(i8* %49, i8 signext %50)
br label %54
52: ; preds = %34
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %3
br label %56
54: ; preds = %42
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %56
56: ; preds = %54, %52, %30, %22, %11
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@.str.1 = [43 x i8] c"Incorrect first argument (%d). Exiting...\0A\00"
@.str.2 = [46 x i8] c"First argument (%d) is too large. Exiting...\0A\00"
@.str.3 = [28 x i8] c"Reading %d bytes from file\0A\00"
@.str.4 = [8 x i8] c"ptr.txt\00"
@.str.5 = [2 x i8] c"r\00"
@str = [38 x i8] c"Input file does not exist. Exiting...\00"
@str.8 = [49 x i8] c"Control has returned to main()\0ANormal operation.\00"
@str.9 = [103 x i8] c"Missing first argument (integer).\0AUsing number 20 as the first argument seems interesting.\0A Exiting...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 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 ([103 x i8], [103 x i8]* @str.9, i64 0, i64 0))
br label %33
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 %33
17: ; preds = %8
%18 = icmp ugt i8 %12, 20
br i1 %18, label %19, label %21
19: ; preds = %17
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %13)
br label %33
21: ; preds = %17
%22 = tail call i32 (...) @exploit()
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%24 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %31, label %26
26: ; preds = %21
%27 = call i32 @fread(i8* %4, i32 1, i8 signext %12, i32* %24)
%28 = tail call i32 @fclose(i32* %24)
%29 = call i32 @vuln(i8* %4, i8 signext %12)
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str.8, i64 0, i64 0))
br label %33
31: ; preds = %21
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %33
33: ; preds = %26, %31, %19, %15, %6
%34 = phi i32 [ 0, %6 ], [ 0, %19 ], [ 1, %26 ], [ 0, %31 ], [ 0, %15 ]
ret i32 %34
}