#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 sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct addrinfo {
  int ai_flags;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ *ai_addr;
  int /*<<< orphan*/ *ai_next;
  int /*<<< orphan*/ *ai_canonname;
  scalar_t__ ai_protocol;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int AI_NUMERICSERV;
int AI_PASSIVE;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *gai_strerror(int);
int getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int setupUdpV2() {
  int socket_fd, ecode;
  struct sockaddr_in *s_in;
  struct addrinfo hints, *addrlist;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_flags = AI_NUMERICSERV | AI_PASSIVE;
  hints.ai_protocol = 0;
  hints.ai_canonname = NULL;
  hints.ai_addr = NULL;
  hints.ai_next = NULL;
  ecode = getaddrinfo(NULL, "0", &hints, &addrlist);
  if (ecode != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ecode));
    exit(1);
  }
  s_in = (struct sockaddr_in *)addrlist->ai_addr;
  socket_fd = socket(addrlist->ai_family, addrlist->ai_socktype, 0);
  if (socket_fd < 0) {
    perror("recv_udp:socket");
    exit(1);
  }
  if (bind(socket_fd, (struct sockaddr *)s_in, sizeof(struct sockaddr_in)) <
      0) {
    perror("recv_udp:bind");
    exit(1);
  }
  return socket_fd;
}


x86 assembly (clang, -O0):
<setupUdpV2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1040 <memset@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    0x2e20(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x40(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe10(%rip),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     123a <setupUdpV2+0xba>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xdd7(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x10(%rbp)
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    127e <setupUdpV2+0xfe>
lea    0xda4(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    12af <setupUdpV2+0x12f>
lea    0xd83(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setupUdpV2>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1040 <memset@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e3d(%rip),%rax
mov    0x2e1e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
movups %xmm0,0x28(%rsp)
lea    0xe22(%rip),%rsi
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
jne    1227 <setupUdpV2+0xa7>
mov    (%rsp),%rax
mov    0x10(%rax),%rbx
mov    0x4(%rax),%esi
mov    0x8(%rax),%edi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1252 <setupUdpV2+0xd2>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     125b <setupUdpV2+0xdb>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d92(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xdc4(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdba(%rip),%rdi
jmp    1262 <setupUdpV2+0xe2>
lea    0xdc1(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32*, i32*, i32*, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@AI_NUMERICSERV = global i32 0
@AI_PASSIVE = global i32 0
@.str = [2 x i8] c"0\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.2 = [16 x i8] c"recv_udp:socket\00"
@.str.3 = [14 x i8] c"recv_udp:bind\00"
define i32 @setupUdpV2() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in*
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 48)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 2
store i32 %7, i32* %8
%9 = load i32, i32* @SOCK_DGRAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 1
store i32 %9, i32* %10
%11 = load i32, i32* @AI_NUMERICSERV
%12 = load i32, i32* @AI_PASSIVE
%13 = or i32 %11, %12
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 0
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 6
store i64 0, i64* %15
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 5
store i32* null, i32** %16
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 3
store i32* null, i32** %17
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 4
store i32* null, i32** %18
%19 = call i32 @getaddrinfo(i32* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %4, %struct.addrinfo** %5)
store i32 %19, i32* %2
%20 = load i32, i32* %2
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %0
%23 = load i32, i32* @stderr
%24 = load i32, i32* %2
%25 = call i8* @gai_strerror(i32 %24)
%26 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %0
%29 = load %struct.addrinfo*, %struct.addrinfo** %5
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 3
%31 = load i32*, i32** %30
%32 = bitcast i32* %31 to %struct.sockaddr_in*
store %struct.sockaddr_in* %32, %struct.sockaddr_in** %3
%33 = load %struct.addrinfo*, %struct.addrinfo** %5
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i32 0, i32 2
%35 = load i32, i32* %34
%36 = load %struct.addrinfo*, %struct.addrinfo** %5
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 1
%38 = load i32, i32* %37
%39 = call i32 @socket(i32 %35, i32 %38, i32 0)
store i32 %39, i32* %1
%40 = load i32, i32* %1
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %28
%43 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %28
%46 = load i32, i32* %1
%47 = load %struct.sockaddr_in*, %struct.sockaddr_in** %3
%48 = bitcast %struct.sockaddr_in* %47 to %struct.sockaddr*
%49 = call i64 @bind(i32 %46, %struct.sockaddr* %48, i32 4)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %45
%52 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %45
%55 = load i32, i32* %1
ret i32 %55
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32*, i32*, i32*, i64 }
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@AI_NUMERICSERV = global i32 0
@AI_PASSIVE = global i32 0
@.str = [2 x i8] c"0\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.2 = [16 x i8] c"recv_udp:socket\00"
@.str.3 = [14 x i8] c"recv_udp:bind\00"
define i32 @setupUdpV2() {
%1 = alloca %struct.addrinfo
%2 = alloca %struct.addrinfo*
%3 = bitcast %struct.addrinfo* %1 to i8*
%4 = bitcast %struct.addrinfo** %2 to i8*
%5 = call i32 @memset(%struct.addrinfo* %1, i32 0, i32 48)
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 2
store i32 %6, i32* %7
%8 = load i32, i32* @SOCK_DGRAM
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 1
store i32 %8, i32* %9
%10 = load i32, i32* @AI_NUMERICSERV
%11 = load i32, i32* @AI_PASSIVE
%12 = or i32 %11, %10
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 0
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 3
%15 = bitcast i32** %14 to i8*
%16 = call i32 @getaddrinfo(i32* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %1, %struct.addrinfo** %2)
%17 = icmp eq i32 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %0
%19 = load i32, i32* @stderr
%20 = call i8* @gai_strerror(i32 %16)
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %0
%24 = load %struct.addrinfo*, %struct.addrinfo** %2
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 3
%26 = bitcast i32** %25 to %struct.sockaddr**
%27 = load %struct.sockaddr*, %struct.sockaddr** %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 2
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 1
%31 = load i32, i32* %30
%32 = call i32 @socket(i32 %29, i32 %31, i32 0)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %23
%35 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %23
%38 = call i64 @bind(i32 %32, %struct.sockaddr* %27, i32 4)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %37
ret i32 %32
}