#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 */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int ht_open(const char *host, const char *port) {
  if (port == 0)
    port = "80";
  struct addrinfo hints, *addr;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  int t = getaddrinfo(host, port, &hints, &addr);
  if (t != 0) {
    fprintf(stderr, "getaddrinfo fault\n");
    return 0;
  }
  int s = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
  if (s == -1) {
    fprintf(stderr, "open socket error\n");
    freeaddrinfo(addr);
    return 1;
  }
  t = connect(s, addr->ai_addr, addr->ai_addrlen);
  if (t == -1) {
    fprintf(stderr, "connect fault\n");
    freeaddrinfo(addr);
    close(s);
    return 0;
  }
  freeaddrinfo(addr);
  return s;
}


x86 assembly (clang, -O0):
<ht_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1196 <ht_open+0x26>
lea    0xe6e(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     1201 <ht_open+0x91>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <ht_open+0x145>
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x38(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    1252 <ht_open+0xe2>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    0xdde(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b5 <ht_open+0x145>
mov    -0x40(%rbp),%edi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1060 <connect@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    12a6 <ht_open+0x136>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x40(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <ht_open+0x145>
mov    -0x38(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ht_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbp
mov    %rdi,%rbx
test   %rsi,%rsi
lea    0xe76(%rip),%rax
cmove  %rax,%rbp
lea    0x10(%rsp),%r15
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     11f9 <ht_open+0x89>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
call   1050 <fprintf@plt>
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1238 <ht_open+0xc8>
mov    %eax,%ebp
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebp,%edi
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
je     125f <ht_open+0xef>
mov    0x8(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebp,%r14d
jmp    11eb <ht_open+0x7b>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    0xdce(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x8(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    $0x1,%r14d
jmp    11eb <ht_open+0x7b>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x8(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11eb <ht_open+0x7b>

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

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