#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 {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serveraddr;
typedef int /*<<< orphan*/ on;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int server_u(const char *ipaddr, int port, int blocking) {
  int server_sock = 0;
  int on = 1;
  int stat = 0;
  struct sockaddr_in serveraddr;
  server_sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (server_sock < 0) {
    return -1;
  }
  {
    on = 1;
    setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  }
  if (!blocking) {
    stat = fcntl(server_sock, F_GETFL, NULL);
    stat |= O_NONBLOCK;
    fcntl(server_sock, F_SETFL, stat);
  }
  memset(&serveraddr, 0, sizeof(serveraddr));
  serveraddr.sin_family = AF_INET;
  serveraddr.sin_addr.s_addr = inet_addr(ipaddr);
  serveraddr.sin_port = htons(port);
  if (bind(server_sock, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) <
      0) {
    close(server_sock);
    return -1;
  }
  signal(SIGPIPE, SIG_IGN);
  return server_sock;
}


x86 assembly (clang, -O0):
<server_u>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11e9 <server_u+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d6 <server_u+0x146>
movl   $0x1,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    0x2d9d(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmpl   $0x0,-0x18(%rbp)
jne    1257 <server_u+0xc7>
mov    -0x1c(%rbp),%edi
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
call   1040 <fcntl@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    12b9 <server_u+0x129>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d6 <server_u+0x146>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
mov    0x2cff(%rip),%rax
mov    (%rax),%esi
call   1090 <signal@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server_u>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e33(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    $0xffffffff,%r12d
test   %eax,%eax
js     1299 <server_u+0x109>
mov    %eax,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
mov    0x2db7(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %ebp,%ebp
jne    122e <server_u+0x9e>
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    0x2dbe(%rip),%rcx
or     (%rcx),%eax
mov    0x2d95(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
call   1040 <fcntl@plt>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %r15,%rdi
call   1080 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
js     1292 <server_u+0x102>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
call   1090 <signal@plt>
mov    %ebx,%r12d
jmp    1299 <server_u+0x109>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
define i32 @server_u(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 1, i32* %9
store i32 0, i32* %10
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %59
18: ; preds = %3
store i32 1, i32* %9
%19 = load i32, i32* %8
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_REUSEADDR
%22 = call i32 @setsockopt(i32 %19, i32 %20, i32 %21, i32* %9, i32 4)
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, 0
br i1 %24, label %36, label %25
25: ; preds = %18
%26 = load i32, i32* %8
%27 = load i32, i32* @F_GETFL
%28 = call i32 @fcntl(i32 %26, i32 %27, i32 0)
store i32 %28, i32* %10
%29 = load i32, i32* @O_NONBLOCK
%30 = load i32, i32* %10
%31 = or i32 %30, %29
store i32 %31, i32* %10
%32 = load i32, i32* %8
%33 = load i32, i32* @F_SETFL
%34 = load i32, i32* %10
%35 = call i32 @fcntl(i32 %32, i32 %33, i32 %34)
br label %36
36: ; preds = %25, %18
%37 = call i32 @memset(%struct.sockaddr_in* %11, i32 0, i32 12)
%38 = load i32, i32* @AF_INET
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %38, i32* %39
%40 = load i8*, i8** %5
%41 = call i32 @inet_addr(i8* %40)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%43 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %42, i32 0, i32 0
store i32 %41, i32* %43
%44 = load i32, i32* %6
%45 = call i32 @htons(i32 %44)
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %45, i32* %46
%47 = load i32, i32* %8
%48 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%49 = call i64 @bind(i32 %47, %struct.sockaddr* %48, i32 12)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %36
%52 = load i32, i32* %8
%53 = call i32 @close(i32 %52)
store i32 -1, i32* %4
br label %59
54: ; preds = %36
%55 = load i32, i32* @SIGPIPE
%56 = load i32, i32* @SIG_IGN
%57 = call i32 @signal(i32 %55, i32 %56)
%58 = load i32, i32* %8
store i32 %58, i32* %4
br label %59
59: ; preds = %54, %51, %17
%60 = load i32, i32* %4
ret i32 %60
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
define i32 @server_u(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = bitcast i32* %4 to i8*
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %41, label %12
12: ; preds = %3
store i32 1, i32* %4
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_REUSEADDR
%15 = call i32 @setsockopt(i32 %10, i32 %13, i32 %14, i32* %4, i32 4)
%16 = icmp eq i32 %2, 0
br i1 %16, label %17, label %24
17: ; preds = %12
%18 = load i32, i32* @F_GETFL
%19 = call i32 @fcntl(i32 %10, i32 %18, i32 0)
%20 = load i32, i32* @O_NONBLOCK
%21 = or i32 %20, %19
%22 = load i32, i32* @F_SETFL
%23 = call i32 @fcntl(i32 %10, i32 %22, i32 %21)
br label %24
24: ; preds = %17, %12
%25 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %26, i32* %27
%28 = call i32 @inet_addr(i8* %0)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %28, i32* %29
%30 = call i32 @htons(i32 %1)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %30, i32* %31
%32 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%33 = call i64 @bind(i32 %10, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %24
%36 = call i32 @close(i32 %10)
br label %41
37: ; preds = %24
%38 = load i32, i32* @SIGPIPE
%39 = load i32, i32* @SIG_IGN
%40 = call i32 @signal(i32 %38, i32 %39)
br label %41
41: ; preds = %3, %37, %35
%42 = phi i32 [ -1, %35 ], [ %10, %37 ], [ -1, %3 ]
ret i32 %42
}