#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ vul(char *, int);
int main(int argc, char **argv) {
  char *data;
  int length;
  FILE *finput;
  printf("Compile: gcc vulp.c -o vulp -fno-stack-protector ");
  printf("(-g should be involved to use SystemTap)\n");
  printf("Run: ./vulp filename (<=16B: normal, >16B: abnormal)\n");
  data = (char *)malloc(1024);
  finput = fopen(argv[1], "r");
  length = fread(data, 1, 1024, finput);
  printf("%d bytes read.\n", length);
  fclose(finput);
  vul(data, length);
  printf("Returned Properly\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xea7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xec3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xed5(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1080 <vul@plt>
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe7f(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xeb5(%rip),%rdi
call   1030 <puts@plt>
lea    0xed2(%rip),%rdi
call   1030 <puts@plt>
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    0x8(%r14),%rdi
lea    0xe7a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
lea    0xe56(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
mov    %ebp,%esi
call   1090 <vul@plt>
lea    0xea2(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [50 x i8] c"Compile: gcc vulp.c -o vulp -fno-stack-protector \00"
@.str.1 = [42 x i8] c"(-g should be involved to use SystemTap)\0A\00"
@.str.2 = [54 x i8] c"Run: ./vulp filename (<=16B: normal, >16B: abnormal)\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [16 x i8] c"%d bytes read.\0A\00"
@.str.5 = [19 x i8] c"Returned Properly\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 i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0))
%12 = call i64 @malloc(i32 1024)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %6
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %17, i32** %8
%18 = load i8*, i8** %6
%19 = load i32*, i32** %8
%20 = call i32 @fread(i8* %18, i32 1, i32 1024, i32* %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%23 = load i32*, i32** %8
%24 = call i32 @fclose(i32* %23)
%25 = load i8*, i8** %6
%26 = load i32, i32* %7
%27 = call i32 @vul(i8* %25, i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [50 x i8] c"Compile: gcc vulp.c -o vulp -fno-stack-protector \00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [16 x i8] c"%d bytes read.\0A\00"
@str = [41 x i8] c"(-g should be involved to use SystemTap)\00"
@str.6 = [53 x i8] c"Run: ./vulp filename (<=16B: normal, >16B: abnormal)\00"
@str.7 = [18 x i8] c"Returned Properly\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.6, i64 0, i64 0))
%6 = tail call i64 @malloc(i32 1024)
%7 = inttoptr i64 %6 to i8*
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%11 = tail call i32 @fread(i8* %7, i32 1, i32 1024, i32* %10)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %11)
%13 = tail call i32 @fclose(i32* %10)
%14 = tail call i32 @vul(i8* %7, i32 %11)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.7, i64 0, i64 0))
ret i32 0
}