#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__ EINTR;
int close(int);
scalar_t__ errno;
void close_fd(int *fd) {
  if (*fd != -1) {
    int err;
    do {
      err = close(*fd);
    } while (err == -1 && errno == EINTR);
    *fd = -1;
  }
}


x86 assembly (clang, -O0):
<close_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
cmpl   $0xffffffff,(%rax)
je     117a <close_fd+0x6a>
jmp    112e <close_fd+0x1e>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0xc(%rbp)
mov    %al,-0xd(%rbp)
jne    1165 <close_fd+0x55>
mov    0x2e8e(%rip),%rax
mov    (%rax),%rax
mov    0x2e7c(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    112e <close_fd+0x1e>
mov    -0x8(%rbp),%rax
movl   $0xffffffff,(%rax)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_fd>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    (%rdi),%edi
cmp    $0xffffffff,%edi
je     115a <close_fd+0x4a>
call   1030 <close@plt>
cmp    $0xffffffff,%eax
jne    1154 <close_fd+0x44>
mov    0x2eb0(%rip),%r14
mov    0x2ea1(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    (%r15),%rax
cmp    %rax,(%r14)
jne    1154 <close_fd+0x44>
mov    (%rbx),%edi
call   1030 <close@plt>
cmp    $0xffffffff,%eax
je     1140 <close_fd+0x30>
movl   $0xffffffff,(%rbx)
pop    %rbx
pop    %r14
pop    %r15
ret

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

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