#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;
};

/* Variables and functions */
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char const *, char *, int /*<<< orphan*/ *,
                       struct addrinfo **);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int init_connection(const char *name) {
  struct addrinfo *addr;
  int fd = -1;
  if (getaddrinfo(name, "80", NULL, &addr) < 0)
    return -1;
  if (addr == NULL)
    return -2;
  fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
  if (fd < 0)
    return -3;
  if (connect(fd, addr->ai_addr, addr->ai_addrlen) < 0)
    return -4;
  freeaddrinfo(addr);
  return fd;
}


x86 assembly (clang, -O0):
<init_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x18(%rbp),%rcx
call   1040 <getaddrinfo@plt>
cmp    $0x0,%rax
jge    1181 <init_connection+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1205 <init_connection+0xc5>
cmpq   $0x0,-0x18(%rbp)
jne    1198 <init_connection+0x58>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1205 <init_connection+0xc5>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x18(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11cb <init_connection+0x8b>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1205 <init_connection+0xc5>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1030 <connect@plt>
cmp    $0x0,%rax
jge    11f6 <init_connection+0xb6>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1205 <init_connection+0xc5>
mov    -0x18(%rbp),%rdi
call   1050 <freeaddrinfo@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_connection>:
push   %rbx
sub    $0x10,%rsp
lea    0xeb4(%rip),%rsi
lea    0x8(%rsp),%rcx
xor    %edx,%edx
call   1040 <getaddrinfo@plt>
test   %rax,%rax
js     119d <init_connection+0x5d>
mov    0x8(%rsp),%rax
test   %rax,%rax
je     11a4 <init_connection+0x64>
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   1060 <socket@plt>
test   %eax,%eax
js     11ab <init_connection+0x6b>
mov    %eax,%ebx
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1030 <connect@plt>
test   %rax,%rax
js     11b2 <init_connection+0x72>
mov    0x8(%rsp),%rdi
call   1050 <freeaddrinfo@plt>
jmp    11b7 <init_connection+0x77>
mov    $0xffffffff,%ebx
jmp    11b7 <init_connection+0x77>
mov    $0xfffffffe,%ebx
jmp    11b7 <init_connection+0x77>
mov    $0xfffffffd,%ebx
jmp    11b7 <init_connection+0x77>
mov    $0xfffffffc,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@.str = [3 x i8] c"80\00"
define i32 @init_connection(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.addrinfo*
%5 = alloca i32
store i8* %0, i8** %3
store i32 -1, i32* %5
%6 = load i8*, i8** %3
%7 = call i64 @getaddrinfo(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* null, %struct.addrinfo** %4)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 -1, i32* %2
br label %43
10: ; preds = %1
%11 = load %struct.addrinfo*, %struct.addrinfo** %4
%12 = icmp eq %struct.addrinfo* %11, null
br i1 %12, label %13, label %14
13: ; preds = %10
store i32 -2, i32* %2
br label %43
14: ; preds = %10
%15 = load %struct.addrinfo*, %struct.addrinfo** %4
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i32 0, i32 4
%17 = load i32, i32* %16
%18 = load %struct.addrinfo*, %struct.addrinfo** %4
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i32 0, i32 3
%20 = load i32, i32* %19
%21 = load %struct.addrinfo*, %struct.addrinfo** %4
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i32 0, i32 2
%23 = load i32, i32* %22
%24 = call i32 @socket(i32 %17, i32 %20, i32 %23)
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %14
store i32 -3, i32* %2
br label %43
28: ; preds = %14
%29 = load i32, i32* %5
%30 = load %struct.addrinfo*, %struct.addrinfo** %4
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i32 0, i32 1
%32 = load i32, i32* %31
%33 = load %struct.addrinfo*, %struct.addrinfo** %4
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i64 @connect(i32 %29, i32 %32, i32 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %28
store i32 -4, i32* %2
br label %43
39: ; preds = %28
%40 = load %struct.addrinfo*, %struct.addrinfo** %4
%41 = call i32 @freeaddrinfo(%struct.addrinfo* %40)
%42 = load i32, i32* %5
store i32 %42, i32* %2
br label %43
43: ; preds = %39, %38, %27, %13, %9
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@.str = [3 x i8] c"80\00"
define i32 @init_connection(i8* %0) {
%2 = alloca %struct.addrinfo*
%3 = bitcast %struct.addrinfo** %2 to i8*
%4 = call i64 @getaddrinfo(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* null, %struct.addrinfo** %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %29, label %6
6: ; preds = %1
%7 = load %struct.addrinfo*, %struct.addrinfo** %2
%8 = icmp eq %struct.addrinfo* %7, null
br i1 %8, label %29, label %9
9: ; preds = %6
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 4
%11 = load i32, i32* %10
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 3
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 2
%15 = load i32, i32* %14
%16 = call i32 @socket(i32 %11, i32 %13, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %29, label %18
18: ; preds = %9
%19 = load %struct.addrinfo*, %struct.addrinfo** %2
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 1
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i64 @connect(i32 %16, i32 %21, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %18
%27 = load %struct.addrinfo*, %struct.addrinfo** %2
%28 = call i32 @freeaddrinfo(%struct.addrinfo* %27)
br label %29
29: ; preds = %18, %9, %6, %1, %26
%30 = phi i32 [ %16, %26 ], [ -1, %1 ], [ -2, %6 ], [ -3, %9 ], [ -4, %18 ]
ret i32 %30
}