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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to_server(char *address, char *portNo) {
  int sockfd;
  struct addrinfo hints;
  struct addrinfo *servinfo;
  struct addrinfo *p;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if (getaddrinfo(address, portNo, &hints, &servinfo) != 0) {
    printf("getddrinfo\n");
    return -1;
  }
  for (p = servinfo; p != 0; p = p->ai_next) {
    (void)printf("Finding suitable servinfo\n");
    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 == 0) {
    printf("p==0, errno: %d\n", errno);
    return -1;
  }
  freeaddrinfo(servinfo);
  return sockfd;
}


x86 assembly (clang, -O0):
<connect_to_server>:
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   1040 <memset@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1070 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11e1 <connect_to_server+0x71>
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ab <connect_to_server+0x13b>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     126e <connect_to_server+0xfe>
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    122d <connect_to_server+0xbd>
jmp    125d <connect_to_server+0xed>
mov    -0x1c(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    1258 <connect_to_server+0xe8>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
jmp    125d <connect_to_server+0xed>
jmp    126e <connect_to_server+0xfe>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    11e9 <connect_to_server+0x79>
cmpq   $0x0,-0x50(%rbp)
jne    129c <connect_to_server+0x12c>
mov    0x2d50(%rip),%rax
mov    (%rax),%esi
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ab <connect_to_server+0x13b>
mov    -0x48(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_server>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1080 <getaddrinfo@plt>
test   %rax,%rax
je     11ea <connect_to_server+0x6a>
lea    0xe50(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
jmp    1268 <connect_to_server+0xe8>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     1240 <connect_to_server+0xc0>
lea    0xe16(%rip),%r14
jmp    1209 <connect_to_server+0x89>
nopl   (%rax)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1240 <connect_to_server+0xc0>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1200 <connect_to_server+0x80>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    125e <connect_to_server+0xde>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1200 <connect_to_server+0x80>
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
lea    0xdb0(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebp
jmp    1268 <connect_to_server+0xe8>
mov    0x8(%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [17 x i8] c"p==0, errno: %d\0A\00"
@errno = global i32 0
@str = [26 x i8] c"Finding suitable servinfo\00"
@str.3 = [11 x i8] c"getddrinfo\00"
define i32 @connect_to_server(i8* %0, i8* %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_UNSPEC
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %10, i32* %11
%12 = call i64 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load %struct.addrinfo*, %struct.addrinfo** %4
%16 = icmp eq %struct.addrinfo* %15, null
br i1 %16, label %43, label %19
17: ; preds = %2
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.3, i64 0, i64 0))
br label %49
19: ; preds = %14, %39
%20 = phi %struct.addrinfo* [ %41, %39 ], [ %15, %14 ]
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 4
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 3
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 2
%27 = load i32, i32* %26
%28 = call i32 @socket(i32 %23, i32 %25, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %39, label %30
30: ; preds = %19
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 1
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @connect(i32 %28, i32 %32, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %46
37: ; preds = %30
%38 = call i32 @close(i32 %28)
br label %39
39: ; preds = %19, %37
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 5
%41 = load %struct.addrinfo*, %struct.addrinfo** %40
%42 = icmp eq %struct.addrinfo* %41, null
br i1 %42, label %43, label %19
43: ; preds = %39, %14
%44 = load i32, i32* @errno
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %44)
br label %49
46: ; preds = %30
%47 = load %struct.addrinfo*, %struct.addrinfo** %4
%48 = call i32 @freeaddrinfo(%struct.addrinfo* %47)
br label %49
49: ; preds = %46, %43, %17
%50 = phi i32 [ -1, %17 ], [ -1, %43 ], [ %28, %46 ]
ret i32 %50
}