#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 errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int handle_ptr(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
FILE *fopen_test(char *file, char *mode) {
  FILE *fp;
  int save_errno;
  int passed;
  printf("fopen(): file=%s, mode=\"%s\"\n",
         (((file) == NULL) ? "(null)" : (file)), mode);
  fflush(NULL);
  passed = handle_ptr(fp = fopen(file, mode));
  save_errno = errno;
  switch (passed) {
  case -1:
  case 0:
    printf("\t\tfp = 0x%p\n", fp);
    printf("\tSucceeded Phase 1\n");
    fflush(NULL);
    break;
  case -2:
    printf("\tFailed Phase 1: returned undefined value! "
           "fp = 0x%p\n",
           fp);
    fflush(NULL);
    break;
    break;
  default:
    printf("Internal syscalltester error: passed = %d\n", passed);
    fflush(NULL);
    break;
  }
  errno = save_errno;
  return fp;
}


x86 assembly (clang, -O0):
<fopen_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    116b <fopen_test+0x2b>
lea    0xebb(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    1173 <fopen_test+0x33>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe7e(%rip),%rdi
xor    %eax,%eax
mov    %eax,-0x30(%rbp)
call   1030 <printf@plt>
mov    -0x30(%rbp),%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <fopen@plt>
mov    %rax,%rdi
mov    %rdi,-0x18(%rbp)
call   1040 <handle_ptr@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0xfffffffe,%eax
je     1214 <fopen_test+0xd4>
jmp    11d2 <fopen_test+0x92>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
sub    $0x1,%eax
ja     1234 <fopen_test+0xf4>
jmp    11e6 <fopen_test+0xa6>
mov    -0x18(%rbp),%rsi
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    124e <fopen_test+0x10e>
mov    -0x18(%rbp),%rsi
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    124e <fopen_test+0x10e>
mov    -0x20(%rbp),%esi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2d88(%rip),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fopen_test>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rdi,%rdi
lea    0xeb9(%rip),%rsi
cmovne %rdi,%rsi
lea    0xe91(%rip),%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <handle_ptr@plt>
mov    0x2e43(%rip),%r14
mov    (%r14),%ebp
lea    0x1(%rax),%ecx
cmp    $0x2,%ecx
jae    11c7 <fopen_test+0x77>
lea    0xe75(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xed2(%rip),%rdi
call   1030 <puts@plt>
jmp    11ef <fopen_test+0x9f>
cmp    $0xfffffffe,%eax
jne    11df <fopen_test+0x8f>
lea    0xe5e(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11ef <fopen_test+0x9f>
lea    0xe81(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
mov    %ebp,(%r14)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"fopen(): file=%s, mode=\22%s\22\0A\00"
@.str.1 = [7 x i8] c"(null)\00"
@errno = global i32 0
@.str.2 = [13 x i8] c"\09\09fp = 0x%p\0A\00"
@.str.3 = [20 x i8] c"\09Succeeded Phase 1\0A\00"
@.str.4 = [54 x i8] c"\09Failed Phase 1: returned undefined value! fp = 0x%p\0A\00"
@.str.5 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
define i32* @fopen_test(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %3
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %2
br label %13
11: ; preds = %2
%12 = load i8*, i8** %3
br label %13
13: ; preds = %11, %10
%14 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %10 ], [ %12, %11 ]
%15 = load i8*, i8** %4
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %15)
%17 = call i32 @fflush(i32* null)
%18 = load i8*, i8** %3
%19 = load i8*, i8** %4
%20 = call i32* @fopen(i8* %18, i8* %19)
store i32* %20, i32** %5
%21 = call i32 @handle_ptr(i32* %20)
store i32 %21, i32* %7
%22 = load i32, i32* @errno
store i32 %22, i32* %6
%23 = load i32, i32* %7
switch i32 %23, label %33 [
i32 -1, label %24
i32 0, label %24
i32 -2, label %29
]
24: ; preds = %13, %13
%25 = load i32*, i32** %5
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32* %25)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @fflush(i32* null)
br label %37
29: ; preds = %13
%30 = load i32*, i32** %5
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0), i32* %30)
%32 = call i32 @fflush(i32* null)
br label %37
33: ; preds = %13
%34 = load i32, i32* %7
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %34)
%36 = call i32 @fflush(i32* null)
br label %37
37: ; preds = %33, %29, %24
%38 = load i32, i32* %6
store i32 %38, i32* @errno
%39 = load i32*, i32** %5
ret i32* %39
}

LLVM IR (-O2):
@.str = [29 x i8] c"fopen(): file=%s, mode=\22%s\22\0A\00"
@.str.1 = [7 x i8] c"(null)\00"
@errno = global i32 0
@.str.2 = [13 x i8] c"\09\09fp = 0x%p\0A\00"
@.str.4 = [54 x i8] c"\09Failed Phase 1: returned undefined value! fp = 0x%p\0A\00"
@.str.5 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
@str = [19 x i8] c"\09Succeeded Phase 1\00"
define i32* @fopen_test(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
%4 = select i1 %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %4, i8* %1)
%6 = tail call i32 @fflush(i32* null)
%7 = tail call i32* @fopen(i8* %0, i8* %1)
%8 = tail call i32 @handle_ptr(i32* %7)
%9 = load i32, i32* @errno
switch i32 %8, label %15 [
i32 -1, label %10
i32 0, label %10
i32 -2, label %13
]
10: ; preds = %2, %2
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32* %7)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %17
13: ; preds = %2
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0), i32* %7)
br label %17
15: ; preds = %2
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %8)
br label %17
17: ; preds = %15, %13, %10
%18 = tail call i32 @fflush(i32* null)
store i32 %9, i32* @errno
ret i32* %7
}