#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 /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
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(host, port, &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);
  freeaddrinfo(res);
  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    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1060 <getaddrinfo@plt>
cmp    $0xffffffff,%eax
jne    11c2 <open_socket+0x62>
lea    0xe43(%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   1080 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11f5 <open_socket+0x95>
lea    0xe2a(%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)
mov    -0x18(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
cmpl   $0xffffffff,-0x38(%rbp)
jne    122c <open_socket+0xcc>
lea    0xe05(%rip),%rdi
call   1040 <error@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <getaddrinfo@plt>
cmp    $0xffffffff,%eax
jne    11c2 <open_socket+0x62>
lea    0xe43(%rip),%rdi
call   1040 <error@plt>
mov    0x8(%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   1080 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11e8 <open_socket+0x88>
lea    0xe37(%rip),%rdi
call   1040 <error@plt>
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1050 <connect@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%rdi
call   1070 <freeaddrinfo@plt>
cmp    $0xffffffff,%ebp
jne    1216 <open_socket+0xb6>
lea    0xe1b(%rip),%rdi
call   1040 <error@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [26 x i8] c"Can't resolve the address\00"
@.str.1 = [18 x i8] c"Can't open socket\00"
@.str.2 = [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 = load i8*, i8** %3
%15 = load i8*, i8** %4
%16 = call i32 @getaddrinfo(i8* %14, i8* %15, %struct.addrinfo* %6, %struct.addrinfo** %5)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %2
%21 = load %struct.addrinfo*, %struct.addrinfo** %5
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i32 0, i32 4
%23 = load i32, i32* %22
%24 = load %struct.addrinfo*, %struct.addrinfo** %5
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i32 0, i32 3
%26 = load i32, i32* %25
%27 = load %struct.addrinfo*, %struct.addrinfo** %5
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i32 0, i32 2
%29 = load i32, i32* %28
%30 = call i32 @socket(i32 %23, i32 %26, i32 %29)
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %20
%34 = call i32 @error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %20
%36 = load i32, i32* %7
%37 = load %struct.addrinfo*, %struct.addrinfo** %5
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 1
%39 = load i32, i32* %38
%40 = load %struct.addrinfo*, %struct.addrinfo** %5
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @connect(i32 %36, i32 %39, i32 %42)
store i32 %43, i32* %8
%44 = load %struct.addrinfo*, %struct.addrinfo** %5
%45 = call i32 @freeaddrinfo(%struct.addrinfo* %44)
%46 = load i32, i32* %8
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %35
%49 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %50
50: ; preds = %48, %35
%51 = load i32, i32* %7
ret i32 %51
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [26 x i8] c"Can't resolve the address\00"
@.str.1 = [18 x i8] c"Can't open socket\00"
@.str.2 = [24 x i8] c"Can't connect to socket\00"
define i32 @open_socket(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 = 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* %0, i8* %1, %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, 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.1, 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 = load %struct.addrinfo*, %struct.addrinfo** %3
%36 = call i32 @freeaddrinfo(%struct.addrinfo* %35)
%37 = icmp eq i32 %34, -1
br i1 %37, label %38, label %40
38: ; preds = %28
%39 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %28
ret i32 %24
}