#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 {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int fnInitSocket(struct sockaddr *dest_addr, socklen_t *addrlen) {
  int sockfd, state;
  struct addrinfo hints, *serv_addr;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_DGRAM;
  if ((state = getaddrinfo("localhost", "6249", &hints, &serv_addr)) != 0)
    return -1;
  if ((sockfd = socket(serv_addr->ai_family, serv_addr->ai_socktype,
                       serv_addr->ai_protocol)) < 0)
    return -1;
  *dest_addr = *((struct sockaddr *)serv_addr->ai_addr);
  *addrlen = serv_addr->ai_addrlen;
  freeaddrinfo(serv_addr);
  return sockfd;
}


x86 assembly (clang, -O0):
<fnInitSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
lea    0xe81(%rip),%rdi
lea    0xe84(%rip),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1040 <getaddrinfo@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     11ab <fnInitSocket+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <fnInitSocket+0xc8>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11dd <fnInitSocket+0x9d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <fnInitSocket+0xc8>
mov    -0x10(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    0x8(%rcx),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
mov    -0x48(%rbp),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x48(%rbp),%rdi
call   1050 <freeaddrinfo@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fnInitSocket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0xe7b(%rip),%rdi
lea    0xe7e(%rip),%rsi
mov    %rsp,%rcx
mov    %rbx,%rdx
call   1040 <getaddrinfo@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
jne    11d1 <fnInitSocket+0x91>
mov    (%rsp),%rax
mov    0x18(%rax),%edi
mov    0x10(%rax),%edx
mov    0x14(%rax),%esi
call   1060 <socket@plt>
test   %eax,%eax
js     11d1 <fnInitSocket+0x91>
mov    %eax,%ebx
mov    (%rsp),%rdi
mov    0x8(%rdi),%rax
mov    (%rax),%eax
mov    %eax,(%r15)
mov    (%rdi),%eax
mov    %eax,(%r14)
call   1050 <freeaddrinfo@plt>
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { i32, i64, i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [10 x i8] c"localhost\00"
@.str.1 = [5 x i8] c"6249\00"
define i32 @fnInitSocket(%struct.sockaddr* %0, i32* %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
store %struct.sockaddr* %0, %struct.sockaddr** %4
store i32* %1, i32** %5
%10 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 32)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_DGRAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i32 %13, i32* %14
%15 = call i32 @getaddrinfo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %15, i32* %7
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -1, i32* %3
br label %46
18: ; preds = %2
%19 = load %struct.addrinfo*, %struct.addrinfo** %9
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i32 0, i32 4
%21 = load i32, i32* %20
%22 = load %struct.addrinfo*, %struct.addrinfo** %9
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i32 0, i32 3
%24 = load i32, i32* %23
%25 = load %struct.addrinfo*, %struct.addrinfo** %9
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i32 0, i32 2
%27 = load i32, i32* %26
%28 = call i32 @socket(i32 %21, i32 %24, i32 %27)
store i32 %28, i32* %6
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %18
store i32 -1, i32* %3
br label %46
31: ; preds = %18
%32 = load %struct.sockaddr*, %struct.sockaddr** %4
%33 = load %struct.addrinfo*, %struct.addrinfo** %9
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i32 0, i32 1
%35 = load i64, i64* %34
%36 = inttoptr i64 %35 to %struct.sockaddr*
%37 = bitcast %struct.sockaddr* %32 to i8*
%38 = bitcast %struct.sockaddr* %36 to i8*
%39 = load %struct.addrinfo*, %struct.addrinfo** %9
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = load i32*, i32** %5
store i32 %41, i32* %42
%43 = load %struct.addrinfo*, %struct.addrinfo** %9
%44 = call i32 @freeaddrinfo(%struct.addrinfo* %43)
%45 = load i32, i32* %6
store i32 %45, i32* %3
br label %46
46: ; preds = %31, %30, %17
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { i32, i64, i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [10 x i8] c"localhost\00"
@.str.1 = [5 x i8] c"6249\00"
define i32 @fnInitSocket(%struct.sockaddr* nocapture writeonly %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 32)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_DGRAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %10, i32* %11
%12 = call i32 @getaddrinfo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %3, %struct.addrinfo** %4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %34
14: ; preds = %2
%15 = load %struct.addrinfo*, %struct.addrinfo** %4
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 4
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 3
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 2
%21 = load i32, i32* %20
%22 = call i32 @socket(i32 %17, i32 %19, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %34, label %24
24: ; preds = %14
%25 = load %struct.addrinfo*, %struct.addrinfo** %4
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 1
%27 = load i64, i64* %26
%28 = inttoptr i64 %27 to i32*
%29 = getelementptr %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%30 = load i32, i32* %28
store i32 %30, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 0
%32 = load i32, i32* %31
store i32 %32, i32* %1
%33 = call i32 @freeaddrinfo(%struct.addrinfo* %25)
br label %34
34: ; preds = %14, %2, %24
%35 = phi i32 [ %22, %24 ], [ -1, %2 ], [ -1, %14 ]
ret i32 %35
}