#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;
  scalar_t__ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ *ai_canonname;
  int /*<<< orphan*/ ai_flags;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, scalar_t__);
int /*<<< orphan*/ stderr;
int tcp_connect(char *host, char *service) {
  int n, s;
  struct addrinfo hints;
  struct addrinfo *aip, *rp;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  hints.ai_protocol = 0;
  hints.ai_canonname = NULL;
  hints.ai_addr = NULL;
  hints.ai_next = NULL;
  n = getaddrinfo(host, service, &hints, &aip);
  if (n != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(n));
    exit(EXIT_FAILURE);
  }
  for (rp = aip; rp != NULL; rp = rp->ai_next) {
    s = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (s == -1)
      continue;
    if (connect(s, rp->ai_addr, rp->ai_addrlen) == 0)
      break;
    close(s);
  }
  if (rp == NULL) {
    perror("socket");
    exit(3);
  }
  return s;
}


x86 assembly (clang, -O0):
<tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     122a <tcp_connect+0xaa>
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x64(%rbp),%edi
mov    %rax,%rdx
lea    0xde9(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     12a8 <tcp_connect+0x128>
mov    -0x60(%rbp),%rax
mov    0x1c(%rax),%edi
mov    -0x60(%rbp),%rax
mov    0x18(%rax),%esi
mov    -0x60(%rbp),%rax
mov    0x10(%rax),%rdx
call   10a0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    126a <tcp_connect+0xea>
jmp    1297 <tcp_connect+0x117>
mov    -0x18(%rbp),%edi
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x60(%rbp),%rax
mov    (%rax),%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jne    128f <tcp_connect+0x10f>
jmp    12a8 <tcp_connect+0x128>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x60(%rbp),%rax
mov    0x20(%rax),%rax
mov    %rax,-0x60(%rbp)
jmp    1232 <tcp_connect+0xb2>
cmpq   $0x0,-0x60(%rbp)
jne    12c9 <tcp_connect+0x149>
lea    0xd57(%rip),%rdi
call   1060 <perror@plt>
mov    $0x3,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_connect>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x40(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
movups %xmm0,0x30(%rsp)
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
jne    1239 <tcp_connect+0xb9>
mov    0x8(%rsp),%rbx
jmp    11e4 <tcp_connect+0x64>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x20(%rbx),%rbx
test   %rbx,%rbx
je     1223 <tcp_connect+0xa3>
mov    0x1c(%rbx),%edi
mov    0x18(%rbx),%esi
mov    0x10(%rbx),%rdx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     11e0 <tcp_connect+0x60>
mov    %eax,%ebp
mov    0x8(%rbx),%rsi
mov    (%rbx),%edx
mov    %eax,%edi
call   1080 <connect@plt>
test   %rax,%rax
je     121a <tcp_connect+0x9a>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11e0 <tcp_connect+0x60>
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xde7(%rip),%rdi
call   1060 <perror@plt>
mov    $0x3,%edi
call   1070 <exit@plt>
mov    0x2d90(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xdb0(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32*, i64, i32, i32, %struct.addrinfo*, i32*, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"getaddrinfo: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [7 x i8] c"socket\00"
define i32 @tcp_connect(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
%9 = alloca %struct.addrinfo*
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i32, i32* @AF_UNSPEC
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 4
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 3
store i32 %12, i32* %13
%14 = load i32, i32* @AI_PASSIVE
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 7
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 2
store i64 0, i64* %16
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 6
store i32* null, i32** %17
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 1
store i32* null, i32** %18
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 5
store %struct.addrinfo* null, %struct.addrinfo** %19
%20 = load i8*, i8** %3
%21 = load i8*, i8** %4
%22 = call i32 @getaddrinfo(i8* %20, i8* %21, %struct.addrinfo* %7, %struct.addrinfo** %8)
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %2
%26 = load i32, i32* @stderr
%27 = load i32, i32* %5
%28 = call i8* @gai_strerror(i32 %27)
%29 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %2
%33 = load %struct.addrinfo*, %struct.addrinfo** %8
store %struct.addrinfo* %33, %struct.addrinfo** %9
br label %34
34: ; preds = %65, %32
%35 = load %struct.addrinfo*, %struct.addrinfo** %9
%36 = icmp ne %struct.addrinfo* %35, null
br i1 %36, label %37, label %69
37: ; preds = %34
%38 = load %struct.addrinfo*, %struct.addrinfo** %9
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 4
%40 = load i32, i32* %39
%41 = load %struct.addrinfo*, %struct.addrinfo** %9
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 3
%43 = load i32, i32* %42
%44 = load %struct.addrinfo*, %struct.addrinfo** %9
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i32 0, i32 2
%46 = load i64, i64* %45
%47 = call i32 @socket(i32 %40, i32 %43, i64 %46)
store i32 %47, i32* %6
%48 = load i32, i32* %6
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %51
50: ; preds = %37
br label %65
51: ; preds = %37
%52 = load i32, i32* %6
%53 = load %struct.addrinfo*, %struct.addrinfo** %9
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 1
%55 = load i32*, i32** %54
%56 = load %struct.addrinfo*, %struct.addrinfo** %9
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i64 @connect(i32 %52, i32* %55, i32 %58)
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %51
br label %69
62: ; preds = %51
%63 = load i32, i32* %6
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %62, %50
%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 %34
69: ; preds = %61, %34
%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 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%74 = call i32 @exit(i32 3)
unreachable
75: ; preds = %69
%76 = load i32, i32* %6
ret i32 %76
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32*, i64, i32, i32, %struct.addrinfo*, i32*, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"getaddrinfo: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [7 x i8] c"socket\00"
define i32 @tcp_connect(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 = load i32, i32* @AF_UNSPEC
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %7, i32* %8
%9 = load i32, i32* @SOCK_STREAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %9, i32* %10
%11 = load i32, i32* @AI_PASSIVE
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 7
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 1
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 5
%15 = bitcast i32** %13 to i8*
%16 = bitcast %struct.addrinfo** %14 to i8*
%17 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = load %struct.addrinfo*, %struct.addrinfo** %4
%21 = icmp eq %struct.addrinfo* %20, null
br i1 %21, label %51, label %28
22: ; preds = %2
%23 = load i32, i32* @stderr
%24 = call i8* @gai_strerror(i32 %17)
%25 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %19, %47
%29 = phi %struct.addrinfo* [ %49, %47 ], [ %20, %19 ]
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i64 0, i32 4
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i64 0, i32 3
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i64 0, i32 2
%35 = load i64, i64* %34
%36 = call i32 @socket(i32 %31, i32 %33, i64 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %47, label %38
38: ; preds = %28
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i64 0, i32 1
%40 = load i32*, i32** %39
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i64 0, i32 0
%42 = load i32, i32* %41
%43 = call i64 @connect(i32 %36, i32* %40, i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %54, label %45
45: ; preds = %38
%46 = call i32 @close(i32 %36)
br label %47
47: ; preds = %28, %45
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i64 0, i32 5
%49 = load %struct.addrinfo*, %struct.addrinfo** %48
%50 = icmp eq %struct.addrinfo* %49, null
br i1 %50, label %51, label %28
51: ; preds = %47, %19
%52 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%53 = call i32 @exit(i32 3)
unreachable
54: ; preds = %38
ret i32 %36
}