#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 ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ O_RDONLY;
int close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *getenv(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int print_pf_key_and_read_protected_file(char *who) {
  char *secret = getenv("SECRET_PROVISION_SECRET_STRING");
  if (!secret) {
    fprintf(stderr, "did not receive protected files master key!\n");
    return -1;
  }
  printf("--- [%s] Received protected files master key = '%s' ---\n", who,
         secret);
  int fd = open("files/input.txt", O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "[error] cannot open '"
                    "files/input.txt"
                    "'\n");
    return -1;
  }
  char buf[1024] = {0};
  ssize_t bytes_read = 0;
  while (1) {
    ssize_t ret = read(fd, buf + bytes_read, sizeof(buf) - bytes_read);
    if (ret > 0) {
      bytes_read += ret;
    } else if (ret == 0) {
      break;
    } else if (errno == EAGAIN || errno == EINTR) {
      continue;
    } else {
      fprintf(stderr, "[error] cannot read '"
                      "files/input.txt"
                      "'\n");
      close(fd);
      return -1;
    }
  }
  int ret = close(fd);
  if (ret < 0) {
    fprintf(stderr, "[error] cannot close '"
                    "files/input.txt"
                    "'\n");
    return -1;
  }
  printf("--- [%s] Read from protected file: '%s' ---\n", who, buf);
  return 0;
}


