#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_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memcpy(struct addrinfo *, struct addrinfo *, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int socketUDP(const char *targetHost, int targetPort,
              struct addrinfo *pAddrInfo) {
  struct addrinfo hints;
  struct addrinfo *servinfo;
  struct addrinfo *p;
  int sockfd = -1;
  int rv;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_DGRAM;
  char portBuf[32];
  snprintf(portBuf, 32, "%d", targetPort);
  if ((rv = getaddrinfo(targetHost, portBuf, &hints, &servinfo)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    return -1;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
      perror("socket");
      continue;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "failed to bind socket!\n");
    freeaddrinfo(servinfo);
    return -1;
  }
  memcpy(pAddrInfo, p, sizeof(struct addrinfo));
  freeaddrinfo(servinfo);
  return sockfd;
}


x86 assembly (clang, -O0):
<socketUDP>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0xffffffff,-0x4c(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x20,%esi
lea    0xe18(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x70(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x50(%rbp)
cmp    $0x0,%eax
je     1242 <socketUDP+0xb2>
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x50(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x74(%rbp),%edi
mov    %rax,%rdx
lea    0xdd4(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f9 <socketUDP+0x169>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12a1 <socketUDP+0x111>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0xffffffff,%eax
jne    128b <socketUDP+0xfb>
lea    0xd93(%rip),%rdi
call   1080 <perror@plt>
jmp    1290 <socketUDP+0x100>
jmp    12a1 <socketUDP+0x111>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    124a <socketUDP+0xba>
cmpq   $0x0,-0x48(%rbp)
jne    12d8 <socketUDP+0x148>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
lea    0xd5f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x40(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f9 <socketUDP+0x169>
mov    -0x20(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    $0x18,%edx
call   1070 <memcpy@plt>
mov    -0x40(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketUDP>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
mov    %rdi,%r12
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0xe27(%rip),%rdx
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x20,%esi
mov    %ebx,%ecx
call   1030 <snprintf@plt>
mov    %rsp,%rcx
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     122c <socketUDP+0x9c>
mov    0x2dc7(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xdea(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    12a1 <socketUDP+0x111>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     125f <socketUDP+0xcf>
lea    0xdd8(%rip),%r15
mov    0x8(%rbx),%edi
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
jne    1286 <socketUDP+0xf6>
mov    %r15,%rdi
call   1080 <perror@plt>
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
jne    123c <socketUDP+0xac>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
lea    0xdac(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    $0xffffffff,%ebp
jmp    12a1 <socketUDP+0x111>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x18,%edx
call   1070 <memcpy@plt>
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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