#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*/ on;

/* Variables and functions */
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ net_set_error(char *, char *, int /*<<< orphan*/);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int net_create_socket(char *err, int domain) {
  int sock, on = 1;
  if ((sock = socket(domain, SOCK_STREAM, 0)) == -1) {
    net_set_error(err, "socket: %s", strerror(errno));
    return -1;
  }
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
    net_set_error(err, "setsockopt: %s", strerror(errno));
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<net_create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11a9 <net_create_socket+0x69>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
lea    0xe68(%rip),%rsi
call   1040 <net_set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <net_create_socket+0xd0>
mov    -0x18(%rbp),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    120a <net_create_socket+0xca>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xe12(%rip),%rsi
call   1040 <net_set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <net_create_socket+0xd0>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_create_socket>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%eax
mov    %rdi,%rbx
movl   $0x1,0x4(%rsp)
mov    0x2e81(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
xor    %edx,%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     11a9 <net_create_socket+0x69>
mov    %eax,%ebp
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11cd <net_create_socket+0x8d>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe64(%rip),%rsi
jmp    11be <net_create_socket+0x7e>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe42(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   1040 <net_set_error@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"socket: %s\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [15 x i8] c"setsockopt: %s\00"
define i32 @net_create_socket(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 1, i32* %7
%8 = load i32, i32* %5
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i32, i32* @errno
%15 = call i32 @strerror(i32 %14)
%16 = call i32 @net_set_error(i8* %13, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 -1, i32* %3
br label %30
17: ; preds = %2
%18 = load i32, i32* %6
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i32 @setsockopt(i32 %18, i32 %19, i32 %20, i32* %7, i32 4)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %28
23: ; preds = %17
%24 = load i8*, i8** %4
%25 = load i32, i32* @errno
%26 = call i32 @strerror(i32 %25)
%27 = call i32 @net_set_error(i8* %24, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %26)
store i32 -1, i32* %3
br label %30
28: ; preds = %17
%29 = load i32, i32* %6
store i32 %29, i32* %3
br label %30
30: ; preds = %28, %23, %12
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"socket: %s\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [15 x i8] c"setsockopt: %s\00"
define i32 @net_create_socket(i8* %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %1, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* @errno
%10 = tail call i32 @strerror(i32 %9)
%11 = tail call i32 @net_set_error(i8* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %10)
br label %21
12: ; preds = %2
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_REUSEADDR
%15 = call i32 @setsockopt(i32 %6, i32 %13, i32 %14, i32* %3, i32 4)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 @net_set_error(i8* %0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %12, %17, %8
%22 = phi i32 [ -1, %8 ], [ -1, %17 ], [ %6, %12 ]
ret i32 %22
}