#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sin;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_NONE;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_udp_socket(unsigned short port) {
  struct sockaddr_in sin;
  int sock, size;
  if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
    return -1;
  }
  size = 1;
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &size, sizeof(int)) == -1) {
    return -1;
  }
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_port = htons(port);
  if ((sin.sin_addr.s_addr = inet_addr("0.0.0.0")) == INADDR_NONE) {
    return -1;
  }
  if (bind(sock, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == -1) {
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<open_udp_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %di,%ax
mov    %ax,-0x6(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11a7 <open_udp_socket+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    1265 <open_udp_socket+0x105>
movl   $0x1,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11e7 <open_udp_socket+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    1265 <open_udp_socket+0x105>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movzwl -0x6(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
lea    0xdea(%rip),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x18(%rbp)
mov    0x2db3(%rip),%rcx
cmp    (%rcx),%eax
jne    1239 <open_udp_socket+0xd9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1265 <open_udp_socket+0x105>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    125f <open_udp_socket+0xff>
movl   $0xffffffff,-0x4(%rbp)
jmp    1265 <open_udp_socket+0x105>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_udp_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
mov    0x2e6c(%rip),%r15
mov    (%r15),%edi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     1228 <open_udp_socket+0xc8>
mov    %eax,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1228 <open_udp_socket+0xc8>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
movzwl %r14w,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
lea    0xe05(%rip),%rdi
call   1060 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    0x2dcd(%rip),%rcx
cmp    (%rcx),%eax
je     1228 <open_udp_socket+0xc8>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [8 x i8] c"0.0.0.0\00"
@INADDR_NONE = global i32 0
define i32 @open_udp_socket(i16 zeroext %0) {
%2 = alloca i32
%3 = alloca i16
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
store i16 %0, i16* %3
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = load i32, i32* @IPPROTO_UDP
%10 = call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %41
13: ; preds = %1
store i32 1, i32* %6
%14 = load i32, i32* %5
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_REUSEADDR
%17 = call i32 @setsockopt(i32 %14, i32 %15, i32 %16, i32* %6, i32 4)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %13
store i32 -1, i32* %2
br label %41
20: ; preds = %13
%21 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i16, i16* %3
%25 = call i32 @htons(i16 zeroext %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %25, i32* %26
%27 = call i32 @inet_addr(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
store i32 %27, i32* %29
%30 = load i32, i32* @INADDR_NONE
%31 = icmp eq i32 %27, %30
br i1 %31, label %32, label %33
32: ; preds = %20
store i32 -1, i32* %2
br label %41
33: ; preds = %20
%34 = load i32, i32* %5
%35 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%36 = call i32 @bind(i32 %34, %struct.sockaddr* %35, i32 12)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %39
38: ; preds = %33
store i32 -1, i32* %2
br label %41
39: ; preds = %33
%40 = load i32, i32* %5
store i32 %40, i32* %2
br label %41
41: ; preds = %39, %38, %32, %19, %12
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [8 x i8] c"0.0.0.0\00"
@INADDR_NONE = global i32 0
define i32 @open_udp_socket(i16 zeroext %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = load i32, i32* @IPPROTO_UDP
%9 = tail call i32 @socket(i32 %6, i32 %7, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %31, label %11
11: ; preds = %1
store i32 1, i32* %3
%12 = load i32, i32* @SOL_SOCKET
%13 = load i32, i32* @SO_REUSEADDR
%14 = call i32 @setsockopt(i32 %9, i32 %12, i32 %13, i32* %3, i32 4)
%15 = icmp eq i32 %14, -1
br i1 %15, label %31, label %16
16: ; preds = %11
%17 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @htons(i16 zeroext %0)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %20, i32* %21
%22 = call i32 @inet_addr(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* @INADDR_NONE
%25 = icmp eq i32 %22, %24
br i1 %25, label %31, label %26
26: ; preds = %16
%27 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%28 = call i32 @bind(i32 %9, %struct.sockaddr* %27, i32 12)
%29 = icmp eq i32 %28, -1
%30 = select i1 %29, i32 -1, i32 %9
br label %31
31: ; preds = %26, %16, %11, %1
%32 = phi i32 [ -1, %1 ], [ -1, %11 ], [ -1, %16 ], [ %30, %26 ]
ret i32 %32
}