#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, ...);
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);
  errno = 0;
  ret = close(999);
  printf("close 2: ret = %d, errno = %d, %s\n", ret, errno, strerrno(errno));
}


x86 assembly (clang, -O0):
<test_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e89(%rip),%rax
movl   $0x0,(%rax)
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
lea    0xe9b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11c6 <test_close+0x86>
mov    0x2e5a(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    -0xc(%rbp),%edi
lea    0xeaf(%rip),%rcx
lea    0xe87(%rip),%rax
cmp    $0x0,%edi
cmove  %rax,%rcx
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11e0 <test_close+0xa0>
mov    -0xc(%rbp),%esi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2df1(%rip),%rax
movl   $0x0,(%rax)
mov    $0x3e7,%edi
call   1050 <close@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   1030 <strerrno@plt>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    %rax,%rcx
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_close>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e8e(%rip),%rbp
movl   $0x0,0x0(%rbp)
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11a1 <test_close+0x61>
movl   $0x0,0x0(%rbp)
mov    %eax,%edi
call   1050 <close@plt>
mov    0x0(%rbp),%edx
test   %eax,%eax
lea    0xeaf(%rip),%rsi
lea    0xec9(%rip),%rcx
cmove  %rsi,%rcx
lea    0xe7a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11b2 <test_close+0x72>
mov    0x0(%rbp),%edx
lea    0xe8b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x0,0x0(%rbp)
mov    $0x3e7,%edi
call   1050 <close@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%ebp
mov    %ebp,%edi
call   1030 <strerrno@plt>
lea    0xe7f(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <printf@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
store i32 0, i32* @errno
%23 = call i32 @close(i32 999)
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = load i32, i32* @errno
%26 = load i32, i32* @errno
%27 = call i8* @strerrno(i32 %26)
%28 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i32 %24, i32 %25, i8* %27)
%29 = load i32, i32* %1
ret i32 %29
}

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
store i32 0, i32* @errno
%14 = tail call i32 @close(i32 999)
%15 = load i32, i32* @errno
%16 = tail call i8* @strerrno(i32 %15)
%17 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i32 %14, i32 %15, i8* %16)
ret i32 undef
}