#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ system(char *);
void get_flag(char *buf) {
  FILE *f = fopen("/home/ctf/flag.txt", "r");
  if (!f) {
    printf("Couldn't open flag.txt file!\n");
    system("pwd");
    exit(0);
  }
  fgets(buf, 128, f);
  fclose(f);
}


x86 assembly (clang, -O0):
<get_flag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe8d(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ad <get_flag+0x4d>
lea    0xe80(%rip),%rdi
call   1050 <printf@plt>
lea    0xe92(%rip),%rdi
call   1040 <system@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x80,%esi
call   1060 <fgets@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_flag>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe92(%rip),%rdi
lea    0xe9e(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11a1 <get_flag+0x41>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <fclose@plt>
lea    0xe71(%rip),%rdi
call   1030 <puts@plt>
lea    0xe61(%rip),%rdi
call   1050 <system@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"/home/ctf/flag.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [30 x i8] c"Couldn't open flag.txt file!\0A\00"
@.str.3 = [4 x i8] c"pwd\00"
define void @get_flag(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
store i8* %0, i8** %2
%4 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %3
%5 = load i32*, i32** %3
%6 = icmp ne i32* %5, null
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%9 = call i32 @system(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%10 = call i32 @exit(i32 0)
unreachable
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = load i32*, i32** %3
%14 = call i32 @fgets(i8* %12, i32 128, i32* %13)
%15 = load i32*, i32** %3
%16 = call i32 @fclose(i32* %15)
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"/home/ctf/flag.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [4 x i8] c"pwd\00"
@str = [29 x i8] c"Couldn't open flag.txt file!\00"
define void @get_flag(i8* %0) {
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @system(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%7 = tail call i32 @exit(i32 0)
unreachable
8: ; preds = %1
%9 = tail call i32 @fgets(i8* %0, i32 128, i32* %2)
%10 = tail call i32 @fclose(i32* %2)
ret void
}