#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 */

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ main_menu(int);
int /*<<< orphan*/ print_exit(char *, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strtol(char *, char **, int);
int main(int argc, char **argv) {
  printf("argc: %d", argc);
  if (argc == 2) {
    int x = 0;
    x = (int)strtol(argv[1], (char **)NULL, 10);
    if (0 < x && x < 4) {
      main_menu(x);
    } else {
      print_exit("M - Incorrect parameter entered.", errno, NULL);
    }
  } else if (argc > 2) {
    printf("Usage: %s [1] | [2] | [3]\nExiting..\n", argv[0]);
  } else {
    printf("%s",
           "Welcome to DC612 January 2015\n"
           "Tonight, we are going to play with some basic format string "
           "exploitation.\n"
           "Feel free to ask all those pesky questions, and enjoy yourself!\n");
    main_menu(0);
  }
  printf("%s\n", "Thanks for playing!");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x2,-0x8(%rbp)
jne    11e1 <main+0x91>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
jge    11c3 <main+0x73>
cmpl   $0x4,-0x14(%rbp)
jge    11c3 <main+0x73>
mov    -0x14(%rbp),%edi
call   1050 <main_menu@plt>
jmp    11dc <main+0x8c>
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
lea    0xe36(%rip),%rdi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <print_exit@plt>
jmp    1226 <main+0xd6>
cmpl   $0x2,-0x8(%rbp)
jle    1205 <main+0xb5>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1221 <main+0xd1>
lea    0xe43(%rip),%rdi
lea    0xe3f(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1050 <main_menu@plt>
jmp    1226 <main+0xd6>
lea    0xece(%rip),%rdi
lea    0xecb(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe91(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x2,%ebp
jne    1199 <main+0x39>
mov    0x8(%rbx),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
lea    -0x1(%rax),%ecx
cmp    $0x2,%ecx
ja     11ae <main+0x4e>
mov    %eax,%edi
jmp    11d5 <main+0x75>
jle    11c7 <main+0x67>
mov    (%rbx),%rsi
lea    0xe85(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11da <main+0x7a>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xe4b(%rip),%rdi
xor    %edx,%edx
call   1080 <print_exit@plt>
jmp    11da <main+0x7a>
lea    0xe95(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1060 <main_menu@plt>
lea    0xe6e(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"argc: %d\00"
@.str.1 = [33 x i8] c"M - Incorrect parameter entered.\00"
@errno = global i32 0
@.str.2 = [37 x i8] c"Usage: %s [1] | [2] | [3]\0AExiting..\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [169 x i8] c"Welcome to DC612 January 2015\0ATonight, we are going to play with some basic format string exploitation.\0AFeel free to ask all those pesky questions, and enjoy yourself!\0A\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [20 x i8] c"Thanks for playing!\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 2
br i1 %10, label %11, label %29
11: ; preds = %2
store i32 0, i32* %6
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i64 @strtol(i8* %14, i8** null, i32 10)
%16 = trunc i64 %15 to i32
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 0, %17
br i1 %18, label %19, label %25
19: ; preds = %11
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 4
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* %6
%24 = call i32 @main_menu(i32 %23)
br label %28
25: ; preds = %19, %11
%26 = load i32, i32* @errno
%27 = call i32 @print_exit(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %26, i32* null)
br label %28
28: ; preds = %25, %22
br label %41
29: ; preds = %2
%30 = load i32, i32* %4
%31 = icmp sgt i32 %30, 2
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 0
%35 = load i8*, i8** %34
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %35)
br label %40
37: ; preds = %29
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([169 x i8], [169 x i8]* @.str.4, i64 0, i64 0))
%39 = call i32 @main_menu(i32 0)
br label %40
40: ; preds = %37, %32
br label %41
41: ; preds = %40, %28
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
%43 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [9 x i8] c"argc: %d\00"
@.str.1 = [33 x i8] c"M - Incorrect parameter entered.\00"
@errno = global i32 0
@.str.2 = [37 x i8] c"Usage: %s [1] | [2] | [3]\0AExiting..\0A\00"
@.str.6 = [20 x i8] c"Thanks for playing!\00"
@str = [168 x i8] c"Welcome to DC612 January 2015\0ATonight, we are going to play with some basic format string exploitation.\0AFeel free to ask all those pesky questions, and enjoy yourself!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %0)
%4 = icmp eq i32 %0, 2
br i1 %4, label %5, label %17
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i64 @strtol(i8* nocapture %7, i8** null, i32 10)
%9 = trunc i64 %8 to i32
%10 = add i32 %9, -1
%11 = icmp ult i32 %10, 3
br i1 %11, label %12, label %14
12: ; preds = %5
%13 = tail call i32 @main_menu(i32 %9)
br label %25
14: ; preds = %5
%15 = load i32, i32* @errno
%16 = tail call i32 @print_exit(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32* null)
br label %25
17: ; preds = %2
%18 = icmp sgt i32 %0, 2
br i1 %18, label %19, label %22
19: ; preds = %17
%20 = load i8*, i8** %1
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %20)
br label %25
22: ; preds = %17
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([168 x i8], [168 x i8]* @str, i64 0, i64 0))
%24 = tail call i32 @main_menu(i32 0)
br label %25
25: ; preds = %12, %14, %19, %22
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
%27 = tail call i32 @exit(i32 0)
unreachable
}