#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, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ vuln(char *, int);
int main(int argc, char **argv) {
  FILE *f;
  int count;
  char *msg;
  char fstr[64];
  if (argc < 2) {
    printf("Missing first argument (integer).\nUsing number 7 as the first "
           "argument seems interesting.\nExiting...\n");
    return 0;
  }
  if (argc < 3) {
    printf("Missing second argument (string) to echo.\nExiting...\n");
    return 0;
  }
  count = (char)atoi(argv[1]) % 16;
  msg = argv[2];
  if (!(count > 0)) {
    printf("Incorrect first argument (%d). Exiting...\n", count);
    return 0;
  }
  exploit(count, msg);
  printf("Reading string from file\n");
  f = fopen("format.txt", "r");
  if (f != NULL) {
    fread(fstr, sizeof(char), 64, f);
    fclose(f);
    vuln(fstr, 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    $0x70,%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    12b8 <main+0x148>
cmpl   $0x3,-0x8(%rbp)
jge    11ce <main+0x5e>
lea    0xeab(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b8 <main+0x148>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
movsbl %al,%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     121c <main+0xac>
mov    -0x1c(%rbp),%esi
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b8 <main+0x148>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1090 <exploit@plt>
lea    0xe98(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xea4(%rip),%rdi
lea    0xea8(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1289 <main+0x119>
lea    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x40,%edx
call   1040 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
lea    -0x70(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1030 <vuln@plt>
jmp    12a3 <main+0x133>
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b8 <main+0x148>
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
cmp    $0x1,%edi
jg     1198 <main+0x18>
lea    0xf47(%rip),%rdi
jmp    11a4 <main+0x24>
cmp    $0x2,%edi
jne    11b8 <main+0x38>
lea    0xf04(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
mov    %eax,%ecx
sar    $0x7,%cl
shr    $0x4,%cl
add    %al,%cl
and    $0xf0,%cl
sub    %cl,%al
movsbl %al,%r14d
test   %r14b,%r14b
jle    1250 <main+0xd0>
mov    0x10(%rbp),%rsi
mov    %r14d,%edi
call   10a0 <exploit@plt>
lea    0xe49(%rip),%rdi
call   1030 <puts@plt>
lea    0xe30(%rip),%rdi
lea    0xe34(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1268 <main+0xe8>
mov    %rax,%rbx
mov    %rsp,%r15
mov    $0x1,%ebp
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x40,%edx
mov    %rax,%rcx
call   1050 <fread@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r15,%rdi
mov    %r14d,%esi
call   1040 <vuln@plt>
lea    0xe31(%rip),%rdi
call   1030 <puts@plt>
jmp    11ab <main+0x2b>
lea    0xda9(%rip),%rdi
xor    %ebp,%ebp
mov    %r14d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    11ab <main+0x2b>
lea    0xde2(%rip),%rdi
jmp    11a4 <main+0x24>

LLVM IR (-O0):
@.str = [102 x i8] c"Missing first argument (integer).\0AUsing number 7 as the first argument seems interesting.\0AExiting...\0A\00"
@.str.1 = [54 x i8] c"Missing second argument (string) to echo.\0AExiting...\0A\00"
@.str.2 = [43 x i8] c"Incorrect first argument (%d). Exiting...\0A\00"
@.str.3 = [26 x i8] c"Reading string from file\0A\00"
@.str.4 = [11 x i8] c"format.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 i32
%8 = alloca i8*
%9 = alloca [64 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 ([102 x i8], [102 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %56
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 3
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %56
19: ; preds = %14
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i64 @atoi(i8* %22)
%24 = trunc i64 %23 to i8
%25 = sext i8 %24 to i32
%26 = srem i32 %25, 16
store i32 %26, i32* %7
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
store i8* %29, i8** %8
%30 = load i32, i32* %7
%31 = icmp sgt i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %19
%33 = load i32, i32* %7
%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 %56
35: ; preds = %19
%36 = load i32, i32* %7
%37 = load i8*, i8** %8
%38 = call i32 @exploit(i32 %36, i8* %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 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* %40, i32** %6
%41 = load i32*, i32** %6
%42 = icmp ne i32* %41, null
br i1 %42, label %43, label %52
43: ; preds = %35
%44 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%45 = load i32*, i32** %6
%46 = call i32 @fread(i8* %44, i32 1, i32 64, i32* %45)
%47 = load i32*, i32** %6
%48 = call i32 @fclose(i32* %47)
%49 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%50 = load i32, i32* %7
%51 = call i32 @vuln(i8* %49, i32 %50)
br label %54
52: ; preds = %35
%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 = %43
%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, %32, %17, %12
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@.str.2 = [43 x i8] c"Incorrect first argument (%d). Exiting...\0A\00"
@.str.4 = [11 x i8] c"format.txt\00"
@.str.5 = [2 x i8] c"r\00"
@str = [25 x i8] c"Reading string from file\00"
@str.8 = [38 x i8] c"Input file does not exist. Exiting...\00"
@str.9 = [49 x i8] c"Control has returned to main()\0ANormal operation.\00"
@str.10 = [53 x i8] c"Missing second argument (string) to echo.\0AExiting...\00"
@str.11 = [101 x i8] c"Missing first argument (integer).\0AUsing number 7 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 ([101 x i8], [101 x i8]* @str.11, i64 0, i64 0))
br label %36
8: ; preds = %2
%9 = icmp eq i32 %0, 2
br i1 %9, label %10, label %12
10: ; preds = %8
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.10, i64 0, i64 0))
br label %36
12: ; preds = %8
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i64 @atoi(i8* %14)
%16 = trunc i64 %15 to i8
%17 = srem i8 %16, 16
%18 = sext i8 %17 to i32
%19 = icmp sgt i8 %17, 0
br i1 %19, label %22, label %20
20: ; preds = %12
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %18)
br label %36
22: ; preds = %12
%23 = getelementptr inbounds i8*, i8** %1, i64 2
%24 = load i8*, i8** %23
%25 = tail call i32 @exploit(i32 %18, i8* %24)
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%27 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %34, label %29
29: ; preds = %22
%30 = call i32 @fread(i8* %4, i32 1, i32 64, i32* %27)
%31 = tail call i32 @fclose(i32* %27)
%32 = call i32 @vuln(i8* %4, i32 %18)
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str.9, i64 0, i64 0))
br label %36
34: ; preds = %22
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.8, i64 0, i64 0))
br label %36
36: ; preds = %29, %34, %20, %10, %6
%37 = phi i32 [ 0, %6 ], [ 0, %10 ], [ 1, %29 ], [ 0, %34 ], [ 0, %20 ]
ret i32 %37
}