#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*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ set_nonblocking(int);
scalar_t__ set_nosigpipe(int);
scalar_t__ set_reuseaddr(int);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int create_socket(int af) {
  int fd = socket(af, SOCK_STREAM, 0);
  if (fd < 0) {
    return -1;
  }
  if (set_nonblocking(fd) < 0) {
    close(fd);
    return -1;
  }
  if (set_reuseaddr(fd) < 0) {
    close(fd);
    return -1;
  }
  if (set_nosigpipe(fd) < 0) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1187 <create_socket+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <create_socket+0xaf>
mov    -0xc(%rbp),%edi
call   1050 <set_nonblocking@plt>
cmp    $0x0,%rax
jge    11ad <create_socket+0x5d>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <create_socket+0xaf>
mov    -0xc(%rbp),%edi
call   1060 <set_reuseaddr@plt>
cmp    $0x0,%rax
jge    11d3 <create_socket+0x83>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <create_socket+0xaf>
mov    -0xc(%rbp),%edi
call   1040 <set_nosigpipe@plt>
cmp    $0x0,%rax
jge    11f9 <create_socket+0xa9>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <create_socket+0xaf>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     119d <create_socket+0x4d>
mov    %eax,%ebp
mov    %eax,%edi
call   1050 <set_nonblocking@plt>
test   %rax,%rax
js     1196 <create_socket+0x46>
mov    %ebp,%edi
call   1060 <set_reuseaddr@plt>
test   %rax,%rax
js     1196 <create_socket+0x46>
mov    %ebp,%edi
call   1040 <set_nosigpipe@plt>
test   %rax,%rax
js     1196 <create_socket+0x46>
mov    %ebp,%ebx
jmp    119d <create_socket+0x4d>
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@SOCK_STREAM = global i32 0
define i32 @create_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %34
11: ; preds = %1
%12 = load i32, i32* %4
%13 = call i64 @set_nonblocking(i32 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* %4
%17 = call i32 @close(i32 %16)
store i32 -1, i32* %2
br label %34
18: ; preds = %11
%19 = load i32, i32* %4
%20 = call i64 @set_reuseaddr(i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* %4
%24 = call i32 @close(i32 %23)
store i32 -1, i32* %2
br label %34
25: ; preds = %18
%26 = load i32, i32* %4
%27 = call i64 @set_nosigpipe(i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* %4
%31 = call i32 @close(i32 %30)
store i32 -1, i32* %2
br label %34
32: ; preds = %25
%33 = load i32, i32* %4
store i32 %33, i32* %2
br label %34
34: ; preds = %32, %29, %22, %15, %10
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@SOCK_STREAM = global i32 0
define i32 @create_socket(i32 %0) {
%2 = load i32, i32* @SOCK_STREAM
%3 = tail call i32 @socket(i32 %0, i32 %2, i32 0)
%4 = icmp slt i32 %3, 0
br i1 %4, label %16, label %5
5: ; preds = %1
%6 = tail call i64 @set_nonblocking(i32 %3)
%7 = icmp slt i64 %6, 0
br i1 %7, label %14, label %8
8: ; preds = %5
%9 = tail call i64 @set_reuseaddr(i32 %3)
%10 = icmp slt i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = tail call i64 @set_nosigpipe(i32 %3)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11, %8, %5
%15 = tail call i32 @close(i32 %3)
br label %16
16: ; preds = %14, %11, %1
%17 = phi i32 [ -1, %1 ], [ %3, %11 ], [ -1, %14 ]
ret i32 %17
}