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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, scalar_t__);
int socketconnect(const char *host, const char *port) {
  struct addrinfo hints;
  struct addrinfo *result, *rp;
  int sockd;
  int ret;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = 0;
  hints.ai_protocol = 0;
  int res = getaddrinfo(host, port, &hints, &result);
  if (res) {
    printf("Host not found\n");
    return -1;
  }
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    sockd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (sockd == -1)
      continue;
    if (connect(sockd, rp->ai_addr, rp->ai_addrlen) != -1) {
      ret = sockd;
      break;
    }
    close(sockd);
  }
  if (rp == NULL) {
    printf("Error connecting the socket\n");
    ret = -1;
    goto exit;
  }
exit:
  freeaddrinfo(result);
  return ret;
}


x86 assembly (clang, -O0):
<socketconnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
je     11f2 <socketconnect+0x82>
lea    0xe1f(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ab <socketconnect+0x13b>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1274 <socketconnect+0x104>
mov    -0x50(%rbp),%rax
mov    0x14(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%rdx
call   1090 <socket@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    1232 <socketconnect+0xc2>
jmp    1263 <socketconnect+0xf3>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
je     125b <socketconnect+0xeb>
mov    -0x54(%rbp),%eax
mov    %eax,-0x58(%rbp)
jmp    1274 <socketconnect+0x104>
mov    -0x54(%rbp),%edi
call   1050 <close@plt>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    11fa <socketconnect+0x8a>
cmpq   $0x0,-0x50(%rbp)
jne    1297 <socketconnect+0x127>
lea    0xd8a(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x58(%rbp)
jmp    129c <socketconnect+0x12c>
jmp    129c <socketconnect+0x12c>
mov    -0x48(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x58(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketconnect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x30(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     11ea <socketconnect+0x7a>
lea    0xe3e(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
jmp    1254 <socketconnect+0xe4>
mov    0x8(%rsp),%rbx
jmp    1204 <socketconnect+0x94>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1239 <socketconnect+0xc9>
mov    0x14(%rbx),%edi
mov    0x10(%rbx),%esi
mov    0x8(%rbx),%rdx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1200 <socketconnect+0x90>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    124a <socketconnect+0xda>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1200 <socketconnect+0x90>
lea    0xdc0(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    0x8(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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