#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*/ close(int);
scalar_t__ errno;
int fd_close(int fd) {
  register unsigned int i = 0;
doit:
  if (!close(fd))
    return 0;
  i++;
  if (errno == EINTR)
    goto doit;
  return ((errno == EBADF) && (i > 1)) ? 0 : -1;
}


x86 assembly (clang, -O0):
<fd_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%eax
jne    113f <fd_close+0x2f>
movl   $0x0,-0x4(%rbp)
jmp    11a3 <fd_close+0x93>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    0x2e91(%rip),%rax
mov    (%rax),%rax
mov    0x2e7f(%rip),%rcx
cmp    (%rcx),%rax
jne    1167 <fd_close+0x57>
jmp    1122 <fd_close+0x12>
mov    0x2e72(%rip),%rax
mov    (%rax),%rcx
xor    %eax,%eax
mov    0x2e56(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0xd(%rbp)
jne    1190 <fd_close+0x80>
cmpl   $0x1,-0xc(%rbp)
seta   %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%dl
mov    $0xffffffff,%eax
xor    %ecx,%ecx
test   $0x1,%dl
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_close>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%ebp
xor    %r14d,%r14d
mov    0x2ebc(%rip),%r15
mov    0x2ead(%rip),%r12
xor    %ebx,%ebx
nopl   (%rax)
mov    %ebp,%edi
call   1030 <close@plt>
test   %eax,%eax
je     1163 <fd_close+0x53>
add    $0x1,%ebx
mov    (%r15),%rax
cmp    (%r12),%rax
je     1130 <fd_close+0x20>
mov    0x2e82(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
cmp    $0x2,%ebx
setb   %cl
or     %al,%cl
movzbl %cl,%r14d
neg    %r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EBADF = global i64 0
define i32 @fd_close(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %4
br label %5
5: ; preds = %16, %1
%6 = load i32, i32* %3
%7 = call i32 @close(i32 %6)
%8 = icmp ne i32 %7, 0
br i1 %8, label %10, label %9
9: ; preds = %5
store i32 0, i32* %2
br label %28
10: ; preds = %5
%11 = load i32, i32* %4
%12 = add i32 %11, 1
store i32 %12, i32* %4
%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 = %10
br label %5
17: ; preds = %10
%18 = load i64, i64* @errno
%19 = load i64, i64* @EBADF
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* %4
%23 = icmp ugt i32 %22, 1
br label %24
24: ; preds = %21, %17
%25 = phi i1 [ false, %17 ], [ %23, %21 ]
%26 = zext i1 %25 to i64
%27 = select i1 %25, i32 0, i32 -1
store i32 %27, i32* %2
br label %28
28: ; preds = %24, %9
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EBADF = global i64 0
define i32 @fd_close(i32 %0) {
br label %2
2: ; preds = %6, %1
%3 = phi i32 [ 0, %1 ], [ %7, %6 ]
%4 = tail call i32 @close(i32 %0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %17, label %6
6: ; preds = %2
%7 = add i32 %3, 1
%8 = load i64, i64* @errno
%9 = load i64, i64* @EINTR
%10 = icmp eq i64 %8, %9
br i1 %10, label %2, label %11
11: ; preds = %6
%12 = load i64, i64* @EBADF
%13 = icmp ne i64 %8, %12
%14 = icmp ult i32 %7, 2
%15 = select i1 %13, i1 true, i1 %14
%16 = sext i1 %15 to i32
br label %17
17: ; preds = %2, %11
%18 = phi i32 [ %16, %11 ], [ 0, %2 ]
ret i32 %18
}