#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 close(int);
int /*<<< orphan*/ dropbear_exit(char *, int, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ strerror(scalar_t__);
void m_close(int fd) {
  int val;
  do {
    val = close(fd);
  } while (val < 0 && errno == EINTR);
  if (val < 0 && errno != EBADF) {
    dropbear_exit("Error closing fd %d, %s", fd, strerror(errno));
  }
}


x86 assembly (clang, -O0):
<m_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <close@plt>
mov    %eax,-0x8(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x8(%rbp)
mov    %al,-0x9(%rbp)
jge    116f <m_close+0x3f>
mov    0x2e84(%rip),%rax
mov    (%rax),%rax
mov    0x2e72(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x9(%rbp)
mov    -0x9(%rbp),%al
test   $0x1,%al
jne    113b <m_close+0xb>
cmpl   $0x0,-0x8(%rbp)
jge    11c4 <m_close+0x94>
mov    0x2e55(%rip),%rax
mov    (%rax),%rax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%rax
je     11c4 <m_close+0x94>
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    0xe41(%rip),%rdi
call   1040 <dropbear_exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<m_close>:
push   %r15
push   %r14
push   %rbx
mov    %edi,%ebx
mov    0x2ea2(%rip),%r14
mov    0x2e93(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
call   1030 <close@plt>
mov    (%r14),%rdi
test   %eax,%eax
jns    1163 <m_close+0x33>
cmp    (%r15),%rdi
je     1150 <m_close+0x20>
test   %eax,%eax
jns    118d <m_close+0x5d>
mov    0x2e62(%rip),%rax
cmp    (%rax),%rdi
je     118d <m_close+0x5d>
call   1050 <strerror@plt>
lea    0xe81(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <dropbear_exit@plt>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EBADF = global i64 0
@.str = [24 x i8] c"Error closing fd %d, %s\00"
define void @m_close(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
br label %4
4: ; preds = %14, %1
%5 = load i32, i32* %2
%6 = call i32 @close(i32 %5)
store i32 %6, i32* %3
br label %7
7: ; preds = %4
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %7
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br label %14
14: ; preds = %10, %7
%15 = phi i1 [ false, %7 ], [ %13, %10 ]
br i1 %15, label %4, label %16
16: ; preds = %14
%17 = load i32, i32* %3
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %16
%20 = load i64, i64* @errno
%21 = load i64, i64* @EBADF
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = load i32, i32* %2
%25 = load i64, i64* @errno
%26 = call i32 @strerror(i64 %25)
%27 = call i32 @dropbear_exit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %26)
br label %28
28: ; preds = %23, %19, %16
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EBADF = global i64 0
@.str = [24 x i8] c"Error closing fd %d, %s\00"
define void @m_close(i32 %0) {
br label %2
2: ; preds = %2, %1
%3 = tail call i32 @close(i32 %0)
%4 = icmp slt i32 %3, 0
%5 = load i64, i64* @errno
%6 = load i64, i64* @EINTR
%7 = icmp eq i64 %5, %6
%8 = select i1 %4, i1 %7, i1 false
br i1 %8, label %2, label %9
9: ; preds = %2
%10 = xor i1 %4, true
%11 = load i64, i64* @EBADF
%12 = icmp eq i64 %5, %11
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = tail call i32 @strerror(i64 %5)
%16 = tail call i32 @dropbear_exit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %15)
br label %17
17: ; preds = %14, %9
ret void
}