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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, short);
int /*<<< orphan*/ htonl(unsigned int);
int /*<<< orphan*/ htons(short);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int setup_receive(int usd, unsigned int interface, short port) {
  struct sockaddr_in sa_rcvr;
  bzero((char *)&sa_rcvr, sizeof(sa_rcvr));
  sa_rcvr.sin_family = AF_INET;
  sa_rcvr.sin_addr.s_addr = htonl(interface);
  sa_rcvr.sin_port = htons(port);
  if (bind(usd, (struct sockaddr *)&sa_rcvr, sizeof(sa_rcvr)) == -1) {
    fprintf(stderr, "Could not bind to udp port %d\n", port);
    perror("bind");
    return 0;
  }
  listen(usd, 3);
  fcntl(usd, F_SETFD, FD_CLOEXEC);
  return 1;
}


x86 assembly (clang, -O0):
<setup_receive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dx,%ax
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %ax,-0xe(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <htonl@plt>
mov    %eax,-0x1c(%rbp)
movswl -0xe(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    1211 <setup_receive+0x91>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
movswl -0xe(%rbp),%edx
call   1060 <fprintf@plt>
lea    0xe1f(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    123f <setup_receive+0xbf>
mov    -0x8(%rbp),%edi
mov    $0x3,%esi
call   1070 <listen@plt>
mov    -0x8(%rbp),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
mov    0x2d97(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_receive>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %esi,%ebp
mov    %edi,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     1210 <setup_receive+0x90>
mov    %ebx,%edi
mov    $0x3,%esi
call   1070 <listen@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
movswl %r14w,%edx
lea    0xddc(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xdef(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
jmp    1205 <setup_receive+0x85>

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
@stderr = global i32 0
@.str = [31 x i8] c"Could not bind to udp port %d\0A\00"
@.str.1 = [5 x i8] c"bind\00"
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @setup_receive(i32 %0, i32 %1, i16 signext %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i16
%8 = alloca %struct.sockaddr_in
store i32 %0, i32* %5
store i32 %1, i32* %6
store i16 %2, i16* %7
%9 = bitcast %struct.sockaddr_in* %8 to i8*
%10 = call i32 @bzero(i8* %9, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* %6
%14 = call i32 @htonl(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i32 %14, i32* %16
%17 = load i16, i16* %7
%18 = call i32 @htons(i16 signext %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* %5
%21 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%22 = call i32 @bind(i32 %20, %struct.sockaddr* %21, i32 12)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %3
%25 = load i32, i32* @stderr
%26 = load i16, i16* %7
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i16 signext %26)
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %36
29: ; preds = %3
%30 = load i32, i32* %5
%31 = call i32 @listen(i32 %30, i32 3)
%32 = load i32, i32* %5
%33 = load i32, i32* @F_SETFD
%34 = load i32, i32* @FD_CLOEXEC
%35 = call i32 @fcntl(i32 %32, i32 %33, i32 %34)
store i32 1, i32* %4
br label %36
36: ; preds = %29, %24
%37 = load i32, i32* %4
ret i32 %37
}

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
@stderr = global i32 0
@.str = [31 x i8] c"Could not bind to udp port %d\0A\00"
@.str.1 = [5 x i8] c"bind\00"
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @setup_receive(i32 %0, i32 %1, i16 signext %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = call i32 @bzero(i8* %5, i32 12)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %7, i32* %8
%9 = tail call i32 @htonl(i32 %1)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %9, i32* %10
%11 = tail call i32 @htons(i16 signext %2)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %11, i32* %12
%13 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%14 = call i32 @bind(i32 %0, %struct.sockaddr* %13, i32 12)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %3
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i16 signext %2)
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %25
20: ; preds = %3
%21 = call i32 @listen(i32 %0, i32 3)
%22 = load i32, i32* @F_SETFD
%23 = load i32, i32* @FD_CLOEXEC
%24 = call i32 @fcntl(i32 %0, i32 %22, i32 %23)
br label %25
25: ; preds = %20, %16
%26 = phi i32 [ 0, %16 ], [ 1, %20 ]
ret i32 %26
}