#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 */
typedef int /*<<< orphan*/ x;
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*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_udp_socket(int port) {
  struct sockaddr_in addr;
  int sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock < 0)
    error("socket");
  int x;
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x)) == -1)
    error("setsockopt (SO_REUSEADDR)");
  if (port) {
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1)
      error("bind");
  }
  fcntl(sock, F_SETFL, O_NONBLOCK);
  return sock;
}


x86 assembly (clang, -O0):
<open_udp_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ad <open_udp_socket+0x3d>
lea    0xe58(%rip),%rdi
call   1080 <error@plt>
mov    -0x14(%rbp),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11e6 <open_udp_socket+0x76>
lea    0xe26(%rip),%rdi
call   1080 <error@plt>
cmpl   $0x0,-0x4(%rbp)
je     1243 <open_udp_socket+0xd3>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0xc(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    123e <open_udp_socket+0xce>
lea    0xde8(%rip),%rdi
call   1080 <error@plt>
jmp    1243 <open_udp_socket+0xd3>
mov    -0x14(%rbp),%edi
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
mov    0x2d82(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_udp_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    0x2e5f(%rip),%r14
mov    (%r14),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11a6 <open_udp_socket+0x36>
lea    0xe5f(%rip),%rdi
call   1080 <error@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     11d5 <open_udp_socket+0x65>
test   %ebp,%ebp
jne    11e5 <open_udp_socket+0x75>
jmp    1228 <open_udp_socket+0xb8>
lea    0xe2b(%rip),%rdi
call   1080 <error@plt>
test   %ebp,%ebp
je     1228 <open_udp_socket+0xb8>
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1228 <open_udp_socket+0xb8>
lea    0xdfe(%rip),%rdi
call   1080 <error@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
mov    0x2da0(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1040 <fcntl@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
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
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [26 x i8] c"setsockopt (SO_REUSEADDR)\00"
@INADDR_ANY = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @open_udp_socket(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
%14 = load i32, i32* %4
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_REUSEADDR
%17 = call i32 @setsockopt(i32 %14, i32 %15, i32 %16, i32* %5, i32 4)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %13
%22 = load i32, i32* %2
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %41
24: ; preds = %21
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i32, i32* %2
%28 = call i32 @htons(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %28, i32* %29
%30 = load i32, i32* @INADDR_ANY
%31 = call i32 @htonl(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = load i32, i32* %4
%35 = bitcast %struct.sockaddr_in* %3 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 %40
38: ; preds = %24
%39 = call i32 @error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %24
br label %41
41: ; preds = %40, %21
%42 = load i32, i32* %4
%43 = load i32, i32* @F_SETFL
%44 = load i32, i32* @O_NONBLOCK
%45 = call i32 @fcntl(i32 %42, i32 %43, i32 %44)
%46 = load i32, i32* %4
ret i32 %46
}

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
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [26 x i8] c"setsockopt (SO_REUSEADDR)\00"
@INADDR_ANY = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @open_udp_socket(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %1
%12 = bitcast i32* %3 to i8*
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_REUSEADDR
%15 = call i32 @setsockopt(i32 %7, i32 %13, i32 %14, i32* %3, i32 4)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %11
%20 = icmp eq i32 %0, 0
br i1 %20, label %34, label %21
21: ; preds = %19
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @htons(i32 %0)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = call i32 @htonl(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %27, i32* %28
%29 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%30 = call i32 @bind(i32 %7, %struct.sockaddr* %29, i32 12)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %21
%33 = call i32 @error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %21, %32, %19
%35 = load i32, i32* @F_SETFL
%36 = load i32, i32* @O_NONBLOCK
%37 = call i32 @fcntl(i32 %7, i32 %35, i32 %36)
ret i32 %7
}