#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 */
int /*<<< orphan*/ _IONBF;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setvbuf(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char **argv) {
  char *filename_buf, *name_buf;
  FILE *fp;
  setvbuf(stdout, _IONBF, 0, 0);
  if (argc < 2) {
    printf("\nUsage: buffer_overflow FileName Yourname\n");
    exit(1);
  }
  if (strcmp(argv[1], "secret.txt") == 0) {
    printf(
        "\nSorry, you are not allowed to access the secret files. Exiting\n");
    exit(1);
  }
  char data_buf[60];
  memset(data_buf, 0, 60);
  name_buf = (char *)malloc(9);
  filename_buf = (char *)malloc(9);
  strcpy(filename_buf, argv[1]);
  printf("\nfilename_buf = %s\n", filename_buf);
  strcpy(name_buf, argv[2]);
  printf("\nfilename_buf = %s\n", filename_buf);
  printf("\nHello %s. Opening the file %s for you\n", name_buf, filename_buf);
  fp = fopen(filename_buf, "r");
  fread(data_buf, 1, 60, fp);
  printf("\n %s\n", data_buf);
  return 0;
}


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)
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1090 <setvbuf@plt>
cmpl   $0x2,-0x8(%rbp)
jge    11e3 <main+0x53>
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe39(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1219 <main+0x89>
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x3c,%edx
call   1060 <memset@plt>
mov    $0x9,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x9,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rsi
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rsi
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe07(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3c,%edx
call   1040 <fread@plt>
lea    -0x70(%rbp),%rsi
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%r15
mov    %edi,%ebx
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   10a0 <setvbuf@plt>
cmp    $0x1,%ebx
jle    12ad <main+0x10d>
mov    0x8(%r15),%rdi
lea    0xe1f(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     12b6 <main+0x116>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x3c,%edx
call   1070 <memset@plt>
mov    $0x9,%edi
call   1090 <malloc@plt>
mov    %rax,%r12
mov    $0x9,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    0x8(%r15),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
lea    0xddd(%rip),%r13
mov    %r13,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x10(%r15),%rsi
mov    %r12,%rdi
call   1030 <strcpy@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdc4(%rip),%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdd8(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x3c,%edx
mov    %rax,%rcx
call   1050 <fread@plt>
lea    0xdb6(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xddb(%rip),%rdi
jmp    12bd <main+0x11d>
lea    0xd92(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@_IONBF = global i32 0
@.str = [43 x i8] c"\0AUsage: buffer_overflow FileName Yourname\0A\00"
@.str.1 = [11 x i8] c"secret.txt\00"
@.str.2 = [65 x i8] c"\0ASorry, you are not allowed to access the secret files. Exiting\0A\00"
@.str.3 = [20 x i8] c"\0Afilename_buf = %s\0A\00"
@.str.4 = [40 x i8] c"\0AHello %s. Opening the file %s for you\0A\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [6 x i8] c"\0A %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca [60 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @stdout
%11 = load i32, i32* @_IONBF
%12 = call i32 @setvbuf(i32 %10, i32 %11, i32 0, i32 0)
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %18
%28 = getelementptr inbounds [60 x i8], [60 x i8]* %9, i64 0, i64 0
%29 = call i32 @memset(i8* %28, i32 0, i32 60)
%30 = call i64 @malloc(i32 9)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %7
%32 = call i64 @malloc(i32 9)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %6
%34 = load i8*, i8** %6
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i32 @strcpy(i8* %34, i8* %37)
%39 = load i8*, i8** %6
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = load i8*, i8** %7
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 2
%44 = load i8*, i8** %43
%45 = call i32 @strcpy(i8* %41, i8* %44)
%46 = load i8*, i8** %6
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = load i8*, i8** %7
%49 = load i8*, i8** %6
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i8* %48, i8* %49)
%51 = load i8*, i8** %6
%52 = call i32* @fopen(i8* %51, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %52, i32** %8
%53 = getelementptr inbounds [60 x i8], [60 x i8]* %9, i64 0, i64 0
%54 = load i32*, i32** %8
%55 = call i32 @fread(i8* %53, i32 1, i32 60, i32* %54)
%56 = getelementptr inbounds [60 x i8], [60 x i8]* %9, i64 0, i64 0
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8* %56)
ret i32 0
}

LLVM IR (-O2):
@stdout = global i32 0
@_IONBF = global i32 0
@.str.1 = [11 x i8] c"secret.txt\00"
@.str.3 = [20 x i8] c"\0Afilename_buf = %s\0A\00"
@.str.4 = [40 x i8] c"\0AHello %s. Opening the file %s for you\0A\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [6 x i8] c"\0A %s\0A\00"
@str = [64 x i8] c"\0ASorry, you are not allowed to access the secret files. Exiting\00"
@str.7 = [42 x i8] c"\0AUsage: buffer_overflow FileName Yourname\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [60 x i8]
%4 = load i32, i32* @stdout
%5 = load i32, i32* @_IONBF
%6 = tail call i32 @setvbuf(i32 %4, i32 %5, i32 0, i32 0)
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str.7, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @str, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %11
%20 = getelementptr inbounds [60 x i8], [60 x i8]* %3, i64 0, i64 0
%21 = call i32 @memset(i8* %20, i32 0, i32 60)
%22 = call i64 @malloc(i32 9)
%23 = inttoptr i64 %22 to i8*
%24 = call i64 @malloc(i32 9)
%25 = inttoptr i64 %24 to i8*
%26 = load i8*, i8** %12
%27 = call i32 @strcpy(i8* %25, i8* %26)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %25)
%29 = getelementptr inbounds i8*, i8** %1, i64 2
%30 = load i8*, i8** %29
%31 = call i32 @strcpy(i8* %23, i8* %30)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %25)
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i8* %23, i8* %25)
%34 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%35 = call i32 @fread(i8* %20, i32 1, i32 60, i32* %34)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8* %20)
ret i32 0
}