#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 *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, char, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ secureapp(int *);
int /*<<< orphan*/ vuln(char *, char);
int main(int argc, char **argv) {
  int *privilege;
  char count;
  FILE *f;
  char overflow[32];
  if (argc < 2) {
    printf("Missing first argument (integer).\nUsing number 24 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 > 24)) {
    printf("First argument (%d) too large. Exiting...\n", count);
    return 0;
  }
  privilege = (int *)malloc(1 * sizeof(int));
  *privilege = 0x0;
  exploit(privilege);
  printf("Reading %d bytes from file\n", count);
  f = fopen("priv.txt", "r");
  if (f != NULL) {
    fread(overflow, sizeof(char), count, f);
    fclose(f);
    vuln(overflow, count);
    secureapp(privilege);
  } 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    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11ca <main+0x3a>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f0 <main+0x160>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%eax
cmp    $0x0,%eax
jg     1205 <main+0x75>
movsbl -0x19(%rbp),%esi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f0 <main+0x160>
movsbl -0x19(%rbp),%eax
cmp    $0x18,%eax
jle    1230 <main+0xa0>
movsbl -0x19(%rbp),%esi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f0 <main+0x160>
mov    $0x4,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
call   10b0 <exploit@plt>
movsbl -0x19(%rbp),%esi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe70(%rip),%rdi
lea    0xe72(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12c1 <main+0x131>
lea    -0x50(%rbp),%rdi
mov    -0x19(%rbp),%al
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
movsbl %al,%edx
call   1040 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
lea    -0x50(%rbp),%rdi
movsbl -0x19(%rbp),%esi
call   1030 <vuln@plt>
mov    -0x18(%rbp),%rdi
call   1080 <secureapp@plt>
jmp    12db <main+0x14b>
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f0 <main+0x160>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
cmp    $0x1,%edi
jg     11c4 <main+0x24>
lea    0xf1c(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    1289 <main+0xe9>
mov    0x8(%rsi),%rdi
call   10b0 <atoi@plt>
movsbl %al,%ebp
test   %al,%al
jle    11e1 <main+0x41>
cmp    $0x19,%al
jb     11f8 <main+0x58>
lea    0xe4c(%rip),%rdi
jmp    11e8 <main+0x48>
lea    0xe18(%rip),%rdi
xor    %ebx,%ebx
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1289 <main+0xe9>
mov    $0x4,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
movl   $0x0,(%rax)
mov    %rax,%rdi
call   10c0 <exploit@plt>
lea    0xe3c(%rip),%rdi
xor    %ebx,%ebx
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe46(%rip),%rdi
lea    0xe48(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     127d <main+0xdd>
mov    %rax,%r15
mov    %rsp,%r12
mov    $0x1,%ebx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1050 <fread@plt>
mov    %r15,%rdi
call   1060 <fclose@plt>
mov    %r12,%rdi
mov    %ebp,%esi
call   1040 <vuln@plt>
mov    %r14,%rdi
call   1090 <secureapp@plt>
lea    0xe28(%rip),%rdi
jmp    1284 <main+0xe4>
lea    0xdf9(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [104 x i8] c"Missing first argument (integer).\0AUsing number 24 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 = [43 x i8] c"First argument (%d) too large. Exiting...\0A\00"
@.str.3 = [28 x i8] c"Reading %d bytes from file\0A\00"
@.str.4 = [9 x i8] c"priv.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 i32*
%7 = alloca i8
%8 = alloca i32*
%9 = alloca [32 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = 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 %63
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i64 @atoi(i8* %17)
%19 = trunc i64 %18 to i8
store i8 %19, i8* %7
%20 = load i8, i8* %7
%21 = sext i8 %20 to i32
%22 = icmp sgt i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %14
%24 = load i8, i8* %7
%25 = sext i8 %24 to i32
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %25)
store i32 0, i32* %3
br label %63
27: ; preds = %14
%28 = load i8, i8* %7
%29 = sext i8 %28 to i32
%30 = icmp sgt i32 %29, 24
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = load i8, i8* %7
%33 = sext i8 %32 to i32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %33)
store i32 0, i32* %3
br label %63
35: ; preds = %27
%36 = call i64 @malloc(i32 4)
%37 = inttoptr i64 %36 to i32*
store i32* %37, i32** %6
%38 = load i32*, i32** %6
store i32 0, i32* %38
%39 = load i32*, i32** %6
%40 = call i32 @exploit(i32* %39)
%41 = load i8, i8* %7
%42 = sext i8 %41 to i32
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %42)
%44 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 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* %44, i32** %8
%45 = load i32*, i32** %8
%46 = icmp ne i32* %45, null
br i1 %46, label %47, label %59
47: ; preds = %35
%48 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%49 = load i8, i8* %7
%50 = load i32*, i32** %8
%51 = call i32 @fread(i8* %48, i32 1, i8 signext %49, i32* %50)
%52 = load i32*, i32** %8
%53 = call i32 @fclose(i32* %52)
%54 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%55 = load i8, i8* %7
%56 = call i32 @vuln(i8* %54, i8 signext %55)
%57 = load i32*, i32** %6
%58 = call i32 @secureapp(i32* %57)
br label %61
59: ; preds = %35
%60 = 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 %63
61: ; preds = %47
%62 = 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 %63
63: ; preds = %61, %59, %31, %23, %12
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
@.str.1 = [43 x i8] c"Incorrect first argument (%d). Exiting...\0A\00"
@.str.2 = [43 x i8] c"First argument (%d) too large. Exiting...\0A\00"
@.str.3 = [28 x i8] c"Reading %d bytes from file\0A\00"
@.str.4 = [9 x i8] c"priv.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 24 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 %36
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 %36
17: ; preds = %8
%18 = icmp ugt i8 %12, 24
br i1 %18, label %19, label %21
19: ; preds = %17
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %13)
br label %36
21: ; preds = %17
%22 = tail call i64 @malloc(i32 4)
%23 = inttoptr i64 %22 to i32*
store i32 0, i32* %23
%24 = tail call i32 @exploit(i32* %23)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%26 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%27 = icmp eq i32* %26, null
br i1 %27, label %34, label %28
28: ; preds = %21
%29 = call i32 @fread(i8* %4, i32 1, i8 signext %12, i32* %26)
%30 = tail call i32 @fclose(i32* %26)
%31 = call i32 @vuln(i8* %4, i8 signext %12)
%32 = call i32 @secureapp(i32* %23)
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str.8, i64 0, i64 0))
br label %36
34: ; preds = %21
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %36
36: ; preds = %28, %34, %19, %15, %6
%37 = phi i32 [ 0, %6 ], [ 0, %19 ], [ 1, %28 ], [ 0, %34 ], [ 0, %15 ]
ret i32 %37
}