#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 /*<<< orphan*/ SHUT_RDWR;
scalar_t__ close(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ shutdown(int, int /*<<< orphan*/);
int socket_close(int sock) {
  if (shutdown(sock, SHUT_RDWR) < 0) {
    perror("socket_close() -> shutdown()");
    return -1;
  }
  if (close(sock) < 0) {
    perror("socket_close() -> close()");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<socket_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
call   1030 <shutdown@plt>
cmp    $0x0,%rax
jge    116e <socket_close+0x3e>
lea    0xea3(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    119f <socket_close+0x6f>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    1198 <socket_close+0x68>
lea    0xe96(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    119f <socket_close+0x6f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_close>:
push   %rbx
mov    %edi,%ebx
mov    0x2eb6(%rip),%rax
mov    (%rax),%esi
call   1030 <shutdown@plt>
test   %rax,%rax
js     1159 <socket_close+0x29>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
js     1162 <socket_close+0x32>
pop    %rbx
ret
lea    0xea0(%rip),%rdi
jmp    1169 <socket_close+0x39>
lea    0xeb4(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@SHUT_RDWR = global i32 0
@.str = [29 x i8] c"socket_close() -> shutdown()\00"
@.str.1 = [26 x i8] c"socket_close() -> close()\00"
define i32 @socket_close(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = load i32, i32* @SHUT_RDWR
%6 = call i64 @shutdown(i32 %4, i32 %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %17
10: ; preds = %1
%11 = load i32, i32* %3
%12 = call i64 @close(i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %17
16: ; preds = %10
store i32 0, i32* %2
br label %17
17: ; preds = %16, %14, %8
%18 = load i32, i32* %2
ret i32 %18
}

LLVM IR (-O2):
@SHUT_RDWR = global i32 0
@.str = [29 x i8] c"socket_close() -> shutdown()\00"
@.str.1 = [26 x i8] c"socket_close() -> close()\00"
define i32 @socket_close(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 %8, label %5
5: ; preds = %1
%6 = tail call i64 @close(i32 %0)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %5, %1
%9 = phi i8* [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), %5 ]
%10 = tail call i32 @perror(i8* %9)
br label %11
11: ; preds = %8, %5
%12 = phi i32 [ 0, %5 ], [ -1, %8 ]
ret i32 %12
}