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


x86 assembly (clang, -O0):
<fclose_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xea9(%rip),%rdi
xor    %eax,%eax
mov    %eax,-0x1c(%rbp)
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,%edi
mov    %edi,-0xc(%rbp)
call   1060 <handle_eof@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0xfffffffe,%eax
je     11e2 <fclose_test+0xa2>
jmp    11a1 <fclose_test+0x61>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
sub    $0x1,%eax
ja     1201 <fclose_test+0xc1>
jmp    11b5 <fclose_test+0x75>
mov    -0xc(%rbp),%esi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    121b <fclose_test+0xdb>
mov    -0xc(%rbp),%esi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    121b <fclose_test+0xdb>
mov    -0x14(%rbp),%esi
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0x10(%rbp),%ecx
mov    0x2dbb(%rip),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fclose_test>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
lea    0xea2(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1070 <handle_eof@plt>
mov    0x2e59(%rip),%r14
mov    (%r14),%ebp
lea    0x1(%rax),%ecx
cmp    $0x2,%ecx
jae    11b0 <fclose_test+0x60>
lea    0xe7a(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xed6(%rip),%rdi
call   1030 <puts@plt>
jmp    11d0 <fclose_test+0x80>
cmp    $0xfffffffe,%eax
jne    11c0 <fclose_test+0x70>
lea    0xe63(%rip),%rdi
mov    %ebx,%esi
jmp    11c9 <fclose_test+0x79>
lea    0xe8d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
mov    %ebp,(%r14)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"fclose(): fp=0x%p\0A\00"
@errno = global i32 0
@.str.1 = [12 x i8] c"\09\09ret = %d\0A\00"
@.str.2 = [20 x i8] c"\09Succeeded Phase 1\0A\00"
@.str.3 = [53 x i8] c"\09Failed Phase 1: returned undefined value! ret = %d\0A\00"
@.str.4 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
define i32 @fclose_test(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %2
%6 = load i32*, i32** %2
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32* %6)
%8 = call i32 @fflush(i32* null)
%9 = load i32*, i32** %2
%10 = call i32 @fclose(i32* %9)
store i32 %10, i32* %3
%11 = call i32 @handle_eof(i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* @errno
store i32 %12, i32* %4
%13 = load i32, i32* %5
switch i32 %13, label %23 [
i32 -1, label %14
i32 0, label %14
i32 -2, label %19
]
14: ; preds = %1, %1
%15 = load i32, i32* %3
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @fflush(i32* null)
br label %27
19: ; preds = %1
%20 = load i32, i32* %3
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = call i32 @fflush(i32* null)
br label %27
23: ; preds = %1
%24 = load i32, i32* %5
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%26 = call i32 @fflush(i32* null)
br label %27
27: ; preds = %23, %19, %14
%28 = load i32, i32* %4
store i32 %28, i32* @errno
%29 = load i32, i32* %3
ret i32 %29
}

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