#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 /*<<< orphan*/ SHUT_RDWR;
int close(int);
scalar_t__ errno;
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
void close_socket(int sock) {
  int ret;
  if (sock < 0)
    return;
  shutdown(sock, SHUT_RDWR);
  do {
    ret = close(sock);
  } while (ret < 0 && errno == EINTR);
  return;
}


x86 assembly (clang, -O0):
<close_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    113a <close_socket+0x1a>
jmp    118f <close_socket+0x6f>
mov    -0x4(%rbp),%edi
mov    0x2eac(%rip),%rax
mov    (%rax),%esi
call   1030 <shutdown@plt>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x8(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x8(%rbp)
mov    %al,-0x9(%rbp)
jge    117f <close_socket+0x5f>
mov    0x2e6c(%rip),%rax
mov    (%rax),%rax
mov    0x2e5a(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x9(%rbp)
mov    -0x9(%rbp),%al
test   $0x1,%al
jne    114b <close_socket+0x2b>
jmp    118f <close_socket+0x6f>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_socket>:
push   %r15
push   %r14
push   %rbx
test   %edi,%edi
js     1163 <close_socket+0x43>
mov    %edi,%ebx
mov    0x2ebe(%rip),%rax
mov    (%rax),%esi
call   1030 <shutdown@plt>
mov    0x2e98(%rip),%r14
mov    0x2e89(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1040 <close@plt>
test   %eax,%eax
jns    1163 <close_socket+0x43>
mov    (%r15),%rax
cmp    %rax,(%r14)
je     1150 <close_socket+0x30>
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@SHUT_RDWR = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define void @close_socket(i32 %0) {
%2 = icmp slt i32 %0, 0
br i1 %2, label %13, label %3
3: ; preds = %1
%4 = load i32, i32* @SHUT_RDWR
%5 = tail call i32 @shutdown(i32 %0, i32 %4)
br label %6
6: ; preds = %6, %3
%7 = tail call i32 @close(i32 %0)
%8 = icmp slt i32 %7, 0
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINTR
%11 = icmp eq i64 %9, %10
%12 = select i1 %8, i1 %11, i1 false
br i1 %12, label %6, label %13
13: ; preds = %6, %1
ret void
}