#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;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ PF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ error(char *);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_socket(char *host, char *port) {
  struct addrinfo *res;
  struct addrinfo hints;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = PF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if (getaddrinfo("www.infoq.com", "80", &hints, &res) == -1)
    error("Can't resolve the address");
  int d_sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  if (d_sock == -1)
    error("Can't open socket");
  int c = connect(d_sock, res->ai_addr, res->ai_addrlen);
  if (c == -1)
    error("Can't connect to socket");
  return d_sock;
}


x86 assembly (clang, -O0):
<open_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    0xe71(%rip),%rdi
lea    0xe78(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1060 <getaddrinfo@plt>
cmp    $0xffffffff,%eax
jne    11b8 <open_socket+0x68>
lea    0xe5e(%rip),%rdi
call   1040 <error@plt>
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   1070 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11eb <open_socket+0x9b>
lea    0xe45(%rip),%rdi
call   1040 <error@plt>
mov    -0x34(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1050 <connect@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    1219 <open_socket+0xc9>
lea    0xe29(%rip),%rdi
call   1040 <error@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_socket>:
push   %rbx
sub    $0x20,%rsp
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0xe76(%rip),%rdi
lea    0xe7d(%rip),%rsi
mov    %rsp,%rcx
mov    %rbx,%rdx
call   1060 <getaddrinfo@plt>
cmp    $0xffffffff,%eax
jne    11ad <open_socket+0x5d>
lea    0xe69(%rip),%rdi
call   1040 <error@plt>
mov    (%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   1070 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11d2 <open_socket+0x82>
lea    0xe5e(%rip),%rdi
call   1040 <error@plt>
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    11f3 <open_socket+0xa3>
lea    0xe4f(%rip),%rdi
call   1040 <error@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [14 x i8] c"www.infoq.com\00"
@.str.1 = [3 x i8] c"80\00"
@.str.2 = [26 x i8] c"Can't resolve the address\00"
@.str.3 = [18 x i8] c"Can't open socket\00"
@.str.4 = [24 x i8] c"Can't connect to socket\00"
define i32 @open_socket(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.addrinfo*
%6 = alloca %struct.addrinfo
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 20)
%10 = load i32, i32* @PF_UNSPEC
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %12, i32* %13
%14 = call i32 @getaddrinfo(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %6, %struct.addrinfo** %5)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = load %struct.addrinfo*, %struct.addrinfo** %5
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i32 0, i32 4
%21 = load i32, i32* %20
%22 = load %struct.addrinfo*, %struct.addrinfo** %5
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i32 0, i32 3
%24 = load i32, i32* %23
%25 = load %struct.addrinfo*, %struct.addrinfo** %5
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i32 0, i32 2
%27 = load i32, i32* %26
%28 = call i32 @socket(i32 %21, i32 %24, i32 %27)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %18
%32 = call i32 @error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %31, %18
%34 = load i32, i32* %7
%35 = load %struct.addrinfo*, %struct.addrinfo** %5
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 1
%37 = load i32, i32* %36
%38 = load %struct.addrinfo*, %struct.addrinfo** %5
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @connect(i32 %34, i32 %37, i32 %40)
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %33
%45 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %46
46: ; preds = %44, %33
%47 = load i32, i32* %7
ret i32 %47
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [14 x i8] c"www.infoq.com\00"
@.str.1 = [3 x i8] c"80\00"
@.str.2 = [26 x i8] c"Can't resolve the address\00"
@.str.3 = [18 x i8] c"Can't open socket\00"
@.str.4 = [24 x i8] c"Can't connect to socket\00"
define i32 @open_socket(i8* nocapture readnone %0, i8* nocapture readnone %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* %4, i32 0, i32 20)
%8 = load i32, i32* @PF_UNSPEC
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %10, i32* %11
%12 = call i32 @getaddrinfo(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %4, %struct.addrinfo** %3)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = load %struct.addrinfo*, %struct.addrinfo** %3
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i64 0, i32 4
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i64 0, i32 3
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i64 0, i32 2
%23 = load i32, i32* %22
%24 = call i32 @socket(i32 %19, i32 %21, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %16
%27 = call i32 @error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %16
%29 = load %struct.addrinfo*, %struct.addrinfo** %3
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i64 0, i32 1
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i64 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @connect(i32 %24, i32 %31, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %28
%37 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %38
38: ; preds = %36, %28
ret i32 %24
}