#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 close(int);
int errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, int, ...);
int /*<<< orphan*/ stop();
char *strerrno(int);
int test_close() {
  int fd, ret;
  errno = 0;
  fd = open("foo.fileio.test", O_RDONLY);
  if (fd >= 0) {
    errno = 0;
    ret = close(fd);
    printf("close 1: ret = %d, errno = %d, %s\n", ret, errno,
           ret == 0 ? "OK" : "");
  } else
    printf("close 1: ret = %d, errno = %d\n", ret, errno);
  stop();
  errno = 0;
  ret = close(999);
  printf("close 2: ret = %d, errno = %d, %s\n", ret, errno, strerrno(errno));
  stop();
}


x86 assembly (clang, -O0):
<test_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e79(%rip),%rax
movl   $0x0,(%rax)
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0xe8b(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11d6 <test_close+0x86>
mov    0x2e4a(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
mov    -0xc(%rbp),%edi
lea    0xe9f(%rip),%rcx
lea    0xe77(%rip),%rax
cmp    $0x0,%edi
cmove  %rax,%rcx
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f0 <test_close+0xa0>
mov    -0xc(%rbp),%esi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <stop@plt>
mov    0x2dda(%rip),%rax
movl   $0x0,(%rax)
mov    $0x3e7,%edi
call   1060 <close@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   1030 <strerrno@plt>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    %rax,%rcx
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <stop@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_close>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e7e(%rip),%rbp
movl   $0x0,0x0(%rbp)
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11b1 <test_close+0x61>
movl   $0x0,0x0(%rbp)
mov    %eax,%edi
call   1060 <close@plt>
mov    0x0(%rbp),%edx
test   %eax,%eax
lea    0xe9f(%rip),%rsi
lea    0xeb9(%rip),%rcx
cmove  %rsi,%rcx
lea    0xe6a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11c2 <test_close+0x72>
mov    0x0(%rbp),%edx
lea    0xe7b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <stop@plt>
movl   $0x0,0x0(%rbp)
mov    $0x3e7,%edi
call   1060 <close@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%ebp
mov    %ebp,%edi
call   1030 <strerrno@plt>
lea    0xe68(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <stop@plt>

LLVM IR (-O0):
@errno = global i32 0
@.str = [16 x i8] c"foo.fileio.test\00"
@O_RDONLY = global i32 0
@.str.1 = [35 x i8] c"close 1: ret = %d, errno = %d, %s\0A\00"
@.str.2 = [3 x i8] c"OK\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [31 x i8] c"close 1: ret = %d, errno = %d\0A\00"
@.str.5 = [35 x i8] c"close 2: ret = %d, errno = %d, %s\0A\00"
define i32 @test_close() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* @errno
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp sge i32 %6, 0
br i1 %7, label %8, label %18
8: ; preds = %0
store i32 0, i32* @errno
%9 = load i32, i32* %2
%10 = call i32 @close(i32 %9)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = load i32, i32* @errno
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, 0
%15 = zext i1 %14 to i64
%16 = select i1 %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0)
%17 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %12, i8* %16)
br label %22
18: ; preds = %0
%19 = load i32, i32* %3
%20 = load i32, i32* @errno
%21 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %19, i32 %20)
br label %22
22: ; preds = %18, %8
%23 = call i32 (...) @stop()
store i32 0, i32* @errno
%24 = call i32 @close(i32 999)
store i32 %24, i32* %3
%25 = load i32, i32* %3
%26 = load i32, i32* @errno
%27 = load i32, i32* @errno
%28 = call i8* @strerrno(i32 %27)
%29 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i32 %25, i32 %26, i8* %28)
%30 = call i32 (...) @stop()
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [16 x i8] c"foo.fileio.test\00"
@O_RDONLY = global i32 0
@.str.1 = [35 x i8] c"close 1: ret = %d, errno = %d, %s\0A\00"
@.str.2 = [3 x i8] c"OK\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [31 x i8] c"close 1: ret = %d, errno = %d\0A\00"
@.str.5 = [35 x i8] c"close 2: ret = %d, errno = %d, %s\0A\00"
define i32 @test_close() {
store i32 0, i32* @errno
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp sgt i32 %2, -1
br i1 %3, label %4, label %10
4: ; preds = %0
store i32 0, i32* @errno
%5 = tail call i32 @close(i32 %2)
%6 = load i32, i32* @errno
%7 = icmp eq i32 %5, 0
%8 = select i1 %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0)
%9 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %5, i32 %6, i8* %8)
br label %13
10: ; preds = %0
%11 = load i32, i32* @errno
%12 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 undef, i32 %11)
br label %13
13: ; preds = %10, %4
%14 = tail call i32 (...) @stop()
store i32 0, i32* @errno
%15 = tail call i32 @close(i32 999)
%16 = load i32, i32* @errno
%17 = tail call i8* @strerrno(i32 %16)
%18 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i32 %15, i32 %16, i8* %17)
%19 = tail call i32 (...) @stop()
ret i32 undef
}