#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 errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
int test_close(void *);
int run_close_test(void *f, int expected_errno, const char *condition) {
  int err;
  printf("testing close()...");
  printf("%s", condition);
  printf("...");
  err = test_close(f);
  if (err < 0) {
    if (expected_errno == 0) {
      fflush(stdout);
      perror("unexpected error");
      return -1;
    } else if (errno == expected_errno) {
      printf("passed\n");
      return -1;
    }
  } else {
    if (expected_errno == 0) {
      printf("passed\n");
      return 0;
    } else {
      printf("should have failed with %d\n", expected_errno);
      return 0;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<run_close_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xea7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <test_close@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11fd <run_close_test+0xbd>
cmpl   $0x0,-0x14(%rbp)
jne    11c7 <run_close_test+0x87>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
lea    0xe60(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1245 <run_close_test+0x105>
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
cmp    -0x14(%rbp),%eax
jne    11f3 <run_close_test+0xb3>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1245 <run_close_test+0x105>
jmp    11f8 <run_close_test+0xb8>
jmp    123e <run_close_test+0xfe>
cmpl   $0x0,-0x14(%rbp)
jne    1221 <run_close_test+0xe1>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1245 <run_close_test+0x105>
mov    -0x14(%rbp),%esi
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1245 <run_close_test+0x105>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_close_test>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0xe9c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xea1(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe8c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1050 <test_close@plt>
test   %eax,%eax
js     11b0 <run_close_test+0x60>
test   %r14d,%r14d
je     11d4 <run_close_test+0x84>
lea    0xe85(%rip),%rdi
xor    %ebp,%ebp
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11e2 <run_close_test+0x92>
test   %r14d,%r14d
je     11e9 <run_close_test+0x99>
mov    0x2e24(%rip),%rax
mov    $0xffffffff,%ebp
cmp    %r14d,(%rax)
jne    11e2 <run_close_test+0x92>
lea    0xe76(%rip),%rdi
call   1030 <puts@plt>
jmp    11e2 <run_close_test+0x92>
lea    0xe68(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
lea    0xe18(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11e2 <run_close_test+0x92>

LLVM IR (-O0):
@.str = [19 x i8] c"testing close()...\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [4 x i8] c"...\00"
@stdout = global i32 0
@.str.3 = [17 x i8] c"unexpected error\00"
@errno = global i32 0
@.str.4 = [8 x i8] c"passed\0A\00"
@.str.5 = [28 x i8] c"should have failed with %d\0A\00"
define i32 @run_close_test(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%10 = load i8*, i8** %7
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%13 = load i8*, i8** %5
%14 = call i32 @test_close(i8* %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %32
17: ; preds = %3
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* @stdout
%22 = call i32 @fflush(i32 %21)
%23 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %41
24: ; preds = %17
%25 = load i32, i32* @errno
%26 = load i32, i32* %6
%27 = icmp eq i32 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %4
br label %41
30: ; preds = %24
br label %31
31: ; preds = %30
br label %40
32: ; preds = %3
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %4
br label %41
37: ; preds = %32
%38 = load i32, i32* %6
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %38)
store i32 0, i32* %4
br label %41
40: ; preds = %31
store i32 -1, i32* %4
br label %41
41: ; preds = %40, %37, %35, %28, %20
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
@.str = [19 x i8] c"testing close()...\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [4 x i8] c"...\00"
@stdout = global i32 0
@.str.3 = [17 x i8] c"unexpected error\00"
@errno = global i32 0
@.str.5 = [28 x i8] c"should have failed with %d\0A\00"
@str.6 = [7 x i8] c"passed\00"
define i32 @run_close_test(i8* %0, i32 %1, i8* %2) {
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @test_close(i8* %0)
%8 = icmp slt i32 %7, 0
%9 = icmp eq i32 %1, 0
br i1 %8, label %10, label %20
10: ; preds = %3
br i1 %9, label %11, label %15
11: ; preds = %10
%12 = load i32, i32* @stdout
%13 = tail call i32 @fflush(i32 %12)
%14 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %25
15: ; preds = %10
%16 = load i32, i32* @errno
%17 = icmp eq i32 %16, %1
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.6, i64 0, i64 0))
br label %25
20: ; preds = %3
br i1 %9, label %21, label %23
21: ; preds = %20
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.6, i64 0, i64 0))
br label %25
23: ; preds = %20
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %1)
br label %25
25: ; preds = %15, %23, %21, %18, %11
%26 = phi i32 [ -1, %11 ], [ -1, %18 ], [ 0, %21 ], [ 0, %23 ], [ -1, %15 ]
ret i32 %26
}