#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*/ sin;
typedef int /*<<< orphan*/ one;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int, char const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int listenon(unsigned int port) {
  int fd;
  int one = 1;
  struct sockaddr_in sin;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd == -1) {
    perror("socket() failed");
    exit(1);
  }
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = htonl(INADDR_ANY);
  sin.sin_port = htons(port);
  (void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&one,
                   sizeof(one));
  (void)setsockopt(fd, SOL_SOCKET, 15, (const char *)&one, sizeof(one));
  if (bind(fd, (struct sockaddr *)&sin, sizeof(sin))) {
    perror("bind() failed");
    close(fd);
    exit(1);
  }
  return fd;
}


x86 assembly (clang, -O0):
<listenon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x1,-0xc(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11de <listenon+0x4e>
lea    0xe31(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
mov    0x2d97(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x8(%rbp),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rcx
mov    $0xf,%edx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
je     1293 <listenon+0x103>
lea    0xd94(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listenon>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
movl   $0x1,0x4(%rsp)
mov    0x2e35(%rip),%r15
mov    (%r15),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1262 <listenon+0xd2>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x2de0(%rip),%rbp
mov    0x0(%rbp),%esi
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%r15
mov    %ebx,%edi
mov    %r15,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x0(%rbp),%esi
mov    %ebx,%edi
mov    $0xf,%edx
mov    %r15,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
jne    1278 <listenon+0xe8>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd97(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd91(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

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
@.str = [16 x i8] c"socket() failed\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [14 x i8] c"bind() failed\00"
define i32 @listenon(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
store i32 %0, i32* %2
store i32 1, i32* %4
%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* %3
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %1
%15 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = load i32, i32* %2
%23 = call i32 @htons(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* %3
%26 = load i32, i32* @SOL_SOCKET
%27 = load i32, i32* @SO_REUSEADDR
%28 = bitcast i32* %4 to i8*
%29 = call i32 @setsockopt(i32 %25, i32 %26, i32 %27, i8* %28, i32 4)
%30 = load i32, i32* %3
%31 = load i32, i32* @SOL_SOCKET
%32 = bitcast i32* %4 to i8*
%33 = call i32 @setsockopt(i32 %30, i32 %31, i32 15, i8* %32, i32 4)
%34 = load i32, i32* %3
%35 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%36 = call i64 @bind(i32 %34, %struct.sockaddr* %35, i32 12)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %14
%39 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%40 = load i32, i32* %3
%41 = call i32 @close(i32 %40)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %14
%44 = load i32, i32* %3
ret i32 %44
}

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