#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__ ENOTCONN;
int /*<<< orphan*/ SHUT_RDWR;
int /*<<< orphan*/ adbi_assure(int);
scalar_t__ close(int);
scalar_t__ errno;
scalar_t__ shutdown(int, int /*<<< orphan*/);
void close_socket(int sock) {
  if (shutdown(sock, SHUT_RDWR) < 0) {
    adbi_assure(errno == ENOTCONN);
  }
  while (close(sock)) {
    adbi_assure(errno != EBADF);
  }
}


x86 assembly (clang, -O0):
<close_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
call   1030 <shutdown@plt>
cmp    $0x0,%rax
jge    1177 <close_socket+0x47>
mov    0x2e73(%rip),%rax
mov    (%rax),%rax
mov    0x2e79(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <adbi_assure@plt>
jmp    117c <close_socket+0x4c>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     11b4 <close_socket+0x84>
mov    0x2e3b(%rip),%rax
mov    (%rax),%rax
mov    0x2e29(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <adbi_assure@plt>
jmp    117c <close_socket+0x4c>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_socket>:
push   %r15
push   %r14
push   %rbx
mov    %edi,%ebx
mov    0x2eb2(%rip),%rax
mov    (%rax),%esi
call   1030 <shutdown@plt>
test   %rax,%rax
jns    1169 <close_socket+0x39>
mov    0x2e7f(%rip),%rax
mov    (%rax),%rax
mov    0x2e85(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%rax
sete   %dil
call   1050 <adbi_assure@plt>
mov    %ebx,%edi
call   1040 <close@plt>
test   %rax,%rax
je     11ad <close_socket+0x7d>
mov    0x2e54(%rip),%r14
mov    0x2e45(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r14),%rax
xor    %edi,%edi
cmp    (%r15),%rax
setne  %dil
call   1050 <adbi_assure@plt>
mov    %ebx,%edi
call   1040 <close@plt>
test   %rax,%rax
jne    1190 <close_socket+0x60>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SHUT_RDWR = global i32 0
@errno = global i64 0
@ENOTCONN = global i64 0
@EBADF = global i64 0
define void @close_socket(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
%3 = load i32, i32* %2
%4 = load i32, i32* @SHUT_RDWR
%5 = call i64 @shutdown(i32 %3, i32 %4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %1
%8 = load i64, i64* @errno
%9 = load i64, i64* @ENOTCONN
%10 = icmp eq i64 %8, %9
%11 = zext i1 %10 to i32
%12 = call i32 @adbi_assure(i32 %11)
br label %13
13: ; preds = %7, %1
br label %14
14: ; preds = %18, %13
%15 = load i32, i32* %2
%16 = call i64 @close(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %14
%19 = load i64, i64* @errno
%20 = load i64, i64* @EBADF
%21 = icmp ne i64 %19, %20
%22 = zext i1 %21 to i32
%23 = call i32 @adbi_assure(i32 %22)
br label %14
24: ; preds = %14
ret void
}

LLVM IR (-O2):
@SHUT_RDWR = global i32 0
@errno = global i64 0
@ENOTCONN = global i64 0
@EBADF = global i64 0
define void @close_socket(i32 %0) {
%2 = load i32, i32* @SHUT_RDWR
%3 = tail call i64 @shutdown(i32 %0, i32 %2)
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %11
5: ; preds = %1
%6 = load i64, i64* @errno
%7 = load i64, i64* @ENOTCONN
%8 = icmp eq i64 %6, %7
%9 = zext i1 %8 to i32
%10 = tail call i32 @adbi_assure(i32 %9)
br label %11
11: ; preds = %5, %1
%12 = tail call i64 @close(i32 %0)
%13 = icmp eq i64 %12, 0
br i1 %13, label %22, label %14
14: ; preds = %11, %14
%15 = load i64, i64* @errno
%16 = load i64, i64* @EBADF
%17 = icmp ne i64 %15, %16
%18 = zext i1 %17 to i32
%19 = tail call i32 @adbi_assure(i32 %18)
%20 = tail call i64 @close(i32 %0)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %14
22: ; preds = %14, %11
ret void
}