#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*/ yes;
struct sockaddr_in6 {
  int dummy;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int sa_family;
};

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr const *, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int sock_create(const struct sockaddr *sa) {
  int sock;
  int yes = 1;
  int salen;
  switch (sa->sa_family) {
  case AF_INET:
    salen = sizeof(struct sockaddr_in);
    break;
  case AF_INET6:
    salen = sizeof(struct sockaddr_in6);
    break;
  default:
    return -1;
  }
  if ((sock = socket(sa->sa_family, SOCK_DGRAM, 0)) < 0) {
    return -1;
  }
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes))) {
    return -1;
  };
  if (bind(sock, sa, salen) < 0) {
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<sock_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
sub    $0x80,%eax
je     117b <sock_create+0x4b>
jmp    115c <sock_create+0x2c>
mov    -0x20(%rbp),%eax
sub    $0x81,%eax
jne    1187 <sock_create+0x57>
jmp    116f <sock_create+0x3f>
movl   $0x4,-0x1c(%rbp)
jmp    1193 <sock_create+0x63>
movl   $0x4,-0x1c(%rbp)
jmp    1193 <sock_create+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <sock_create+0xf6>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11c1 <sock_create+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <sock_create+0xf6>
mov    -0x14(%rbp),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     11fb <sock_create+0xcb>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <sock_create+0xf6>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <bind@plt>
cmp    $0x0,%rax
jge    1220 <sock_create+0xf0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <sock_create+0xf6>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_create>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
movl   $0x1,0xc(%rsp)
mov    (%rdi),%edi
mov    %edi,%eax
and    $0xfffffffe,%eax
mov    $0xffffffff,%ebp
cmp    $0x80,%eax
jne    11b6 <sock_create+0x86>
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
test   %eax,%eax
js     11b6 <sock_create+0x86>
mov    %eax,%r14d
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %r14d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
jne    11b6 <sock_create+0x86>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1040 <bind@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovs  %eax,%r14d
mov    %r14d,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@SOCK_DGRAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @sock_create(%struct.sockaddr* %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %3
store i32 1, i32* %5
%7 = load %struct.sockaddr*, %struct.sockaddr** %3
%8 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %7, i32 0, i32 0
%9 = load i32, i32* %8
switch i32 %9, label %12 [
i32 129, label %10
i32 128, label %11
]
10: ; preds = %1
store i32 4, i32* %6
br label %13
11: ; preds = %1
store i32 4, i32* %6
br label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %37
13: ; preds = %11, %10
%14 = load %struct.sockaddr*, %struct.sockaddr** %3
%15 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load i32, i32* @SOCK_DGRAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %13
store i32 -1, i32* %2
br label %37
21: ; preds = %13
%22 = load i32, i32* %4
%23 = load i32, i32* @SOL_SOCKET
%24 = load i32, i32* @SO_REUSEADDR
%25 = call i64 @setsockopt(i32 %22, i32 %23, i32 %24, i32* %5, i32 4)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21
store i32 -1, i32* %2
br label %37
28: ; preds = %21
%29 = load i32, i32* %4
%30 = load %struct.sockaddr*, %struct.sockaddr** %3
%31 = load i32, i32* %6
%32 = call i64 @bind(i32 %29, %struct.sockaddr* %30, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %28
store i32 -1, i32* %2
br label %37
35: ; preds = %28
%36 = load i32, i32* %4
store i32 %36, i32* %2
br label %37
37: ; preds = %35, %34, %27, %20, %12
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@SOCK_DGRAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @sock_create(%struct.sockaddr* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%4 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%5 = load i32, i32* %4
%6 = and i32 %5, -2
%7 = icmp eq i32 %6, 128
br i1 %7, label %8, label %21
8: ; preds = %1
%9 = load i32, i32* @SOCK_DGRAM
%10 = tail call i32 @socket(i32 %5, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %21, label %12
12: ; preds = %8
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_REUSEADDR
%15 = call i64 @setsockopt(i32 %10, i32 %13, i32 %14, i32* %2, i32 4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = call i64 @bind(i32 %10, %struct.sockaddr* %0, i32 4)
%19 = icmp slt i64 %18, 0
%20 = select i1 %19, i32 -1, i32 %10
br label %21
21: ; preds = %1, %17, %12, %8
%22 = phi i32 [ -1, %1 ], [ -1, %8 ], [ -1, %12 ], [ %20, %17 ]
ret i32 %22
}