#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;
scalar_t__ asprintf(char **, char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ connect_qps(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
scalar_t__ recv_remote_dest(int, int /*<<< orphan*/);
scalar_t__ send_local_dest(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int pp_client_connect(const char *servername, int port) {
  struct addrinfo *res, *t;
  char *service;
  int ret;
  int sockfd = -1;
  struct addrinfo hints = {.ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM};
  if (asprintf(&service, "%d", port) < 0)
    return 1;
  ret = getaddrinfo(servername, service, &hints, &res);
  if (ret < 0) {
    fprintf(stderr, "%s for %s:%d\n", gai_strerror(ret), servername, port);
    free(service);
    return 1;
  }
  for (t = res; t; t = t->ai_next) {
    sockfd = socket(t->ai_family, t->ai_socktype, t->ai_protocol);
    if (sockfd >= 0) {
      if (!connect(sockfd, t->ai_addr, t->ai_addrlen))
        break;
      close(sockfd);
      sockfd = -1;
    }
  }
  freeaddrinfo(res);
  free(service);
  if (sockfd < 0) {
    fprintf(stderr, "Couldn't connect to %s:%d\n", servername, port);
    return 1;
  }
  if (send_local_dest(sockfd, 0))
    return 1;
  if (recv_remote_dest(sockfd, 0))
    return 1;
  if (connect_qps(0))
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<pp_client_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0xffffffff,-0x38(%rbp)
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x14(%rbp),%edx
lea    -0x30(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   10a0 <asprintf@plt>
cmp    $0x0,%rax
jge    1237 <pp_client_connect+0x67>
movl   $0x1,-0x4(%rbp)
jmp    13ca <pp_client_connect+0x1fa>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
lea    -0x58(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   10c0 <getaddrinfo@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    129e <pp_client_connect+0xce>
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x34(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x5c(%rbp),%edi
mov    %rax,%rdx
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%r8d
lea    0xd81(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ca <pp_client_connect+0x1fa>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1320 <pp_client_connect+0x150>
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x28(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%edx
call   10f0 <socket@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jl     130a <pp_client_connect+0x13a>
mov    -0x38(%rbp),%edi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   10b0 <connect@plt>
cmp    $0x0,%eax
jne    12fb <pp_client_connect+0x12b>
jmp    1320 <pp_client_connect+0x150>
mov    -0x38(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x38(%rbp)
jmp    130f <pp_client_connect+0x13f>
mov    -0x28(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    12a6 <pp_client_connect+0xd6>
mov    -0x20(%rbp),%rdi
call   10e0 <freeaddrinfo@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x38(%rbp)
jge    1366 <pp_client_connect+0x196>
mov    0x2c95(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xcbe(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ca <pp_client_connect+0x1fa>
mov    -0x38(%rbp),%edi
xor    %esi,%esi
call   10d0 <send_local_dest@plt>
cmp    $0x0,%rax
je     1386 <pp_client_connect+0x1b6>
movl   $0x1,-0x4(%rbp)
jmp    13ca <pp_client_connect+0x1fa>
mov    -0x38(%rbp),%edi
xor    %esi,%esi
call   1090 <recv_remote_dest@plt>
cmp    $0x0,%rax
je     13a6 <pp_client_connect+0x1d6>
movl   $0x1,-0x4(%rbp)
jmp    13ca <pp_client_connect+0x1fa>
xor    %edi,%edi
call   1040 <connect_qps@plt>
cmp    $0x0,%rax
je     13c3 <pp_client_connect+0x1f3>
movl   $0x1,-0x4(%rbp)
jmp    13ca <pp_client_connect+0x1fa>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pp_client_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %esi,%r12d
mov    %rdi,%r15
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,0x20(%rsp)
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
lea    0xe00(%rip),%rsi
mov    %rsp,%rdi
mov    %r12d,%edx
call   1090 <asprintf@plt>
mov    $0x1,%r14d
test   %rax,%rax
js     1321 <pp_client_connect+0x161>
mov    (%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %r15,%rdi
call   10b0 <getaddrinfo@plt>
test   %eax,%eax
js     12d0 <pp_client_connect+0x110>
mov    0x8(%rsp),%rbp
test   %rbp,%rbp
jne    1265 <pp_client_connect+0xa5>
mov    $0xffffffff,%ebx
xor    %edi,%edi
jmp    128f <pp_client_connect+0xcf>
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
je     128a <pp_client_connect+0xca>
mov    0x10(%rbp),%edi
mov    0x8(%rbp),%edx
mov    0xc(%rbp),%esi
call   10e0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     125c <pp_client_connect+0x9c>
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
mov    %ebx,%edi
call   10a0 <connect@plt>
test   %eax,%eax
jne    1250 <pp_client_connect+0x90>
mov    0x8(%rsp),%rdi
call   10d0 <freeaddrinfo@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
test   %ebx,%ebx
js     1304 <pp_client_connect+0x144>
mov    %ebx,%edi
xor    %esi,%esi
call   10c0 <send_local_dest@plt>
test   %rax,%rax
jne    1321 <pp_client_connect+0x161>
mov    %ebx,%edi
xor    %esi,%esi
call   1080 <recv_remote_dest@plt>
test   %rax,%rax
jne    1321 <pp_client_connect+0x161>
xor    %edi,%edi
call   1040 <connect_qps@plt>
xor    %r14d,%r14d
test   %rax,%rax
setne  %r14b
jmp    1321 <pp_client_connect+0x161>
mov    0x2d01(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xd1c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
mov    %r15,%rcx
mov    %r12d,%r8d
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
jmp    1321 <pp_client_connect+0x161>
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
lea    0xcfd(%rip),%rsi
mov    %r15,%rdx
mov    %r12d,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r14d,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@SOCK_STREAM = global i32 0
@AF_UNSPEC = global i32 0
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [14 x i8] c"%s for %s:%d\0A\00"
@.str.2 = [27 x i8] c"Couldn't connect to %s:%d\0A\00"
define i32 @pp_client_connect(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.addrinfo*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.addrinfo
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 -1, i32* %10
%12 = bitcast %struct.addrinfo* %11 to i8*
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i32 0, i32 3
%14 = load i32, i32* @SOCK_STREAM
store i32 %14, i32* %13
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i32 0, i32 4
%16 = load i32, i32* @AF_UNSPEC
store i32 %16, i32* %15
%17 = load i32, i32* %5
%18 = call i64 @asprintf(i8** %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %2
store i32 1, i32* %3
br label %100
21: ; preds = %2
%22 = load i8*, i8** %4
%23 = load i8*, i8** %8
%24 = call i32 @getaddrinfo(i8* %22, i8* %23, %struct.addrinfo* %11, %struct.addrinfo** %6)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = load i32, i32* %9
%30 = call i8* @gai_strerror(i32 %29)
%31 = load i8*, i8** %4
%32 = load i32, i32* %5
%33 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %30, i8* %31, i32 %32)
%34 = load i8*, i8** %8
%35 = call i32 @free(i8* %34)
store i32 1, i32* %3
br label %100
36: ; preds = %21
%37 = load %struct.addrinfo*, %struct.addrinfo** %6
store %struct.addrinfo* %37, %struct.addrinfo** %7
br label %38
38: ; preds = %69, %36
%39 = load %struct.addrinfo*, %struct.addrinfo** %7
%40 = icmp ne %struct.addrinfo* %39, null
br i1 %40, label %41, label %73
41: ; preds = %38
%42 = load %struct.addrinfo*, %struct.addrinfo** %7
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %42, i32 0, i32 4
%44 = load i32, i32* %43
%45 = load %struct.addrinfo*, %struct.addrinfo** %7
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 3
%47 = load i32, i32* %46
%48 = load %struct.addrinfo*, %struct.addrinfo** %7
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 2
%50 = load i32, i32* %49
%51 = call i32 @socket(i32 %44, i32 %47, i32 %50)
store i32 %51, i32* %10
%52 = load i32, i32* %10
%53 = icmp sge i32 %52, 0
br i1 %53, label %54, label %68
54: ; preds = %41
%55 = load i32, i32* %10
%56 = load %struct.addrinfo*, %struct.addrinfo** %7
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i32 0, i32 1
%58 = load i32, i32* %57
%59 = load %struct.addrinfo*, %struct.addrinfo** %7
%60 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %59, i32 0, i32 0
%61 = load i32, i32* %60
%62 = call i32 @connect(i32 %55, i32 %58, i32 %61)
%63 = icmp ne i32 %62, 0
br i1 %63, label %65, label %64
64: ; preds = %54
br label %73
65: ; preds = %54
%66 = load i32, i32* %10
%67 = call i32 @close(i32 %66)
store i32 -1, i32* %10
br label %68
68: ; preds = %65, %41
br label %69
69: ; preds = %68
%70 = load %struct.addrinfo*, %struct.addrinfo** %7
%71 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %70, i32 0, i32 5
%72 = load %struct.addrinfo*, %struct.addrinfo** %71
store %struct.addrinfo* %72, %struct.addrinfo** %7
br label %38
73: ; preds = %64, %38
%74 = load %struct.addrinfo*, %struct.addrinfo** %6
%75 = call i32 @freeaddrinfo(%struct.addrinfo* %74)
%76 = load i8*, i8** %8
%77 = call i32 @free(i8* %76)
%78 = load i32, i32* %10
%79 = icmp slt i32 %78, 0
br i1 %79, label %80, label %85
80: ; preds = %73
%81 = load i32, i32* @stderr
%82 = load i8*, i8** %4
%83 = load i32, i32* %5
%84 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %81, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %82, i32 %83)
store i32 1, i32* %3
br label %100
85: ; preds = %73
%86 = load i32, i32* %10
%87 = call i64 @send_local_dest(i32 %86, i32 0)
%88 = icmp ne i64 %87, 0
br i1 %88, label %89, label %90
89: ; preds = %85
store i32 1, i32* %3
br label %100
90: ; preds = %85
%91 = load i32, i32* %10
%92 = call i64 @recv_remote_dest(i32 %91, i32 0)
%93 = icmp ne i64 %92, 0
br i1 %93, label %94, label %95
94: ; preds = %90
store i32 1, i32* %3
br label %100
95: ; preds = %90
%96 = call i64 @connect_qps(i32 0)
%97 = icmp ne i64 %96, 0
br i1 %97, label %98, label %99
98: ; preds = %95
store i32 1, i32* %3
br label %100
99: ; preds = %95
store i32 0, i32* %3
br label %100
100: ; preds = %99, %98, %94, %89, %80, %27, %20
%101 = load i32, i32* %3
ret i32 %101
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@SOCK_STREAM = global i32 0
@AF_UNSPEC = global i32 0
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [14 x i8] c"%s for %s:%d\0A\00"
@.str.2 = [27 x i8] c"Couldn't connect to %s:%d\0A\00"
define i32 @pp_client_connect(i8* %0, i32 %1) {
%3 = alloca %struct.addrinfo*
%4 = alloca i8*
%5 = alloca %struct.addrinfo
%6 = bitcast %struct.addrinfo** %3 to i8*
%7 = bitcast i8** %4 to i8*
%8 = bitcast %struct.addrinfo* %5 to i8*
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 3
%10 = load i32, i32* @SOCK_STREAM
store i32 %10, i32* %9
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 4
%12 = load i32, i32* @AF_UNSPEC
store i32 %12, i32* %11
%13 = call i64 @asprintf(i8** %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%14 = icmp slt i64 %13, 0
br i1 %14, label %75, label %15
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i32 @getaddrinfo(i8* %0, i8* %16, %struct.addrinfo* %5, %struct.addrinfo** %3)
%18 = icmp slt i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = load %struct.addrinfo*, %struct.addrinfo** %3
%21 = icmp eq %struct.addrinfo* %20, null
br i1 %21, label %55, label %28
22: ; preds = %15
%23 = load i32, i32* @stderr
%24 = call i8* @gai_strerror(i32 %17)
%25 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8* %0, i32 %1)
%26 = load i8*, i8** %4
%27 = call i32 @free(i8* %26)
br label %75
28: ; preds = %19, %47
%29 = phi %struct.addrinfo* [ %50, %47 ], [ %20, %19 ]
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i64 0, i32 4
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i64 0, i32 3
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i64 0, i32 2
%35 = load i32, i32* %34
%36 = call i32 @socket(i32 %31, i32 %33, i32 %35)
%37 = icmp sgt i32 %36, -1
br i1 %37, label %38, label %47
38: ; preds = %28
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i64 0, i32 1
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i64 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @connect(i32 %36, i32 %40, i32 %42)
%44 = icmp eq i32 %43, 0
br i1 %44, label %52, label %45
45: ; preds = %38
%46 = call i32 @close(i32 %36)
br label %47
47: ; preds = %28, %45
%48 = phi i32 [ -1, %45 ], [ %36, %28 ]
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i64 0, i32 5
%50 = load %struct.addrinfo*, %struct.addrinfo** %49
%51 = icmp eq %struct.addrinfo* %50, null
br i1 %51, label %52, label %28
52: ; preds = %38, %47
%53 = phi i32 [ %48, %47 ], [ %36, %38 ]
%54 = load %struct.addrinfo*, %struct.addrinfo** %3
br label %55
55: ; preds = %52, %19
%56 = phi %struct.addrinfo* [ null, %19 ], [ %54, %52 ]
%57 = phi i32 [ -1, %19 ], [ %53, %52 ]
%58 = call i32 @freeaddrinfo(%struct.addrinfo* %56)
%59 = load i8*, i8** %4
%60 = call i32 @free(i8* %59)
%61 = icmp slt i32 %57, 0
br i1 %61, label %62, label %65
62: ; preds = %55
%63 = load i32, i32* @stderr
%64 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %1)
br label %75
65: ; preds = %55
%66 = call i64 @send_local_dest(i32 %57, i32 0)
%67 = icmp eq i64 %66, 0
br i1 %67, label %68, label %75
68: ; preds = %65
%69 = call i64 @recv_remote_dest(i32 %57, i32 0)
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %75
71: ; preds = %68
%72 = call i64 @connect_qps(i32 0)
%73 = icmp ne i64 %72, 0
%74 = zext i1 %73 to i32
br label %75
75: ; preds = %71, %68, %65, %2, %62, %22
%76 = phi i32 [ 1, %22 ], [ 1, %62 ], [ 1, %2 ], [ 1, %65 ], [ 1, %68 ], [ %74, %71 ]
ret i32 %76
}