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

/* Variables and functions */
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ close(int);
int connect(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ get_in_addr(struct sockaddr *);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                             int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int getsock(struct addrinfo *info) {
  struct addrinfo *p;
  int sockfd;
  char ipstr[INET6_ADDRSTRLEN];
  for (p = info; p != NULL; p = p->ai_next) {
    if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
      continue;
    }
    if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
      close(sockfd);
      continue;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "[net] client failed to connect.\n");
    exit(8);
  }
  inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), ipstr,
            sizeof ipstr);
  printf("[net\tinfo] connecting to %s...\n", ipstr);
  printf("[net\tinfo] connected.\n");
  return sockfd;
}


x86 assembly (clang, -O0):
<getsock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x38(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     123c <getsock+0xbc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11fa <getsock+0x7a>
jmp    122b <getsock+0xab>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    1226 <getsock+0xa6>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
jmp    122b <getsock+0xab>
jmp    123c <getsock+0xbc>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x10(%rbp)
jmp    11c4 <getsock+0x44>
cmpq   $0x0,-0x10(%rbp)
jne    1266 <getsock+0xe6>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xda9(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x8,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1040 <get_in_addr@plt>
mov    -0x3c(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %eax,%esi
mov    -0x38(%rbp),%rax
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x30(%rbp),%rsi
lea    0xd86(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getsock>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e43(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r15
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
test   %rdi,%rdi
je     1254 <getsock+0xc4>
mov    %rdi,%rbx
jmp    11e1 <getsock+0x51>
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
call   1070 <close@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1254 <getsock+0xc4>
mov    0x14(%rbx),%esi
mov    0x8(%rbx),%edi
mov    0x10(%rbx),%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     11d8 <getsock+0x48>
mov    %eax,%r12d
mov    (%rbx),%rsi
mov    0xc(%rbx),%edx
mov    %eax,%edi
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
je     11d0 <getsock+0x40>
mov    0x8(%rbx),%r13d
mov    (%rbx),%rdi
call   1040 <get_in_addr@plt>
mov    %r13d,%edi
mov    %eax,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1030 <inet_ntop@plt>
lea    0xdf5(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe04(%rip),%rdi
call   1050 <puts@plt>
mov    %r12d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
lea    0xd9c(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x8,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i64, i32, i32, i32, i32, %struct.addrinfo* }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"[net] client failed to connect.\0A\00"
@.str.1 = [32 x i8] c"[net\09info] connecting to %s...\0A\00"
@.str.2 = [23 x i8] c"[net\09info] connected.\0A\00"
define i32 @getsock(%struct.addrinfo* %0) {
%2 = alloca %struct.addrinfo*
%3 = alloca %struct.addrinfo*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
store %struct.addrinfo* %0, %struct.addrinfo** %2
%7 = load i32, i32* @INET6_ADDRSTRLEN
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %5
%10 = alloca i8, i64 %8
store i64 %8, i64* %6
%11 = load %struct.addrinfo*, %struct.addrinfo** %2
store %struct.addrinfo* %11, %struct.addrinfo** %3
br label %12
12: ; preds = %42, %1
%13 = load %struct.addrinfo*, %struct.addrinfo** %3
%14 = icmp ne %struct.addrinfo* %13, null
br i1 %14, label %15, label %46
15: ; preds = %12
%16 = load %struct.addrinfo*, %struct.addrinfo** %3
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i32 0, i32 1
%18 = load i32, i32* %17
%19 = load %struct.addrinfo*, %struct.addrinfo** %3
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i32 0, i32 4
%21 = load i32, i32* %20
%22 = load %struct.addrinfo*, %struct.addrinfo** %3
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i32 0, i32 3
%24 = load i32, i32* %23
%25 = call i32 @socket(i32 %18, i32 %21, i32 %24)
store i32 %25, i32* %4
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %28
27: ; preds = %15
br label %42
28: ; preds = %15
%29 = load i32, i32* %4
%30 = load %struct.addrinfo*, %struct.addrinfo** %3
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i32 0, i32 0
%32 = load i64, i64* %31
%33 = load %struct.addrinfo*, %struct.addrinfo** %3
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i32 0, i32 2
%35 = load i32, i32* %34
%36 = call i32 @connect(i32 %29, i64 %32, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %28
%39 = load i32, i32* %4
%40 = call i32 @close(i32 %39)
br label %42
41: ; preds = %28
br label %46
42: ; preds = %38, %27
%43 = load %struct.addrinfo*, %struct.addrinfo** %3
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 5
%45 = load %struct.addrinfo*, %struct.addrinfo** %44
store %struct.addrinfo* %45, %struct.addrinfo** %3
br label %12
46: ; preds = %41, %12
%47 = load %struct.addrinfo*, %struct.addrinfo** %3
%48 = icmp eq %struct.addrinfo* %47, null
br i1 %48, label %49, label %53
49: ; preds = %46
%50 = load i32, i32* @stderr
%51 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%52 = call i32 @exit(i32 8)
unreachable
53: ; preds = %46
%54 = load %struct.addrinfo*, %struct.addrinfo** %3
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %54, i32 0, i32 1
%56 = load i32, i32* %55
%57 = load %struct.addrinfo*, %struct.addrinfo** %3
%58 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %57, i32 0, i32 0
%59 = load i64, i64* %58
%60 = inttoptr i64 %59 to %struct.sockaddr*
%61 = call i32 @get_in_addr(%struct.sockaddr* %60)
%62 = trunc i64 %8 to i32
%63 = call i32 @inet_ntop(i32 %56, i32 %61, i8* %10, i32 %62)
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%66 = load i32, i32* %4
%67 = load i8*, i8** %5
ret i32 %66
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i32, i32, i32, %struct.addrinfo* }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"[net] client failed to connect.\0A\00"
@.str.1 = [32 x i8] c"[net\09info] connecting to %s...\0A\00"
@str = [22 x i8] c"[net\09info] connected.\00"
define i32 @getsock(%struct.addrinfo* %0) {
%2 = load i32, i32* @INET6_ADDRSTRLEN
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = icmp eq %struct.addrinfo* %0, null
br i1 %5, label %29, label %6
6: ; preds = %1, %25
%7 = phi %struct.addrinfo* [ %27, %25 ], [ %0, %1 ]
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 1
%9 = load i32, i32* %8
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 4
%11 = load i32, i32* %10
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 3
%13 = load i32, i32* %12
%14 = tail call i32 @socket(i32 %9, i32 %11, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %25, label %16
16: ; preds = %6
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 0
%18 = load i64, i64* %17
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 2
%20 = load i32, i32* %19
%21 = tail call i32 @connect(i32 %14, i64 %18, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %33
23: ; preds = %16
%24 = tail call i32 @close(i32 %14)
br label %25
25: ; preds = %6, %23
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 5
%27 = load %struct.addrinfo*, %struct.addrinfo** %26
%28 = icmp eq %struct.addrinfo* %27, null
br i1 %28, label %29, label %6
29: ; preds = %25, %1
%30 = load i32, i32* @stderr
%31 = tail call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%32 = tail call i32 @exit(i32 8)
unreachable
33: ; preds = %16
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 1
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 0
%36 = load i32, i32* %34
%37 = load i64, i64* %35
%38 = inttoptr i64 %37 to %struct.sockaddr*
%39 = tail call i32 @get_in_addr(%struct.sockaddr* %38)
%40 = call i32 @inet_ntop(i32 %36, i32 %39, i8* %4, i32 %2)
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
ret i32 %14
}