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

/* Variables and functions */
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ add_cache(char *, struct addrinfo *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
struct addrinfo *get_cache(char *, char *);
scalar_t__ getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ set_conn_socket(struct connection *, int);
int tcp_connected(struct connection *);
int try_connect(struct addrinfo *, int *);
int connect_socket(struct connection *conn, char *hostname, char *port) {
  int sock = -1, deferred;
  struct addrinfo hints, *result, *r;
  r = get_cache(hostname, port);
  if (r)
    sock = try_connect(r, &deferred);
  if (sock < 0) {
    memset(&hints, 0, sizeof(hints));
    hints.ai_socktype = SOCK_STREAM;
    if (getaddrinfo(hostname, port, &hints, &result)) {
      printf("Unable to get host %s\n", hostname);
      return -1;
    }
    for (r = result; r; r = r->ai_next) {
      sock = try_connect(r, &deferred);
      if (sock >= 0)
        break;
    }
    if (r) {
      add_cache(hostname, r);
      freeaddrinfo(result);
    } else {
      freeaddrinfo(result);
      printf("Unable to get socket for host %s\n", hostname);
      return -1;
    }
  }
  if (!set_conn_socket(conn, sock)) {
    printf("Problems! Could not set socket\n");
    close(sock);
    return -1;
  }
  if (deferred)
    return 0;
  else
    return tcp_connected(conn);
}


x86 assembly (clang, -O0):
<connect_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <get_cache@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     11e7 <connect_socket+0x47>
mov    -0x48(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <try_connect@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    12e3 <connect_socket+0x143>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
cmp    $0x0,%rax
je     124a <connect_socket+0xaa>
mov    -0x18(%rbp),%rsi
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133c <connect_socket+0x19c>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1291 <connect_socket+0xf1>
mov    -0x48(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <try_connect@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     127c <connect_socket+0xdc>
jmp    1291 <connect_socket+0xf1>
jmp    1281 <connect_socket+0xe1>
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1252 <connect_socket+0xb2>
cmpq   $0x0,-0x48(%rbp)
je     12b7 <connect_socket+0x117>
mov    -0x18(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   10b0 <add_cache@plt>
mov    -0x40(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
jmp    12de <connect_socket+0x13e>
mov    -0x40(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    -0x18(%rbp),%rsi
lea    0xd4c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133c <connect_socket+0x19c>
jmp    12e3 <connect_socket+0x143>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1090 <set_conn_socket@plt>
cmp    $0x0,%eax
jne    131a <connect_socket+0x17a>
lea    0xd3a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133c <connect_socket+0x19c>
cmpl   $0x0,-0x28(%rbp)
je     1330 <connect_socket+0x190>
movl   $0x0,-0x4(%rbp)
jmp    133c <connect_socket+0x19c>
mov    -0x10(%rbp),%rdi
call   1060 <tcp_connected@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
mov    %rdx,%rsi
call   1080 <get_cache@plt>
test   %rax,%rax
je     1214 <connect_socket+0x64>
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
call   1090 <try_connect@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1214 <connect_socket+0x64>
mov    %r14,%rdi
mov    %ebp,%esi
call   10a0 <set_conn_socket@plt>
test   %eax,%eax
je     128e <connect_socket+0xde>
xor    %eax,%eax
cmpl   $0x0,0x4(%rsp)
jne    12c0 <connect_socket+0x110>
mov    %r14,%rdi
call   1070 <tcp_connected@plt>
jmp    12c0 <connect_socket+0x110>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
lea    0x8(%rsp),%rcx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   10b0 <getaddrinfo@plt>
test   %rax,%rax
je     1256 <connect_socket+0xa6>
lea    0xdac(%rip),%rdi
jmp    12b1 <connect_socket+0x101>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     12a3 <connect_socket+0xf3>
lea    0x4(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %r12,%rsi
call   1090 <try_connect@plt>
test   %eax,%eax
jns    12cd <connect_socket+0x11d>
mov    (%rbx),%rbx
test   %rbx,%rbx
jne    1270 <connect_socket+0xc0>
mov    0x8(%rsp),%rdi
jmp    12a5 <connect_socket+0xf5>
lea    0xda4(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    12bb <connect_socket+0x10b>
xor    %edi,%edi
call   10d0 <freeaddrinfo@plt>
lea    0xd66(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
mov    %r15,%rdi
mov    %rbx,%rsi
call   10c0 <add_cache@plt>
mov    0x8(%rsp),%rdi
call   10d0 <freeaddrinfo@plt>
jmp    11e8 <connect_socket+0x38>

LLVM IR (-O0):
%struct.connection = type { i32 }
%struct.addrinfo = type { %struct.addrinfo*, i32 }
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"Unable to get host %s\0A\00"
@.str.1 = [34 x i8] c"Unable to get socket for host %s\0A\00"
@.str.2 = [32 x i8] c"Problems! Could not set socket\0A\00"
define i32 @connect_socket(%struct.connection* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.connection*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.addrinfo
%11 = alloca %struct.addrinfo*
%12 = alloca %struct.addrinfo*
store %struct.connection* %0, %struct.connection** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 -1, i32* %8
%13 = load i8*, i8** %6
%14 = load i8*, i8** %7
%15 = call %struct.addrinfo* @get_cache(i8* %13, i8* %14)
store %struct.addrinfo* %15, %struct.addrinfo** %12
%16 = load %struct.addrinfo*, %struct.addrinfo** %12
%17 = icmp ne %struct.addrinfo* %16, null
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = load %struct.addrinfo*, %struct.addrinfo** %12
%20 = call i32 @try_connect(%struct.addrinfo* %19, i32* %9)
store i32 %20, i32* %8
br label %21
21: ; preds = %18, %3
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %66
24: ; preds = %21
%25 = call i32 @memset(%struct.addrinfo* %10, i32 0, i32 16)
%26 = load i32, i32* @SOCK_STREAM
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 1
store i32 %26, i32* %27
%28 = load i8*, i8** %6
%29 = load i8*, i8** %7
%30 = call i64 @getaddrinfo(i8* %28, i8* %29, %struct.addrinfo* %10, %struct.addrinfo** %11)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %24
%33 = load i8*, i8** %6
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %33)
store i32 -1, i32* %4
br label %82
35: ; preds = %24
%36 = load %struct.addrinfo*, %struct.addrinfo** %11
store %struct.addrinfo* %36, %struct.addrinfo** %12
br label %37
37: ; preds = %47, %35
%38 = load %struct.addrinfo*, %struct.addrinfo** %12
%39 = icmp ne %struct.addrinfo* %38, null
br i1 %39, label %40, label %51
40: ; preds = %37
%41 = load %struct.addrinfo*, %struct.addrinfo** %12
%42 = call i32 @try_connect(%struct.addrinfo* %41, i32* %9)
store i32 %42, i32* %8
%43 = load i32, i32* %8
%44 = icmp sge i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %40
br label %51
46: ; preds = %40
br label %47
47: ; preds = %46
%48 = load %struct.addrinfo*, %struct.addrinfo** %12
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 0
%50 = load %struct.addrinfo*, %struct.addrinfo** %49
store %struct.addrinfo* %50, %struct.addrinfo** %12
br label %37
51: ; preds = %45, %37
%52 = load %struct.addrinfo*, %struct.addrinfo** %12
%53 = icmp ne %struct.addrinfo* %52, null
br i1 %53, label %54, label %60
54: ; preds = %51
%55 = load i8*, i8** %6
%56 = load %struct.addrinfo*, %struct.addrinfo** %12
%57 = call i32 @add_cache(i8* %55, %struct.addrinfo* %56)
%58 = load %struct.addrinfo*, %struct.addrinfo** %11
%59 = call i32 @freeaddrinfo(%struct.addrinfo* %58)
br label %65
60: ; preds = %51
%61 = load %struct.addrinfo*, %struct.addrinfo** %11
%62 = call i32 @freeaddrinfo(%struct.addrinfo* %61)
%63 = load i8*, i8** %6
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %63)
store i32 -1, i32* %4
br label %82
65: ; preds = %54
br label %66
66: ; preds = %65, %21
%67 = load %struct.connection*, %struct.connection** %5
%68 = load i32, i32* %8
%69 = call i32 @set_conn_socket(%struct.connection* %67, i32 %68)
%70 = icmp ne i32 %69, 0
br i1 %70, label %75, label %71
71: ; preds = %66
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%73 = load i32, i32* %8
%74 = call i32 @close(i32 %73)
store i32 -1, i32* %4
br label %82
75: ; preds = %66
%76 = load i32, i32* %9
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %79
78: ; preds = %75
store i32 0, i32* %4
br label %82
79: ; preds = %75
%80 = load %struct.connection*, %struct.connection** %5
%81 = call i32 @tcp_connected(%struct.connection* %80)
store i32 %81, i32* %4
br label %82
82: ; preds = %79, %78, %71, %60, %32
%83 = load i32, i32* %4
ret i32 %83
}

LLVM IR (-O2):
%struct.connection = type { i32 }
%struct.addrinfo = type { %struct.addrinfo*, i32 }
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"Unable to get host %s\0A\00"
@.str.1 = [34 x i8] c"Unable to get socket for host %s\0A\00"
@str = [31 x i8] c"Problems! Could not set socket\00"
define i32 @connect_socket(%struct.connection* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.addrinfo
%6 = alloca %struct.addrinfo*
%7 = bitcast i32* %4 to i8*
%8 = bitcast %struct.addrinfo* %5 to i8*
%9 = bitcast %struct.addrinfo** %6 to i8*
%10 = tail call %struct.addrinfo* @get_cache(i8* %1, i8* %2)
%11 = icmp eq %struct.addrinfo* %10, null
br i1 %11, label %15, label %12
12: ; preds = %3
%13 = call i32 @try_connect(%struct.addrinfo* %10, i32* %4)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %44
15: ; preds = %3, %12
%16 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 16)
%17 = load i32, i32* @SOCK_STREAM
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 1
store i32 %17, i32* %18
%19 = call i64 @getaddrinfo(i8* %1, i8* %2, %struct.addrinfo* %5, %struct.addrinfo** %6)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = load %struct.addrinfo*, %struct.addrinfo** %6
%23 = icmp eq %struct.addrinfo* %22, null
br i1 %23, label %40, label %26
24: ; preds = %15
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %56
26: ; preds = %21, %30
%27 = phi %struct.addrinfo* [ %32, %30 ], [ %22, %21 ]
%28 = call i32 @try_connect(%struct.addrinfo* %27, i32* %4)
%29 = icmp sgt i32 %28, -1
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 0
%32 = load %struct.addrinfo*, %struct.addrinfo** %31
%33 = icmp eq %struct.addrinfo* %32, null
br i1 %33, label %38, label %26
34: ; preds = %26
%35 = call i32 @add_cache(i8* %1, %struct.addrinfo* %27)
%36 = load %struct.addrinfo*, %struct.addrinfo** %6
%37 = call i32 @freeaddrinfo(%struct.addrinfo* %36)
br label %44
38: ; preds = %30
%39 = load %struct.addrinfo*, %struct.addrinfo** %6
br label %40
40: ; preds = %38, %21
%41 = phi %struct.addrinfo* [ %39, %38 ], [ null, %21 ]
%42 = call i32 @freeaddrinfo(%struct.addrinfo* %41)
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %56
44: ; preds = %34, %12
%45 = phi i32 [ %28, %34 ], [ %13, %12 ]
%46 = call i32 @set_conn_socket(%struct.connection* %0, i32 %45)
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%50 = call i32 @close(i32 %45)
br label %56
51: ; preds = %44
%52 = load i32, i32* %4
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = call i32 @tcp_connected(%struct.connection* %0)
br label %56
56: ; preds = %51, %54, %48, %40, %24
%57 = phi i32 [ -1, %24 ], [ %55, %54 ], [ -1, %48 ], [ -1, %40 ], [ 0, %51 ]
ret i32 %57
}