#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;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(short);
int /*<<< orphan*/ printf(char *, int, short);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int udp_init(short port) {
  int udp_socket;
  struct sockaddr_in sa;
  int err;
  udp_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (udp_socket == -1)
    return -1;
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  sa.sin_addr.s_addr = INADDR_ANY;
  err = bind(udp_socket, (struct sockaddr *)&sa, sizeof(sa));
  if (err == -1) {
    close(udp_socket);
    return -2;
  } else {
    printf("UDP socket %d listening on port %d\n", udp_socket, port);
  }
  return udp_socket;
}


x86 assembly (clang, -O0):
<udp_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %di,%ax
mov    %ax,-0x6(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1198 <udp_init+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    1207 <udp_init+0xb7>
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movswl -0x6(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ee <udp_init+0x9e>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1207 <udp_init+0xb7>
mov    -0xc(%rbp),%esi
lea    0xe08(%rip),%rdi
movswl -0x6(%rbp),%edx
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_init>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11cf <udp_init+0x7f>
mov    %eax,%ebx
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
movswl %bp,%ebp
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     11d6 <udp_init+0x86>
lea    0xe3c(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1040 <printf@plt>
jmp    11e2 <udp_init+0x92>
mov    $0xffffffff,%ebx
jmp    11e2 <udp_init+0x92>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xfffffffe,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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

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