#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*/ u_short;
typedef int /*<<< orphan*/ trueval;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ inet_aton(char const *, TYPE_1__ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, void *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int udp_listen(const char *iface, int port) {
  int sock;
  struct sockaddr_in serv_addr;
  int trueval = 1;
  memset((char *)&serv_addr, 0, sizeof(serv_addr));
  if (iface == NULL) {
    serv_addr.sin_addr.s_addr = INADDR_ANY;
  } else if (inet_aton(iface, &serv_addr.sin_addr) == 0) {
    return -1;
  }
  sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock == -1) {
    return -1;
  }
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&trueval,
                 sizeof(trueval)) == -1) {
    close(sock);
    return -1;
  }
  serv_addr.sin_port = htons((u_short)port);
  serv_addr.sin_family = AF_INET;
  if (bind(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) {
    close(sock);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<udp_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x2c(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11b2 <udp_listen+0x42>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    11de <udp_listen+0x6e>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
add    $0x8,%rsi
call   1070 <inet_aton@plt>
cmp    $0x0,%rax
jne    11d9 <udp_listen+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    129c <udp_listen+0x12c>
jmp    11de <udp_listen+0x6e>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1210 <udp_listen+0xa0>
movl   $0xffffffff,-0x4(%rbp)
jmp    129c <udp_listen+0x12c>
mov    -0x18(%rbp),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    0x2d9d(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1251 <udp_listen+0xe1>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129c <udp_listen+0x12c>
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    1296 <udp_listen+0x126>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129c <udp_listen+0x12c>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_listen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
movl   $0x1,0x14(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
test   %rbx,%rbx
je     11ba <udp_listen+0x4a>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <inet_aton@plt>
test   %rax,%rax
jne    11c7 <udp_listen+0x57>
mov    $0xffffffff,%ebx
jmp    124a <udp_listen+0xda>
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e12(%rip),%r15
mov    (%r15),%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     124a <udp_listen+0xda>
mov    %eax,%ebp
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1243 <udp_listen+0xd3>
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     1243 <udp_listen+0xd3>
mov    %ebp,%ebx
jmp    124a <udp_listen+0xda>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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