#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;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int connect_to_server(const char *host, const char *service) {
  struct addrinfo connection_hints = {
      .ai_family = AF_UNSPEC,
      .ai_socktype = SOCK_STREAM,
  };
  struct addrinfo *server_addrs;
  int addr_res = getaddrinfo(host, service, &connection_hints, &server_addrs);
  if (addr_res != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(addr_res));
    return -1;
  }
  int socket_fd = -1;
  struct addrinfo *addr;
  for (addr = server_addrs; addr != NULL; addr = addr->ai_next) {
    if ((socket_fd = socket(addr->ai_family, addr->ai_socktype,
                            addr->ai_protocol)) < 0) {
      continue;
    }
    if (connect(socket_fd, addr->ai_addr, addr->ai_addrlen) == 0)
      break;
    close(socket_fd);
  }
  freeaddrinfo(server_addrs);
  if (addr == NULL) {
    return -1;
  }
  return socket_fd;
}


x86 assembly (clang, -O0):
<connect_to_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     120c <connect_to_server+0x8c>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x44(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x54(%rbp),%edi
mov    %rax,%rdx
lea    0xe05(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <connect_to_server+0x134>
movl   $0xffffffff,-0x48(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     128e <connect_to_server+0x10e>
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   10a0 <socket@plt>
mov    %eax,-0x48(%rbp)
cmp    $0x0,%eax
jge    1251 <connect_to_server+0xd1>
jmp    127d <connect_to_server+0xfd>
mov    -0x48(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jne    1275 <connect_to_server+0xf5>
jmp    128e <connect_to_server+0x10e>
mov    -0x48(%rbp),%edi
call   1050 <close@plt>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    121b <connect_to_server+0x9b>
mov    -0x40(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
cmpq   $0x0,-0x50(%rbp)
jne    12ae <connect_to_server+0x12e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <connect_to_server+0x134>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_server>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,0x20(%rsp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     11d8 <connect_to_server+0x68>
mov    0x2e21(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe39(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1236 <connect_to_server+0xc6>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
jne    11f9 <connect_to_server+0x89>
mov    $0xffffffff,%ebp
xor    %edi,%edi
jmp    1231 <connect_to_server+0xc1>
nopl   0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1227 <connect_to_server+0xb7>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1090 <socket@plt>
test   %eax,%eax
js     11f0 <connect_to_server+0x80>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1060 <connect@plt>
test   %rax,%rax
je     122c <connect_to_server+0xbc>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11f0 <connect_to_server+0x80>
mov    $0xffffffff,%ebp
mov    0x8(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
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
@stderr = global i32 0
@.str = [17 x i8] c"getaddrinfo: %s\0A\00"
define i32 @connect_to_server(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.addrinfo*
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = bitcast %struct.addrinfo* %6 to i8*
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
%13 = load i32, i32* @SOCK_STREAM
store i32 %13, i32* %12
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
%15 = load i32, i32* @AF_UNSPEC
store i32 %15, i32* %14
%16 = load i8*, i8** %4
%17 = load i8*, i8** %5
%18 = call i32 @getaddrinfo(i8* %16, i8* %17, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %2
%22 = load i32, i32* @stderr
%23 = load i32, i32* %8
%24 = call i8* @gai_strerror(i32 %23)
%25 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %24)
store i32 -1, i32* %3
br label %70
26: ; preds = %2
store i32 -1, i32* %9
%27 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %27, %struct.addrinfo** %10
br label %28
28: ; preds = %58, %26
%29 = load %struct.addrinfo*, %struct.addrinfo** %10
%30 = icmp ne %struct.addrinfo* %29, null
br i1 %30, label %31, label %62
31: ; preds = %28
%32 = load %struct.addrinfo*, %struct.addrinfo** %10
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 4
%34 = load i32, i32* %33
%35 = load %struct.addrinfo*, %struct.addrinfo** %10
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 3
%37 = load i32, i32* %36
%38 = load %struct.addrinfo*, %struct.addrinfo** %10
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 2
%40 = load i32, i32* %39
%41 = call i32 @socket(i32 %34, i32 %37, i32 %40)
store i32 %41, i32* %9
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %31
br label %58
44: ; preds = %31
%45 = load i32, i32* %9
%46 = load %struct.addrinfo*, %struct.addrinfo** %10
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 1
%48 = load i32, i32* %47
%49 = load %struct.addrinfo*, %struct.addrinfo** %10
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i64 @connect(i32 %45, i32 %48, i32 %51)
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %44
br label %62
55: ; preds = %44
%56 = load i32, i32* %9
%57 = call i32 @close(i32 %56)
br label %58
58: ; preds = %55, %43
%59 = load %struct.addrinfo*, %struct.addrinfo** %10
%60 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %59, i32 0, i32 5
%61 = load %struct.addrinfo*, %struct.addrinfo** %60
store %struct.addrinfo* %61, %struct.addrinfo** %10
br label %28
62: ; preds = %54, %28
%63 = load %struct.addrinfo*, %struct.addrinfo** %7
%64 = call i32 @freeaddrinfo(%struct.addrinfo* %63)
%65 = load %struct.addrinfo*, %struct.addrinfo** %10
%66 = icmp eq %struct.addrinfo* %65, null
br i1 %66, label %67, label %68
67: ; preds = %62
store i32 -1, i32* %3
br label %70
68: ; preds = %62
%69 = load i32, i32* %9
store i32 %69, i32* %3
br label %70
70: ; preds = %68, %67, %21
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@SOCK_STREAM = global i32 0
@AF_UNSPEC = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"getaddrinfo: %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 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
%7 = load i32, i32* @SOCK_STREAM
store i32 %7, i32* %6
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
%9 = load i32, i32* @AF_UNSPEC
store i32 %9, i32* %8
%10 = bitcast %struct.addrinfo** %4 to i8*
%11 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load %struct.addrinfo*, %struct.addrinfo** %4
%15 = icmp eq %struct.addrinfo* %14, null
br i1 %15, label %46, label %20
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i8* @gai_strerror(i32 %11)
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %50
20: ; preds = %13, %39
%21 = phi %struct.addrinfo* [ %41, %39 ], [ %14, %13 ]
%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 slt i32 %28, 0
br i1 %29, label %39, label %30
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 %43, label %37
37: ; preds = %30
%38 = call i32 @close(i32 %28)
br label %39
39: ; preds = %20, %37
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 5
%41 = load %struct.addrinfo*, %struct.addrinfo** %40
%42 = icmp eq %struct.addrinfo* %41, null
br i1 %42, label %43, label %20
43: ; preds = %30, %39
%44 = phi i32 [ -1, %39 ], [ %28, %30 ]
%45 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %46
46: ; preds = %43, %13
%47 = phi %struct.addrinfo* [ null, %13 ], [ %45, %43 ]
%48 = phi i32 [ -1, %13 ], [ %44, %43 ]
%49 = call i32 @freeaddrinfo(%struct.addrinfo* %47)
br label %50
50: ; preds = %46, %16
%51 = phi i32 [ -1, %16 ], [ %48, %46 ]
ret i32 %51
}