#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*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int socketUDP(unsigned short port, struct sockaddr_in *udp_addr) {
  int sockfd = socket(PF_INET, SOCK_DGRAM, 0);
  if (sockfd == -1) {
    perror("socket");
    exit(1);
  }
  if (port == -1 || udp_addr == 0)
    return sockfd;
  memset(udp_addr, 0, sizeof(struct sockaddr_in));
  udp_addr->sin_family = AF_INET;
  udp_addr->sin_port = htons(port);
  udp_addr->sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(sockfd, (struct sockaddr *)udp_addr, sizeof(struct sockaddr_in)) <
      0) {
    perror("bind");
    exit(1);
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<socketUDP>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %di,%ax
mov    %ax,-0x6(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11bf <socketUDP+0x4f>
lea    0xe50(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movzwl -0x6(%rbp),%eax
cmp    $0xffffffff,%eax
je     11d7 <socketUDP+0x67>
cmpq   $0x0,-0x10(%rbp)
jne    11e2 <socketUDP+0x72>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1261 <socketUDP+0xf1>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
movzwl -0x6(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    125b <socketUDP+0xeb>
lea    0xdbb(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketUDP>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     11f1 <socketUDP+0x81>
mov    %eax,%ebp
test   %rbx,%rbx
je     11ea <socketUDP+0x7a>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
movzwl %r14w,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rbx)
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,(%rbx)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     11fa <socketUDP+0x8a>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe08(%rip),%rdi
jmp    1201 <socketUDP+0x91>
lea    0xe06(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

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