#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*/ O_RDONLY;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, unsigned char *, int);
void printflag() {
  int fd;
  int len;
  unsigned char data[128];
  fd = open("flag", O_RDONLY);
  if (fd <= 0) {
    printf("Failed to open flag.\n");
    return;
  }
  len = lseek(fd, 0, SEEK_END);
  lseek(fd, 0, SEEK_SET);
  if (len > 128) {
    len = 128;
  }
  memset(data, 0, 128);
  read(fd, data, len);
  close(fd);
  printf("%s\n", data);
  return;
}


x86 assembly (clang, -O0):
<printflag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0xe85(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jg     11a0 <printflag+0x40>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    121f <printflag+0xbf>
mov    -0x4(%rbp),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmpl   $0x80,-0x8(%rbp)
jle    11dd <printflag+0x7d>
movl   $0x80,-0x8(%rbp)
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1070 <read@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
lea    -0x90(%rbp),%rsi
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printflag>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0xe85(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
jle    11f3 <printflag+0x93>
mov    %eax,%ebx
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,%ebp
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0x80,%ebp
mov    $0x80,%eax
cmovge %eax,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1070 <read@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe0b(%rip),%rdi
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"flag\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"Failed to open flag.\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [4 x i8] c"%s\0A\00"
define void @printflag() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [128 x i8]
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %1
%6 = load i32, i32* %1
%7 = icmp sle i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %31
10: ; preds = %0
%11 = load i32, i32* %1
%12 = load i32, i32* @SEEK_END
%13 = call i32 @lseek(i32 %11, i32 0, i32 %12)
store i32 %13, i32* %2
%14 = load i32, i32* %1
%15 = load i32, i32* @SEEK_SET
%16 = call i32 @lseek(i32 %14, i32 0, i32 %15)
%17 = load i32, i32* %2
%18 = icmp sgt i32 %17, 128
br i1 %18, label %19, label %20
19: ; preds = %10
store i32 128, i32* %2
br label %20
20: ; preds = %19, %10
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%22 = call i32 @memset(i8* %21, i32 0, i32 128)
%23 = load i32, i32* %1
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%25 = load i32, i32* %2
%26 = call i32 @read(i32 %23, i8* %24, i32 %25)
%27 = load i32, i32* %1
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %29)
br label %31
31: ; preds = %20, %8
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"flag\00"
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@str = [21 x i8] c"Failed to open flag.\00"
define void @printflag() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %19
8: ; preds = %0
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @lseek(i32 %4, i32 0, i32 %9)
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @lseek(i32 %4, i32 0, i32 %11)
%13 = icmp slt i32 %10, 128
%14 = select i1 %13, i32 %10, i32 128
%15 = call i32 @memset(i8* %2, i32 0, i32 128)
%16 = call i32 @read(i32 %4, i8* %2, i32 %14)
%17 = call i32 @close(i32 %4)
%18 = call i32 @puts(i8* %2)
br label %19
19: ; preds = %8, %6
ret void
}