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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int SOCK_DGRAM;
int SOCK_NONBLOCK;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(short);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int init_udp_socket(short port) {
  struct sockaddr_in addr;
  int s;
  s = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP);
  if (s < 0) {
    return s;
  }
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = INADDR_ANY;
  if (bind(s, (struct sockaddr *)&addr, sizeof(struct sockaddr))) {
    printf("Could not bind to UDP socket.\n");
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<init_udp_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %di,%ax
mov    %ax,-0x6(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%edi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    0x2e60(%rip),%rax
or     (%rax),%esi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1190 <init_udp_socket+0x50>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ed <init_udp_socket+0xad>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movswl -0x6(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
je     11e7 <init_udp_socket+0xa7>
lea    0xe2a(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <init_udp_socket+0xad>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_udp_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    0x2e97(%rip),%r14
mov    (%r14),%edi
mov    0x2e7d(%rip),%rax
mov    0x2e66(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11be <init_udp_socket+0x7e>
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
movswl %bp,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1050 <bind@plt>
test   %rax,%rax
je     11be <init_udp_socket+0x7e>
lea    0xe4c(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOCK_NONBLOCK = global i32 0
@IPPROTO_UDP = global i32 0
@INADDR_ANY = global i32 0
@.str = [31 x i8] c"Could not bind to UDP socket.\0A\00"
define i32 @init_udp_socket(i16 signext %0) {
%2 = alloca i32
%3 = alloca i16
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i16 %0, i16* %3
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = load i32, i32* @SOCK_NONBLOCK
%9 = or i32 %7, %8
%10 = load i32, i32* @IPPROTO_UDP
%11 = call i32 @socket(i32 %6, i32 %9, i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = load i32, i32* %5
store i32 %15, i32* %2
br label %33
16: ; preds = %1
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i16, i16* %3
%20 = call i32 @htons(i16 signext %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i32, i32* %5
%26 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%27 = call i64 @bind(i32 %25, %struct.sockaddr* %26, i32 4)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %16
%30 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %33
31: ; preds = %16
%32 = load i32, i32* %5
store i32 %32, i32* %2
br label %33
33: ; preds = %31, %29, %14
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOCK_NONBLOCK = global i32 0
@IPPROTO_UDP = global i32 0
@INADDR_ANY = global i32 0
@str = [30 x i8] c"Could not bind to UDP socket.\00"
define i32 @init_udp_socket(i16 signext %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = load i32, i32* @SOCK_NONBLOCK
%7 = or i32 %6, %5
%8 = load i32, i32* @IPPROTO_UDP
%9 = tail call i32 @socket(i32 %4, i32 %7, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %23, label %11
11: ; preds = %1
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %12, i32* %13
%14 = tail call i32 @htons(i16 signext %0)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* @INADDR_ANY
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %16, i32* %17
%18 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%19 = call i64 @bind(i32 %9, %struct.sockaddr* %18, i32 4)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %11
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %23
23: ; preds = %11, %1, %21
%24 = phi i32 [ -1, %21 ], [ %9, %1 ], [ %9, %11 ]
ret i32 %24
}