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

/* Type definitions */
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ ai_flags;
};
typedef int /*<<< orphan*/ addrCriteria;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ acceptClient(int);
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int /*<<< orphan*/ getSockName(int);
int getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sysError(char *);
int /*<<< orphan*/ usrError(char *);
int main(int argc, char **argv) {
  if (argc != 2)
    usrError("Usage:\t<Port>");
  char *port = argv[1];
  struct addrinfo addrCriteria;
  memset(&addrCriteria, 0, sizeof(addrCriteria));
  addrCriteria.ai_family = AF_UNSPEC;
  addrCriteria.ai_flags = AI_PASSIVE;
  addrCriteria.ai_socktype = SOCK_DGRAM;
  addrCriteria.ai_protocol = IPPROTO_UDP;
  struct addrinfo *servAddr;
  int ret = getaddrinfo(NULL, port, &addrCriteria, &servAddr);
  if (ret != 0)
    sysError(gai_strerror(ret));
  int sock =
      socket(servAddr->ai_family, servAddr->ai_socktype, servAddr->ai_protocol);
  if (sock < 0)
    sysError("failed to create socket");
  if (bind(sock, servAddr->ai_addr, servAddr->ai_addrlen) < 0)
    sysError("failed to bind to port");
  freeaddrinfo(servAddr);
  getSockName(sock);
  acceptClient(sock);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11cc <main+0x2c>
lea    0xe39(%rip),%rdi
call   1040 <usrError@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     124a <main+0xaa>
mov    -0x3c(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    %rax,%rdi
call   1030 <sysError@plt>
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x38(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    127d <main+0xdd>
lea    0xd96(%rip),%rdi
call   1030 <sysError@plt>
mov    -0x40(%rbp),%edi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    12a8 <main+0x108>
lea    0xd83(%rip),%rdi
call   1030 <sysError@plt>
mov    -0x38(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x40(%rbp),%edi
call   1070 <getSockName@plt>
mov    -0x40(%rbp),%edi
call   1090 <acceptClient@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
je     11bb <main+0x1b>
lea    0xe4a(%rip),%rdi
call   1040 <usrError@plt>
mov    0x8(%rbx),%rbx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
je     122c <main+0x8c>
mov    %eax,%edi
call   1050 <gai_strerror@plt>
mov    %rax,%rdi
call   1030 <sysError@plt>
mov    0x8(%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10c0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1251 <main+0xb1>
lea    0xdc2(%rip),%rdi
call   1030 <sysError@plt>
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1080 <bind@plt>
test   %rax,%rax
jns    1273 <main+0xd3>
lea    0xdb8(%rip),%rdi
call   1030 <sysError@plt>
mov    0x8(%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    %ebx,%edi
call   1070 <getSockName@plt>
mov    %ebx,%edi
call   1090 <acceptClient@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32 }
@.str = [14 x i8] c"Usage:\09<Port>\00"
@AF_UNSPEC = global i32 0
@AI_PASSIVE = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.1 = [24 x i8] c"failed to create socket\00"
@.str.2 = [23 x i8] c"failed to bind to port\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @usrError(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
store i8* %18, i8** %6
%19 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 24)
%20 = load i32, i32* @AF_UNSPEC
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 4
store i32 %20, i32* %21
%22 = load i32, i32* @AI_PASSIVE
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 5
store i32 %22, i32* %23
%24 = load i32, i32* @SOCK_DGRAM
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 3
store i32 %24, i32* %25
%26 = load i32, i32* @IPPROTO_UDP
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 2
store i32 %26, i32* %27
%28 = load i8*, i8** %6
%29 = call i32 @getaddrinfo(i32* null, i8* %28, %struct.addrinfo* %7, %struct.addrinfo** %8)
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %15
%33 = load i32, i32* %9
%34 = call i8* @gai_strerror(i32 %33)
%35 = call i32 @sysError(i8* %34)
br label %36
36: ; preds = %32, %15
%37 = load %struct.addrinfo*, %struct.addrinfo** %8
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 4
%39 = load i32, i32* %38
%40 = load %struct.addrinfo*, %struct.addrinfo** %8
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 3
%42 = load i32, i32* %41
%43 = load %struct.addrinfo*, %struct.addrinfo** %8
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 2
%45 = load i32, i32* %44
%46 = call i32 @socket(i32 %39, i32 %42, i32 %45)
store i32 %46, i32* %10
%47 = load i32, i32* %10
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %36
%50 = call i32 @sysError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %51
51: ; preds = %49, %36
%52 = load i32, i32* %10
%53 = load %struct.addrinfo*, %struct.addrinfo** %8
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 1
%55 = load i32, i32* %54
%56 = load %struct.addrinfo*, %struct.addrinfo** %8
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i64 @bind(i32 %52, i32 %55, i32 %58)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %51
%62 = call i32 @sysError(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %63
63: ; preds = %61, %51
%64 = load %struct.addrinfo*, %struct.addrinfo** %8
%65 = call i32 @freeaddrinfo(%struct.addrinfo* %64)
%66 = load i32, i32* %10
%67 = call i32 @getSockName(i32 %66)
%68 = load i32, i32* %10
%69 = call i32 @acceptClient(i32 %68)
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32 }
@.str = [14 x i8] c"Usage:\09<Port>\00"
@AF_UNSPEC = global i32 0
@AI_PASSIVE = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.1 = [24 x i8] c"failed to create socket\00"
@.str.2 = [23 x i8] c"failed to bind to port\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @usrError(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = bitcast %struct.addrinfo* %3 to i8*
%12 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 24)
%13 = load i32, i32* @AF_UNSPEC
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %13, i32* %14
%15 = load i32, i32* @AI_PASSIVE
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 5
store i32 %15, i32* %16
%17 = load i32, i32* @SOCK_DGRAM
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %17, i32* %18
%19 = load i32, i32* @IPPROTO_UDP
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32 %19, i32* %20
%21 = bitcast %struct.addrinfo** %4 to i8*
%22 = call i32 @getaddrinfo(i32* null, i8* %10, %struct.addrinfo* %3, %struct.addrinfo** %4)
%23 = icmp eq i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %8
%25 = call i8* @gai_strerror(i32 %22)
%26 = call i32 @sysError(i8* %25)
br label %27
27: ; preds = %24, %8
%28 = load %struct.addrinfo*, %struct.addrinfo** %4
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 4
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 3
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 2
%34 = load i32, i32* %33
%35 = call i32 @socket(i32 %30, i32 %32, i32 %34)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %27
%38 = call i32 @sysError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %39
39: ; preds = %37, %27
%40 = load %struct.addrinfo*, %struct.addrinfo** %4
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i64 0, i32 1
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i64 0, i32 0
%44 = load i32, i32* %43
%45 = call i64 @bind(i32 %35, i32 %42, i32 %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %39
%48 = call i32 @sysError(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %49
49: ; preds = %47, %39
%50 = load %struct.addrinfo*, %struct.addrinfo** %4
%51 = call i32 @freeaddrinfo(%struct.addrinfo* %50)
%52 = call i32 @getSockName(i32 %35)
%53 = call i32 @acceptClient(i32 %35)
ret i32 0
}