#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 EINPROGRESS;
int EINTR;
int close(int);
int errno;
int sjs__close(int fd) {
  int saved_errno;
  int r;
  saved_errno = errno;
  r = close(fd);
  if (r == -1) {
    r = -errno;
    if (r == -EINTR || r == -EINPROGRESS) {
      r = 0;
    }
    errno = saved_errno;
  }
  return r;
}


x86 assembly (clang, -O0):
<sjs__close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2eb6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <close@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1189 <sjs__close+0x79>
xor    %eax,%eax
mov    0x2e93(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
xor    %ecx,%ecx
mov    0x2e7a(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
je     1176 <sjs__close+0x66>
mov    -0xc(%rbp),%eax
xor    %ecx,%ecx
mov    0x2e7c(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    117d <sjs__close+0x6d>
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%ecx
mov    0x2e51(%rip),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sjs__close>:
push   %rbp
push   %rbx
push   %rax
mov    0x2ebe(%rip),%rbx
mov    (%rbx),%ebp
call   1030 <close@plt>
cmp    $0xffffffff,%eax
jne    1146 <sjs__close+0x36>
mov    (%rbx),%eax
mov    0x2ea1(%rip),%rcx
cmp    %eax,(%rcx)
je     113e <sjs__close+0x2e>
mov    0x2eae(%rip),%rcx
cmp    %eax,(%rcx)
jne    1142 <sjs__close+0x32>
xor    %eax,%eax
jmp    1144 <sjs__close+0x34>
neg    %eax
mov    %ebp,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@EINTR = global i32 0
@EINPROGRESS = global i32 0
define i32 @sjs__close(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* @errno
store i32 %5, i32* %3
%6 = load i32, i32* %2
%7 = call i32 @close(i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %25
10: ; preds = %1
%11 = load i32, i32* @errno
%12 = sub nsw i32 0, %11
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = load i32, i32* @EINTR
%15 = sub nsw i32 0, %14
%16 = icmp eq i32 %13, %15
br i1 %16, label %22, label %17
17: ; preds = %10
%18 = load i32, i32* %4
%19 = load i32, i32* @EINPROGRESS
%20 = sub nsw i32 0, %19
%21 = icmp eq i32 %18, %20
br i1 %21, label %22, label %23
22: ; preds = %17, %10
store i32 0, i32* %4
br label %23
23: ; preds = %22, %17
%24 = load i32, i32* %3
store i32 %24, i32* @errno
br label %25
25: ; preds = %23, %1
%26 = load i32, i32* %4
ret i32 %26
}

LLVM IR (-O2):
@errno = global i32 0
@EINTR = global i32 0
@EINPROGRESS = global i32 0
define i32 @sjs__close(i32 %0) {
%2 = load i32, i32* @errno
%3 = tail call i32 @close(i32 %0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %16
5: ; preds = %1
%6 = load i32, i32* @errno
%7 = load i32, i32* @EINTR
%8 = icmp eq i32 %7, %6
br i1 %8, label %13, label %9
9: ; preds = %5
%10 = sub nsw i32 0, %6
%11 = load i32, i32* @EINPROGRESS
%12 = icmp eq i32 %11, %6
br i1 %12, label %13, label %14
13: ; preds = %9, %5
br label %14
14: ; preds = %13, %9
%15 = phi i32 [ 0, %13 ], [ %10, %9 ]
store i32 %2, i32* @errno
br label %16
16: ; preds = %14, %1
%17 = phi i32 [ %15, %14 ], [ %3, %1 ]
ret i32 %17
}