x86 assembly (clang, -O0):
<print_pf_key_and_read_protected_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe7a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11bb <print_pf_key_and_read_protected_file+0x4b>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    0xe75(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1356 <print_pf_key_and_read_protected_file+0x1e6>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe82(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    0xea6(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1212 <print_pf_key_and_read_protected_file+0xa2>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xe94(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1356 <print_pf_key_and_read_protected_file+0x1e6>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
movl   $0x0,-0x424(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x420(%rbp),%rsi
movslq -0x424(%rbp),%rax
add    %rax,%rsi
movslq -0x424(%rbp),%rcx
mov    $0x400,%eax
sub    %rcx,%rax
mov    %eax,%edx
call   1070 <read@plt>
mov    %eax,-0x428(%rbp)
cmpl   $0x0,-0x428(%rbp)
jle    1283 <print_pf_key_and_read_protected_file+0x113>
mov    -0x428(%rbp),%eax
add    -0x424(%rbp),%eax
mov    %eax,-0x424(%rbp)
jmp    12f7 <print_pf_key_and_read_protected_file+0x187>
cmpl   $0x0,-0x428(%rbp)
jne    1295 <print_pf_key_and_read_protected_file+0x125>
jmp    12fc <print_pf_key_and_read_protected_file+0x18c>
mov    0x2d34(%rip),%rax
mov    (%rax),%rax
mov    0x2d12(%rip),%rcx
cmp    (%rcx),%rax
je     12c9 <print_pf_key_and_read_protected_file+0x159>
mov    0x2d1a(%rip),%rax
mov    (%rax),%rax
mov    0x2d08(%rip),%rcx
cmp    (%rcx),%rax
jne    12ce <print_pf_key_and_read_protected_file+0x15e>
jmp    122f <print_pf_key_and_read_protected_file+0xbf>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
lea    0xdde(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1356 <print_pf_key_and_read_protected_file+0x1e6>
jmp    122f <print_pf_key_and_read_protected_file+0xbf>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    %eax,-0x42c(%rbp)
cmpl   $0x0,-0x42c(%rbp)
jge    1338 <print_pf_key_and_read_protected_file+0x1c8>
mov    0x2cba(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1356 <print_pf_key_and_read_protected_file+0x1e6>
mov    -0x10(%rbp),%rsi
lea    -0x420(%rbp),%rdx
lea    0xdc1(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_pf_key_and_read_protected_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%rbx
lea    0xe75(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1248 <print_pf_key_and_read_protected_file+0xd8>
lea    0xeac(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <printf@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
lea    0xeca(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     125a <print_pf_key_and_read_protected_file+0xea>
mov    %eax,%r15d
mov    %rbx,0x8(%rsp)
lea    0x10(%rsp),%rdi
xor    %ebx,%ebx
mov    $0x400,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x2de6(%rip),%r13
mov    0x2dc7(%rip),%r14
mov    0x2dd0(%rip),%r12
mov    %ebx,0x4(%rsp)
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rbx
add    $0x10,%rbx
mov    $0x400,%ebp
sub    %eax,%ebp
xchg   %ax,%ax
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1070 <read@plt>
test   %eax,%eax
jg     1240 <print_pf_key_and_read_protected_file+0xd0>
je     126c <print_pf_key_and_read_protected_file+0xfc>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
je     1210 <print_pf_key_and_read_protected_file+0xa0>
cmp    (%r12),%rax
je     1210 <print_pf_key_and_read_protected_file+0xa0>
jmp    1292 <print_pf_key_and_read_protected_file+0x122>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x4(%rsp),%ebx
add    %eax,%ebx
jmp    11f8 <print_pf_key_and_read_protected_file+0x88>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
jmp    12c1 <print_pf_key_and_read_protected_file+0x151>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
jmp    12c1 <print_pf_key_and_read_protected_file+0x151>
mov    %r15d,%edi
call   1060 <close@plt>
test   %eax,%eax
js     12b1 <print_pf_key_and_read_protected_file+0x141>
lea    0xe8c(%rip),%rdi
lea    0x10(%rsp),%rdx
mov    0x8(%rsp),%rsi
call   1040 <printf@plt>
xor    %eax,%eax
jmp    12cb <print_pf_key_and_read_protected_file+0x15b>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
call   1080 <fprintf@plt>
mov    %r15d,%edi
call   1060 <close@plt>
jmp    12c6 <print_pf_key_and_read_protected_file+0x156>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"SECRET_PROVISION_SECRET_STRING\00"
@stderr = global i32 0
@.str.1 = [45 x i8] c"did not receive protected files master key!\0A\00"
@.str.2 = [57 x i8] c"--- [%s] Received protected files master key = '%s' ---\0A\00"
@.str.3 = [16 x i8] c"files/input.txt\00"
@O_RDONLY = global i32 0
@.str.4 = [39 x i8] c"[error] cannot open 'files/input.txt'\0A\00"
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
@.str.5 = [39 x i8] c"[error] cannot read 'files/input.txt'\0A\00"
@.str.6 = [40 x i8] c"[error] cannot close 'files/input.txt'\0A\00"
@.str.7 = [45 x i8] c"--- [%s] Read from protected file: '%s' ---\0A\00"
define i32 @print_pf_key_and_read_protected_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
%10 = call i8* @getenv(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %77
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = load i8*, i8** %4
%19 = call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), i8* %17, i8* %18)
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %20)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %77
27: ; preds = %16
%28 = bitcast [1024 x i8]* %6 to i8*
store i32 0, i32* %7
br label %29
29: ; preds = %27, %58, %64
%30 = load i32, i32* %5
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%32 = load i32, i32* %7
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = sub i64 1024, %36
%38 = trunc i64 %37 to i32
%39 = call i32 @read(i32 %30, i8* %34, i32 %38)
store i32 %39, i32* %8
%40 = load i32, i32* %8
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %29
%43 = load i32, i32* %8
%44 = load i32, i32* %7
%45 = add nsw i32 %44, %43
store i32 %45, i32* %7
br label %64
46: ; preds = %29
%47 = load i32, i32* %8
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %46
br label %65
50: ; preds = %46
%51 = load i64, i64* @errno
%52 = load i64, i64* @EAGAIN
%53 = icmp eq i64 %51, %52
br i1 %53, label %58, label %54
54: ; preds = %50
%55 = load i64, i64* @errno
%56 = load i64, i64* @EINTR
%57 = icmp eq i64 %55, %56
br i1 %57, label %58, label %59
58: ; preds = %54, %50
br label %29
59: ; preds = %54
%60 = load i32, i32* @stderr
%61 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
%62 = load i32, i32* %5
%63 = call i32 @close(i32 %62)
store i32 -1, i32* %2
br label %77
64: ; preds = %42
br label %29
65: ; preds = %49
%66 = load i32, i32* %5
%67 = call i32 @close(i32 %66)
store i32 %67, i32* %9
%68 = load i32, i32* %9
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %65
%71 = load i32, i32* @stderr
%72 = call i32 @fprintf(i32 %71, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %2
br label %77
73: ; preds = %65
%74 = load i8*, i8** %3
%75 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%76 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.7, i64 0, i64 0), i8* %74, i8* %75)
store i32 0, i32* %2
br label %77
77: ; preds = %73, %70, %59, %24, %13
%78 = load i32, i32* %2
ret i32 %78
}

LLVM IR (-O2):
@.str = [31 x i8] c"SECRET_PROVISION_SECRET_STRING\00"
@stderr = global i32 0
@.str.1 = [45 x i8] c"did not receive protected files master key!\0A\00"
@.str.2 = [57 x i8] c"--- [%s] Received protected files master key = '%s' ---\0A\00"
@.str.3 = [16 x i8] c"files/input.txt\00"
@O_RDONLY = global i32 0
@.str.4 = [39 x i8] c"[error] cannot open 'files/input.txt'\0A\00"
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
@.str.5 = [39 x i8] c"[error] cannot read 'files/input.txt'\0A\00"
@.str.6 = [40 x i8] c"[error] cannot close 'files/input.txt'\0A\00"
@.str.7 = [45 x i8] c"--- [%s] Read from protected file: '%s' ---\0A\00"
define i32 @print_pf_key_and_read_protected_file(i8* %0) {
%2 = alloca [1024 x i8]
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
br label %51
8: ; preds = %1
%9 = tail call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %3)
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
br label %51
16: ; preds = %8
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %18
18: ; preds = %26, %16
%19 = phi i32 [ %27, %26 ], [ 0, %16 ]
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %20
%22 = sub i32 1024, %19
br label %23
23: ; preds = %18, %30
%24 = call i32 @read(i32 %11, i8* %21, i32 %22)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = add nsw i32 %24, %19
br label %18
28: ; preds = %23
%29 = icmp eq i32 %24, 0
br i1 %29, label %41, label %30
30: ; preds = %28
%31 = load i64, i64* @errno
%32 = load i64, i64* @EAGAIN
%33 = icmp eq i64 %31, %32
%34 = load i64, i64* @EINTR
%35 = icmp eq i64 %31, %34
%36 = select i1 %33, i1 true, i1 %35
br i1 %36, label %23, label %37
37: ; preds = %30
%38 = load i32, i32* @stderr
%39 = tail call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
%40 = tail call i32 @close(i32 %11)
br label %49
41: ; preds = %28
%42 = tail call i32 @close(i32 %11)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = load i32, i32* @stderr
%46 = tail call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0))
br label %49
47: ; preds = %41
%48 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.7, i64 0, i64 0), i8* %0, i8* %17)
br label %49
49: ; preds = %37, %44, %47
%50 = phi i32 [ -1, %44 ], [ 0, %47 ], [ -1, %37 ]
br label %51
51: ; preds = %13, %49, %5
%52 = phi i32 [ -1, %5 ], [ -1, %13 ], [ %50, %49 ]
ret i32 %52
}