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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ INADDR_ANY;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr const *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ fail_if(int);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int start_udp_server() {
  int sockfd, bindrc, fcntlrc, flags;
  struct sockaddr_in servaddr;
  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  fail_if(sockfd < 0);
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(9877);
  bindrc = bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr));
  fail_if(bindrc < 0);
  flags = fcntl(sockfd, F_GETFL, 0);
  fail_if(flags < 0);
  flags |= O_NONBLOCK;
  fcntlrc = fcntl(sockfd, F_SETFL, flags);
  fail_if(fcntlrc < 0);
  return sockfd;
}


x86 assembly (clang, -O0):
<start_udp_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <fail_if@plt>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x2695,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <fail_if@plt>
mov    -0x4(%rbp),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <fail_if@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <fail_if@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_udp_server>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e69(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
mov    %eax,%edi
shr    $0x1f,%edi
call   1060 <fail_if@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    $0x2695,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
shr    $0x1f,%eax
mov    %eax,%edi
call   1060 <fail_if@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,%ebp
mov    %eax,%edi
shr    $0x1f,%edi
call   1060 <fail_if@plt>
mov    0x2dce(%rip),%rax
or     (%rax),%ebp
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %ebp,%edx
call   1030 <fcntl@plt>
shr    $0x1f,%eax
mov    %eax,%edi
call   1060 <fail_if@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
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
@INADDR_ANY = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @start_udp_server() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%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* %1
%9 = load i32, i32* %1
%10 = icmp slt i32 %9, 0
%11 = zext i1 %10 to i32
%12 = call i32 @fail_if(i32 %11)
%13 = call i32 @bzero(%struct.sockaddr_in* %5, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @INADDR_ANY
%17 = call i32 @htonl(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = call i32 @htons(i32 9877)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* %1
%23 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%24 = call i32 @bind(i32 %22, %struct.sockaddr* %23, i32 12)
store i32 %24, i32* %2
%25 = load i32, i32* %2
%26 = icmp slt i32 %25, 0
%27 = zext i1 %26 to i32
%28 = call i32 @fail_if(i32 %27)
%29 = load i32, i32* %1
%30 = load i32, i32* @F_GETFL
%31 = call i32 @fcntl(i32 %29, i32 %30, i32 0)
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = icmp slt i32 %32, 0
%34 = zext i1 %33 to i32
%35 = call i32 @fail_if(i32 %34)
%36 = load i32, i32* @O_NONBLOCK
%37 = load i32, i32* %4
%38 = or i32 %37, %36
store i32 %38, i32* %4
%39 = load i32, i32* %1
%40 = load i32, i32* @F_SETFL
%41 = load i32, i32* %4
%42 = call i32 @fcntl(i32 %39, i32 %40, i32 %41)
store i32 %42, i32* %3
%43 = load i32, i32* %3
%44 = icmp slt i32 %43, 0
%45 = zext i1 %44 to i32
%46 = call i32 @fail_if(i32 %45)
%47 = load i32, i32* %1
ret i32 %47
}

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
@INADDR_ANY = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @start_udp_server() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = lshr i32 %5, 31
%7 = tail call i32 @fail_if(i32 %6)
%8 = call i32 @bzero(%struct.sockaddr_in* %1, i32 12)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* @INADDR_ANY
%12 = tail call i32 @htonl(i32 %11)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %12, i32* %13
%14 = tail call i32 @htons(i32 9877)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %14, i32* %15
%16 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%17 = call i32 @bind(i32 %5, %struct.sockaddr* %16, i32 12)
%18 = lshr i32 %17, 31
%19 = call i32 @fail_if(i32 %18)
%20 = load i32, i32* @F_GETFL
%21 = call i32 @fcntl(i32 %5, i32 %20, i32 0)
%22 = lshr i32 %21, 31
%23 = call i32 @fail_if(i32 %22)
%24 = load i32, i32* @O_NONBLOCK
%25 = or i32 %24, %21
%26 = load i32, i32* @F_SETFL
%27 = call i32 @fcntl(i32 %5, i32 %26, i32 %25)
%28 = lshr i32 %27, 31
%29 = call i32 @fail_if(i32 %28)
ret i32 %5
}