#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*/ fnShowUsage(char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char *, 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 fnInitSocket(int argc, char *argv[], struct sockaddr *dest_addr,
                 socklen_t *addrlen) {
  int sockfd, state;
  struct addrinfo hints, *serv_addr;
  if (argc < 2) {
    fnShowUsage(argv[0], "Insufficient arguments");
    return -1;
  }
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_DGRAM;
  if ((state = getaddrinfo(argv[1], "6249", &hints, &serv_addr)) != 0) {
    perror("getaddrinfo");
    return -1;
  }
  if ((sockfd = socket(serv_addr->ai_family, serv_addr->ai_socktype,
                       serv_addr->ai_protocol)) < 0) {
    perror("socket");
    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    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11a0 <fnInitSocket+0x40>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe71(%rip),%rsi
call   1040 <fnShowUsage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1271 <fnInitSocket+0x111>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe40(%rip),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
je     1208 <fnInitSocket+0xa8>
lea    0xe25(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1271 <fnInitSocket+0x111>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1246 <fnInitSocket+0xe6>
lea    0xdf3(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1271 <fnInitSocket+0x111>
mov    -0x18(%rbp),%rax
mov    -0x50(%rbp),%rcx
mov    0x8(%rcx),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
mov    -0x50(%rbp),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x50(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    -0x24(%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,%rbp
cmp    $0x1,%edi
jg     1189 <fnInitSocket+0x29>
mov    0x0(%rbp),%rdi
lea    0xe83(%rip),%rsi
call   1040 <fnShowUsage@plt>
mov    $0xffffffff,%ebp
jmp    1205 <fnInitSocket+0xa5>
mov    %rcx,%r14
mov    %rdx,%rbx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x8(%rbp),%rdi
lea    0xe4f(%rip),%rsi
mov    %rsp,%rcx
mov    %r15,%rdx
call   1060 <getaddrinfo@plt>
test   %eax,%eax
jne    1212 <fnInitSocket+0xb2>
mov    (%rsp),%rax
mov    0x18(%rax),%edi
mov    0x10(%rax),%edx
mov    0x14(%rax),%esi
call   1080 <socket@plt>
test   %eax,%eax
js     121b <fnInitSocket+0xbb>
mov    %eax,%ebp
mov    (%rsp),%rdi
mov    0x8(%rdi),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
mov    (%rdi),%eax
mov    %eax,(%r14)
call   1070 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe03(%rip),%rdi
jmp    1222 <fnInitSocket+0xc2>
lea    0xe06(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1205 <fnInitSocket+0xa5>

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

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