#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 */
scalar_t__ EBADF;
scalar_t__ EINTR;
int /*<<< orphan*/ EX_OSERR;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int close(int);
scalar_t__ errno;
void close_fd(int fd) {
  while (close(fd) == -1) {
    if (errno == EBADF) {
      return;
    } else if (errno == EINTR) {
      continue;
    } else {
      _exit(EX_OSERR);
    }
  }
}


x86 assembly (clang, -O0):
<close_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1188 <close_fd+0x68>
mov    0x2e9d(%rip),%rax
mov    (%rax),%rax
mov    0x2e83(%rip),%rcx
cmp    (%rcx),%rax
jne    115b <close_fd+0x3b>
jmp    1188 <close_fd+0x68>
mov    0x2e7e(%rip),%rax
mov    (%rax),%rax
mov    0x2e6c(%rip),%rcx
cmp    (%rcx),%rax
jne    117a <close_fd+0x5a>
jmp    112b <close_fd+0xb>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_fd>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2eaf(%rip),%r14
mov    0x2e98(%rip),%r15
mov    0x2e99(%rip),%r12
nop
mov    %ebx,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1168 <close_fd+0x48>
mov    (%r14),%rax
cmp    (%r15),%rax
je     1168 <close_fd+0x48>
cmp    (%r12),%rax
je     1140 <close_fd+0x20>
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@EBADF = global i64 0
@EINTR = global i64 0
@EX_OSERR = global i32 0
define void @close_fd(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
br label %3
3: ; preds = %16, %1
%4 = load i32, i32* %2
%5 = call i32 @close(i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %20
7: ; preds = %3
%8 = load i64, i64* @errno
%9 = load i64, i64* @EBADF
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %12
11: ; preds = %7
br label %20
12: ; preds = %7
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %17
16: ; preds = %12
br label %3
17: ; preds = %12
%18 = load i32, i32* @EX_OSERR
%19 = call i32 @_exit(i32 %18)
unreachable
20: ; preds = %11, %3
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EBADF = global i64 0
@EINTR = global i64 0
@EX_OSERR = global i32 0
define void @close_fd(i32 %0) {
br label %2
2: ; preds = %9, %1
%3 = tail call i32 @close(i32 %0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %15
5: ; preds = %2
%6 = load i64, i64* @errno
%7 = load i64, i64* @EBADF
%8 = icmp eq i64 %6, %7
br i1 %8, label %15, label %9
9: ; preds = %5
%10 = load i64, i64* @EINTR
%11 = icmp eq i64 %6, %10
br i1 %11, label %2, label %12
12: ; preds = %9
%13 = load i32, i32* @EX_OSERR
%14 = tail call i32 @_exit(i32 %13)
unreachable
15: ; preds = %5, %2
ret void
}