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

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
scalar_t__ PF_UNSPEC;
scalar_t__ SK_connect_inner(int *, struct addrinfo *, unsigned int, char *,
                            unsigned int);
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, unsigned int,
                           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 /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int /*<<< orphan*/ stderr;
int SK_connect(char *hostname, unsigned int port, unsigned int timeout) {
  struct addrinfo hints, *res, *ai_iter;
  char portbuf[16];
  int socktemp, *sock = &socktemp, error;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = PF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  snprintf(portbuf, 16, "%u", port);
  error = getaddrinfo(hostname, portbuf, &hints, &res);
  if (error != 0) {
    fprintf(stderr, "SK_connect(%s, %d): getaddrinfo(): %s", hostname, port,
            gai_strerror(error));
    return -1;
  }
  for (ai_iter = res; ai_iter != NULL; ai_iter = ai_iter->ai_next) {
    if (ai_iter->ai_family == AF_INET) {
      if (SK_connect_inner(sock, ai_iter, timeout, hostname, port) == 0) {
        freeaddrinfo(res);
        return socktemp;
      }
    }
  }
  for (ai_iter = res; ai_iter != NULL; ai_iter = ai_iter->ai_next) {
    if (ai_iter->ai_family == AF_INET6) {
      if (SK_connect_inner(sock, ai_iter, timeout, hostname, port) == 0) {
        freeaddrinfo(res);
        return socktemp;
      }
    }
  }
  freeaddrinfo(res);
  return -1;
}


x86 assembly (clang, -O0):
<SK_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
lea    -0x54(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x10,%esi
lea    0xe36(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
je     1238 <SK_connect+0xc8>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    -0x64(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x74(%rbp),%edi
mov    -0x70(%rbp),%rdx
mov    -0x68(%rbp),%ecx
mov    %rax,%r8
lea    0xddc(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <SK_connect+0x1ce>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12b3 <SK_connect+0x143>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    0x2d87(%rip),%rcx
cmp    (%rcx),%rax
jne    129d <SK_connect+0x12d>
mov    -0x60(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%r8d
call   1080 <SK_connect_inner@plt>
cmp    $0x0,%rax
jne    1298 <SK_connect+0x128>
mov    -0x38(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    133e <SK_connect+0x1ce>
jmp    129d <SK_connect+0x12d>
jmp    12a2 <SK_connect+0x132>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    1240 <SK_connect+0xd0>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     132e <SK_connect+0x1be>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    0x2d04(%rip),%rcx
cmp    (%rcx),%rax
jne    1318 <SK_connect+0x1a8>
mov    -0x60(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%r8d
call   1080 <SK_connect_inner@plt>
cmp    $0x0,%rax
jne    1313 <SK_connect+0x1a3>
mov    -0x38(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    133e <SK_connect+0x1ce>
jmp    1318 <SK_connect+0x1a8>
jmp    131d <SK_connect+0x1ad>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    12bb <SK_connect+0x14b>
mov    -0x38(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SK_connect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%rbx
lea    0x18(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x18(%rsp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
lea    0xe42(%rip),%rdx
lea    0x30(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x10,%esi
mov    %r15d,%ecx
call   1030 <snprintf@plt>
lea    0x10(%rsp),%rcx
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     1216 <SK_connect+0xa6>
mov    0x2dd7(%rip),%rcx
mov    (%rcx),%ebp
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe02(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %r15d,%ecx
mov    %rax,%r8
call   1060 <fprintf@plt>
jmp    12da <SK_connect+0x16a>
mov    0x10(%rsp),%rbp
test   %rbp,%rbp
je     12cc <SK_connect+0x15c>
mov    0x2db5(%rip),%r13
mov    0x0(%r13),%rax
lea    0xc(%rsp),%r12
jmp    1249 <SK_connect+0xd9>
cs nopw 0x0(%rax,%rax,1)
mov    0x8(%rbp),%rbp
test   %rbp,%rbp
je     126e <SK_connect+0xfe>
cmp    %rax,0x0(%rbp)
jne    1240 <SK_connect+0xd0>
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
mov    %rbx,%rcx
mov    %r15d,%r8d
call   1080 <SK_connect_inner@plt>
test   %rax,%rax
je     12bc <SK_connect+0x14c>
mov    0x0(%r13),%rax
jmp    1240 <SK_connect+0xd0>
mov    0x10(%rsp),%rbp
test   %rbp,%rbp
je     12cc <SK_connect+0x15c>
mov    0x2d59(%rip),%r13
mov    0x0(%r13),%rax
lea    0xc(%rsp),%r12
jmp    129d <SK_connect+0x12d>
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13),%rax
mov    0x8(%rbp),%rbp
test   %rbp,%rbp
je     12d0 <SK_connect+0x160>
cmp    %rax,0x0(%rbp)
jne    1294 <SK_connect+0x124>
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
mov    %rbx,%rcx
mov    %r15d,%r8d
call   1080 <SK_connect_inner@plt>
test   %rax,%rax
jne    1290 <SK_connect+0x120>
mov    0x10(%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    0xc(%rsp),%eax
jmp    12df <SK_connect+0x16f>
xor    %edi,%edi
jmp    12d5 <SK_connect+0x165>
mov    0x10(%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    $0xffffffff,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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