#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*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ 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_server(char *host, char *port) {
  int conn;
  struct addrinfo hints, *res, *scan;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  if (getaddrinfo(host, port, &hints, &res) != 0) {
    fprintf(stderr, "Failed to lookup name : %s\n", host);
    return -1;
  }
  for (scan = res; scan != NULL; scan = scan->ai_next) {
    if ((conn = socket(scan->ai_family, scan->ai_socktype, scan->ai_protocol)) >
        0) {
      if ((connect(conn, scan->ai_addr, scan->ai_addrlen)) == 0) {
        freeaddrinfo(res);
        return conn;
      }
    }
  }
  freeaddrinfo(res);
  fprintf(stderr, "Failed to connect to server at %s:%s\n", host, port);
  return -2;
}


x86 assembly (clang, -O0):
<connect_to_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1060 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11de <connect_to_server+0x7e>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe35(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <connect_to_server+0x134>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1265 <connect_to_server+0x105>
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    124f <connect_to_server+0xef>
mov    -0x1c(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
jne    124a <connect_to_server+0xea>
mov    -0x48(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1294 <connect_to_server+0x134>
jmp    124f <connect_to_server+0xef>
jmp    1254 <connect_to_server+0xf4>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    11e6 <connect_to_server+0x86>
mov    -0x48(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xd96(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_server>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %rsp,%rcx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
call   1060 <getaddrinfo@plt>
test   %rax,%rax
je     11d5 <connect_to_server+0x75>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1254 <connect_to_server+0xf4>
mov    (%rsp),%rbx
test   %rbx,%rbx
jne    11f9 <connect_to_server+0x99>
xor    %edi,%edi
jmp    122d <connect_to_server+0xcd>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1229 <connect_to_server+0xc9>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1080 <socket@plt>
test   %eax,%eax
jle    11f0 <connect_to_server+0x90>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1050 <connect@plt>
test   %rax,%rax
jne    11f0 <connect_to_server+0x90>
mov    (%rsp),%rdi
call   1070 <freeaddrinfo@plt>
jmp    1254 <connect_to_server+0xf4>
mov    (%rsp),%rdi
call   1070 <freeaddrinfo@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
lea    0xdda(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xfffffffe,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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