#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 */
typedef int /*<<< orphan*/ reuseaddr;
typedef int /*<<< orphan*/ nosigpipe;

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int socketClosed() {
  int socketFD = socket(AF_LOCAL, SOCK_RAW, 0);
  if (socketFD == -1) {
    return -1;
  }
  int status;
  status = fcntl(socketFD, F_SETFL, O_NONBLOCK);
  if (status == -1) {
    close(socketFD);
    return -1;
  }
  int reuseaddr = 1;
  status = setsockopt(socketFD, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
                      sizeof(reuseaddr));
  if (status == -1) {
    close(socketFD);
    return -1;
  }
  int nosigpipe = 1;
  status = setsockopt(socketFD, SOL_SOCKET, SO_REUSEADDR, &nosigpipe,
                      sizeof(nosigpipe));
  if (status == -1) {
    close(socketFD);
    return -1;
  }
  return socketFD;
}


x86 assembly (clang, -O0):
<socketClosed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    117a <socketClosed+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <socketClosed+0x113>
mov    -0x8(%rbp),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11b5 <socketClosed+0x75>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <socketClosed+0x113>
movl   $0x1,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1201 <socketClosed+0xc1>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <socketClosed+0x113>
movl   $0x1,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    0x2d9d(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    124d <socketClosed+0x10d>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <socketClosed+0x113>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketClosed>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11fe <socketClosed+0xbe>
mov    %eax,%ebx
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11f7 <socketClosed+0xb7>
movl   $0x1,0xc(%rsp)
mov    0x2e4a(%rip),%r14
mov    (%r14),%esi
mov    0x2e10(%rip),%rbp
mov    0x0(%rbp),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     11e7 <socketClosed+0xa7>
movl   $0x1,0x8(%rsp)
mov    (%r14),%esi
mov    0x0(%rbp),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11f3 <socketClosed+0xb3>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%ebp
jmp    11fe <socketClosed+0xbe>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@AF_LOCAL = global i32 0
@SOCK_RAW = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @socketClosed() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = load i32, i32* @AF_LOCAL
%7 = load i32, i32* @SOCK_RAW
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %0
store i32 -1, i32* %1
br label %44
12: ; preds = %0
%13 = load i32, i32* %2
%14 = load i32, i32* @F_SETFL
%15 = load i32, i32* @O_NONBLOCK
%16 = call i32 @fcntl(i32 %13, i32 %14, i32 %15)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load i32, i32* %2
%21 = call i32 @close(i32 %20)
store i32 -1, i32* %1
br label %44
22: ; preds = %12
store i32 1, i32* %4
%23 = load i32, i32* %2
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_REUSEADDR
%26 = call i32 @setsockopt(i32 %23, i32 %24, i32 %25, i32* %4, i32 4)
store i32 %26, i32* %3
%27 = load i32, i32* %3
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = load i32, i32* %2
%31 = call i32 @close(i32 %30)
store i32 -1, i32* %1
br label %44
32: ; preds = %22
store i32 1, i32* %5
%33 = load i32, i32* %2
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_REUSEADDR
%36 = call i32 @setsockopt(i32 %33, i32 %34, i32 %35, i32* %5, i32 4)
store i32 %36, i32* %3
%37 = load i32, i32* %3
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %32
%40 = load i32, i32* %2
%41 = call i32 @close(i32 %40)
store i32 -1, i32* %1
br label %44
42: ; preds = %32
%43 = load i32, i32* %2
store i32 %43, i32* %1
br label %44
44: ; preds = %42, %39, %29, %19, %11
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@AF_LOCAL = global i32 0
@SOCK_RAW = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @socketClosed() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @AF_LOCAL
%4 = load i32, i32* @SOCK_RAW
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %34, label %7
7: ; preds = %0
%8 = load i32, i32* @F_SETFL
%9 = load i32, i32* @O_NONBLOCK
%10 = tail call i32 @fcntl(i32 %5, i32 %8, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 @close(i32 %5)
br label %34
14: ; preds = %7
%15 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEADDR
%18 = call i32 @setsockopt(i32 %5, i32 %16, i32 %17, i32* %1, i32 4)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @close(i32 %5)
br label %32
22: ; preds = %14
%23 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_REUSEADDR
%26 = call i32 @setsockopt(i32 %5, i32 %24, i32 %25, i32* %2, i32 4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @close(i32 %5)
br label %30
30: ; preds = %22, %28
%31 = phi i32 [ -1, %28 ], [ %5, %22 ]
br label %32
32: ; preds = %30, %20
%33 = phi i32 [ -1, %20 ], [ %31, %30 ]
br label %34
34: ; preds = %12, %32, %0
%35 = phi i32 [ -1, %0 ], [ -1, %12 ], [ %33, %32 ]
ret i32 %35
}