#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;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ PF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
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 /*<<< orphan*/ stderr;
int connect_to_ser2net(void) {
  int ret, s;
  struct addrinfo hints, *ai, *ai0;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = PF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((ret = getaddrinfo("192.168.1.1", "2001", &hints, &ai0)) != 0) {
    fprintf(stderr, "getaddrinfo(192.168.1.2:2001): %s", gai_strerror(ret));
    return -1;
  }
  for (s = -1, ai = ai0; ai; ai = ai->ai_next) {
    s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
    if (s < 0)
      continue;
    if (connect(s, ai->ai_addr, ai->ai_addrlen) != -1)
      break;
    close(s);
    s = -1;
  }
  freeaddrinfo(ai0);
  return s;
}


x86 assembly (clang, -O0):
<connect_to_ser2net>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    0xe49(%rip),%rdi
lea    0xe4e(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
je     1209 <connect_to_ser2net+0x89>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xe19(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a1 <connect_to_ser2net+0x121>
movl   $0xffffffff,-0xc(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1292 <connect_to_ser2net+0x112>
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x38(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    124f <connect_to_ser2net+0xcf>
jmp    1281 <connect_to_ser2net+0x101>
mov    -0xc(%rbp),%edi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     1272 <connect_to_ser2net+0xf2>
jmp    1292 <connect_to_ser2net+0x112>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0xc(%rbp)
mov    -0x38(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    1218 <connect_to_ser2net+0x98>
mov    -0x40(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_ser2net>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0xe45(%rip),%rdi
lea    0xe4a(%rip),%rsi
mov    %rsp,%rcx
mov    %rbx,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     11f9 <connect_to_ser2net+0x79>
mov    0x2e00(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe29(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1255 <connect_to_ser2net+0xd5>
mov    (%rsp),%rbx
test   %rbx,%rbx
jne    1219 <connect_to_ser2net+0x99>
mov    $0xffffffff,%ebp
xor    %edi,%edi
jmp    1250 <connect_to_ser2net+0xd0>
nopl   0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     124c <connect_to_ser2net+0xcc>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10a0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1210 <connect_to_ser2net+0x90>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    124c <connect_to_ser2net+0xcc>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebp
jmp    1210 <connect_to_ser2net+0x90>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [12 x i8] c"192.168.1.1\00"
@.str.1 = [5 x i8] c"2001\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"getaddrinfo(192.168.1.2:2001): %s\00"
define i32 @connect_to_ser2net() {
%1 = alloca %struct.addrinfo
%2 = alloca %struct.addrinfo*
%3 = bitcast %struct.addrinfo* %1 to i8*
%4 = bitcast %struct.addrinfo** %2 to i8*
%5 = call i32 @memset(%struct.addrinfo* %1, i32 0, i32 32)
%6 = load i32, i32* @PF_UNSPEC
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 4
store i32 %6, i32* %7
%8 = load i32, i32* @SOCK_STREAM
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 3
store i32 %8, i32* %9
%10 = call i32 @getaddrinfo(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %1, %struct.addrinfo** %2)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = load %struct.addrinfo*, %struct.addrinfo** %2
%14 = icmp eq %struct.addrinfo* %13, null
br i1 %14, label %46, label %19
15: ; preds = %0
%16 = load i32, i32* @stderr
%17 = call i8* @gai_strerror(i32 %10)
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %17)
br label %50
19: ; preds = %12, %38
%20 = phi %struct.addrinfo* [ %41, %38 ], [ %13, %12 ]
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 4
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 3
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 2
%26 = load i32, i32* %25
%27 = call i32 @socket(i32 %22, i32 %24, i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %38, label %29
29: ; preds = %19
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 1
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @connect(i32 %27, i32 %31, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %43
36: ; preds = %29
%37 = call i32 @close(i32 %27)
br label %38
38: ; preds = %19, %36
%39 = phi i32 [ %27, %19 ], [ -1, %36 ]
%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 = %29, %38
%44 = phi i32 [ %39, %38 ], [ %27, %29 ]
%45 = load %struct.addrinfo*, %struct.addrinfo** %2
br label %46
46: ; preds = %43, %12
%47 = phi %struct.addrinfo* [ null, %12 ], [ %45, %43 ]
%48 = phi i32 [ -1, %12 ], [ %44, %43 ]
%49 = call i32 @freeaddrinfo(%struct.addrinfo* %47)
br label %50
50: ; preds = %46, %15
%51 = phi i32 [ -1, %15 ], [ %48, %46 ]
ret i32 %51
}