#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 /*<<< orphan*/ AF_UNSPEC;
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int /*<<< orphan*/ get_in_addr(struct sockaddr *);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                             int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int zkp_connect(char *hostname) {
  int sockfd, numbytes;
  char buf[1024];
  struct addrinfo hints, *servinfo, *p;
  int rv;
  char s[INET6_ADDRSTRLEN];
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((rv = getaddrinfo(hostname, "3490", &hints, &servinfo)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    return 1;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
      perror("ZKP Client: socket");
      continue;
    }
    if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
      close(sockfd);
      perror("ZKP Client: connect");
      continue;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "ZKP Client: failed to connect\n");
    return 2;
  }
  inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s,
            sizeof s);
  freeaddrinfo(servinfo);
  return sockfd;
}


x86 assembly (clang, -O0):
<zkp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x490,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x480(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x460(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x478(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x468(%rbp)
lea    -0x440(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x438(%rbp)
mov    0x2da4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x42c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdc9(%rip),%rsi
lea    -0x440(%rbp),%rdx
lea    -0x448(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x454(%rbp)
cmp    $0x0,%eax
je     12a0 <zkp_connect+0xf0>
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x484(%rbp)
mov    -0x454(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x484(%rbp),%edi
mov    %rax,%rdx
lea    0xd82(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x46c(%rbp)
jmp    13e9 <zkp_connect+0x239>
mov    -0x448(%rbp),%rax
mov    %rax,-0x450(%rbp)
cmpq   $0x0,-0x450(%rbp)
je     1356 <zkp_connect+0x1a6>
mov    -0x450(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x450(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x450(%rbp),%rax
mov    0x10(%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    12fc <zkp_connect+0x14c>
lea    0xd24(%rip),%rdi
call   1090 <perror@plt>
jmp    133f <zkp_connect+0x18f>
mov    -0x14(%rbp),%edi
mov    -0x450(%rbp),%rax
mov    (%rax),%rsi
mov    -0x450(%rbp),%rax
mov    0xc(%rax),%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
jne    133a <zkp_connect+0x18a>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
lea    0xcf9(%rip),%rdi
call   1090 <perror@plt>
jmp    133f <zkp_connect+0x18f>
jmp    1356 <zkp_connect+0x1a6>
mov    -0x450(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x450(%rbp)
jmp    12ae <zkp_connect+0xfe>
cmpq   $0x0,-0x450(%rbp)
jne    1391 <zkp_connect+0x1e1>
mov    0x2c65(%rip),%rax
mov    (%rax),%edi
lea    0xcc9(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
movl   $0x1,-0x46c(%rbp)
jmp    13e9 <zkp_connect+0x239>
mov    -0x450(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x488(%rbp)
mov    -0x450(%rbp),%rax
mov    (%rax),%rdi
call   1040 <get_in_addr@plt>
mov    -0x488(%rbp),%edi
mov    -0x478(%rbp),%rdx
mov    %eax,%esi
mov    -0x480(%rbp),%rax
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x448(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x46c(%rbp)
mov    -0x460(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zkp_connect>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%rbx
mov    %rsp,%r13
mov    0x2e1a(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r15
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    -0x58(%rbp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
lea    0xdec(%rip),%rsi
lea    -0x38(%rbp),%rcx
mov    %rbx,%rdi
mov    %r12,%rdx
call   10b0 <getaddrinfo@plt>
test   %eax,%eax
je     1254 <zkp_connect+0xa4>
mov    0x2da2(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xdc7(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%ebx
jmp    12fb <zkp_connect+0x14b>
mov    %r13,-0x30(%rbp)
mov    -0x38(%rbp),%r13
test   %r13,%r13
je     12b3 <zkp_connect+0x103>
lea    0xdae(%rip),%r12
mov    0x14(%r13),%esi
mov    0x8(%r13),%edi
mov    0x10(%r13),%edx
call   10d0 <socket@plt>
mov    %r12,%rdi
cmp    $0xffffffff,%eax
je     12a5 <zkp_connect+0xf5>
mov    %eax,%ebx
mov    0x0(%r13),%rsi
mov    0xc(%r13),%edx
mov    %eax,%edi
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12d1 <zkp_connect+0x121>
mov    %ebx,%edi
call   1070 <close@plt>
lea    0xd84(%rip),%rdi
call   1090 <perror@plt>
mov    0x18(%r13),%r13
test   %r13,%r13
jne    1268 <zkp_connect+0xb8>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
lea    0xd7a(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x2,%ebx
jmp    12f7 <zkp_connect+0x147>
mov    0x8(%r13),%r12d
mov    0x0(%r13),%rdi
call   1040 <get_in_addr@plt>
mov    %r12d,%edi
mov    %eax,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1030 <inet_ntop@plt>
mov    -0x38(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    -0x30(%rbp),%r13
mov    %r13,%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i64, i32, i32, i32, i32, %struct.addrinfo* }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [5 x i8] c"3490\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.2 = [19 x i8] c"ZKP Client: socket\00"
@.str.3 = [20 x i8] c"ZKP Client: connect\00"
@.str.4 = [31 x i8] c"ZKP Client: failed to connect\0A\00"
define i32 @zkp_connect(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
%9 = alloca %struct.addrinfo*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store i8* %0, i8** %3
%14 = load i32, i32* @INET6_ADDRSTRLEN
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %11
%17 = alloca i8, i64 %15
store i64 %15, i64* %12
%18 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 32)
%19 = load i32, i32* @AF_UNSPEC
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @SOCK_STREAM
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 4
store i32 %21, i32* %22
%23 = load i8*, i8** %3
%24 = call i32 @getaddrinfo(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %7, %struct.addrinfo** %8)
store i32 %24, i32* %10
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %1
%27 = load i32, i32* @stderr
%28 = load i32, i32* %10
%29 = call i8* @gai_strerror(i32 %28)
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %29)
store i32 1, i32* %2
store i32 1, i32* %13
br label %89
31: ; preds = %1
%32 = load %struct.addrinfo*, %struct.addrinfo** %8
store %struct.addrinfo* %32, %struct.addrinfo** %9
br label %33
33: ; preds = %65, %31
%34 = load %struct.addrinfo*, %struct.addrinfo** %9
%35 = icmp ne %struct.addrinfo* %34, null
br i1 %35, label %36, label %69
36: ; preds = %33
%37 = load %struct.addrinfo*, %struct.addrinfo** %9
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 1
%39 = load i32, i32* %38
%40 = load %struct.addrinfo*, %struct.addrinfo** %9
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 4
%42 = load i32, i32* %41
%43 = load %struct.addrinfo*, %struct.addrinfo** %9
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 3
%45 = load i32, i32* %44
%46 = call i32 @socket(i32 %39, i32 %42, i32 %45)
store i32 %46, i32* %4
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %36
%49 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %65
50: ; preds = %36
%51 = load i32, i32* %4
%52 = load %struct.addrinfo*, %struct.addrinfo** %9
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %52, i32 0, i32 0
%54 = load i64, i64* %53
%55 = load %struct.addrinfo*, %struct.addrinfo** %9
%56 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i32 0, i32 2
%57 = load i32, i32* %56
%58 = call i32 @connect(i32 %51, i64 %54, i32 %57)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %64
60: ; preds = %50
%61 = load i32, i32* %4
%62 = call i32 @close(i32 %61)
%63 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %65
64: ; preds = %50
br label %69
65: ; preds = %60, %48
%66 = load %struct.addrinfo*, %struct.addrinfo** %9
%67 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %66, i32 0, i32 5
%68 = load %struct.addrinfo*, %struct.addrinfo** %67
store %struct.addrinfo* %68, %struct.addrinfo** %9
br label %33
69: ; preds = %64, %33
%70 = load %struct.addrinfo*, %struct.addrinfo** %9
%71 = icmp eq %struct.addrinfo* %70, null
br i1 %71, label %72, label %75
72: ; preds = %69
%73 = load i32, i32* @stderr
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
store i32 2, i32* %2
store i32 1, i32* %13
br label %89
75: ; preds = %69
%76 = load %struct.addrinfo*, %struct.addrinfo** %9
%77 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %76, i32 0, i32 1
%78 = load i32, i32* %77
%79 = load %struct.addrinfo*, %struct.addrinfo** %9
%80 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %79, i32 0, i32 0
%81 = load i64, i64* %80
%82 = inttoptr i64 %81 to %struct.sockaddr*
%83 = call i32 @get_in_addr(%struct.sockaddr* %82)
%84 = trunc i64 %15 to i32
%85 = call i32 @inet_ntop(i32 %78, i32 %83, i8* %17, i32 %84)
%86 = load %struct.addrinfo*, %struct.addrinfo** %8
%87 = call i32 @freeaddrinfo(%struct.addrinfo* %86)
%88 = load i32, i32* %4
store i32 %88, i32* %2
store i32 1, i32* %13
br label %89
89: ; preds = %75, %72, %26
%90 = load i8*, i8** %11
%91 = load i32, i32* %2
ret i32 %91
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i32, i32, i32, %struct.addrinfo* }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [5 x i8] c"3490\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.2 = [19 x i8] c"ZKP Client: socket\00"
@.str.3 = [20 x i8] c"ZKP Client: connect\00"
@.str.4 = [31 x i8] c"ZKP Client: failed to connect\0A\00"
define i32 @zkp_connect(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = bitcast %struct.addrinfo* %2 to i8*
%5 = bitcast %struct.addrinfo** %3 to i8*
%6 = load i32, i32* @INET6_ADDRSTRLEN
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 32)
%11 = load i32, i32* @AF_UNSPEC
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 1
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %13, i32* %14
%15 = call i32 @getaddrinfo(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %2, %struct.addrinfo** %3)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = load %struct.addrinfo*, %struct.addrinfo** %3
%19 = icmp eq %struct.addrinfo* %18, null
br i1 %19, label %49, label %24
20: ; preds = %1
%21 = load i32, i32* @stderr
%22 = call i8* @gai_strerror(i32 %15)
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %62
24: ; preds = %17, %43
%25 = phi %struct.addrinfo* [ %47, %43 ], [ %18, %17 ]
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 1
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 4
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 3
%31 = load i32, i32* %30
%32 = call i32 @socket(i32 %27, i32 %29, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %43, label %34
34: ; preds = %24
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 0
%36 = load i64, i64* %35
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 2
%38 = load i32, i32* %37
%39 = call i32 @connect(i32 %32, i64 %36, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %52
41: ; preds = %34
%42 = call i32 @close(i32 %32)
br label %43
43: ; preds = %24, %41
%44 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), %41 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), %24 ]
%45 = call i32 @perror(i8* %44)
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 5
%47 = load %struct.addrinfo*, %struct.addrinfo** %46
%48 = icmp eq %struct.addrinfo* %47, null
br i1 %48, label %49, label %24
49: ; preds = %43, %17
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %62
52: ; preds = %34
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 1
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 0
%55 = load i32, i32* %53
%56 = load i64, i64* %54
%57 = inttoptr i64 %56 to %struct.sockaddr*
%58 = call i32 @get_in_addr(%struct.sockaddr* %57)
%59 = call i32 @inet_ntop(i32 %55, i32 %58, i8* %9, i32 %6)
%60 = load %struct.addrinfo*, %struct.addrinfo** %3
%61 = call i32 @freeaddrinfo(%struct.addrinfo* %60)
br label %62
62: ; preds = %52, %49, %20
%63 = phi i32 [ 1, %20 ], [ 2, %49 ], [ %32, %52 ]
ret i32 %63
}