#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 in_addr {
  int dummy;
};
struct sockaddr_in {
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ host_name;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(struct in_addr);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int cl_connect_to_host(const char *host_addr, int port) {
  int sock_fd = socket(PF_INET, SOCK_STREAM, 0);
  if (sock_fd < 0) {
    printf("  Error creating sock_fd.\n");
    return -1;
  }
  printf("  Engaging host %s... ", host_addr);
  struct hostent *host_info;
  host_info = gethostbyname(host_addr);
  if (host_info == NULL) {
    printf("Unkown host %s\n.", host_addr);
    return -1;
  }
  struct sockaddr_in host_name;
  host_name.sin_family = AF_INET;
  host_name.sin_port = htons(port);
  host_name.sin_addr = *(struct in_addr *)host_info->h_addr;
  printf("Done.\n");
  printf("  Connecting to %s on bind-port %hu, fifo %d... ",
         inet_ntoa(host_name.sin_addr), ntohs(host_name.sin_port), sock_fd);
  if (connect(sock_fd, (struct sockaddr *)&host_name, sizeof(host_name)) < 0) {
    printf("Error trying to connect.\n");
    return -1;
  }
  printf("Done.\n");
  return sock_fd;
}


x86 assembly (clang, -O0):
<cl_connect_to_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11bf <cl_connect_to_host+0x4f>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <cl_connect_to_host+0x13c>
mov    -0x10(%rbp),%rsi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1207 <cl_connect_to_host+0x97>
mov    -0x10(%rbp),%rsi
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <cl_connect_to_host+0x13c>
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
call   1070 <ntohs@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
mov    -0x18(%rbp),%ecx
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1298 <cl_connect_to_host+0x128>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <cl_connect_to_host+0x13c>
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cl_connect_to_host>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1263 <cl_connect_to_host+0xe3>
mov    %eax,%r15d
lea    0xe45(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbp,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     1271 <cl_connect_to_host+0xf1>
mov    %rax,%rbx
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
call   1050 <htons@plt>
mov    %eax,%ebp
mov    %eax,0xc(%rsp)
mov    (%rbx),%rax
mov    (%rax),%ebx
mov    %ebx,0x8(%rsp)
lea    0xe55(%rip),%r14
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1080 <ntohs@plt>
lea    0xe04(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
mov    %r15d,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
lea    0xe10(%rip),%rdi
cmovns %r14,%rdi
mov    $0xffffffff,%ebx
cmovns %r15d,%ebx
call   1030 <puts@plt>
jmp    1287 <cl_connect_to_host+0x107>
lea    0xe0e(%rip),%rdi
call   1030 <puts@plt>
jmp    1282 <cl_connect_to_host+0x102>
lea    0xd9f(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [27 x i8] c" Error creating sock_fd.\0A\00"
@.str.1 = [23 x i8] c" Engaging host %s... \00"
@.str.2 = [17 x i8] c"Unkown host %s\0A.\00"
@AF_INET = global i32 0
@.str.3 = [7 x i8] c"Done.\0A\00"
@.str.4 = [49 x i8] c" Connecting to %s on bind-port %hu, fifo %d... \00"
@.str.5 = [26 x i8] c"Error trying to connect.\0A\00"
define i32 @cl_connect_to_host(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.hostent*
%8 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %58
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = load i8*, i8** %4
%20 = call %struct.hostent* @gethostbyname(i8* %19)
store %struct.hostent* %20, %struct.hostent** %7
%21 = load %struct.hostent*, %struct.hostent** %7
%22 = icmp eq %struct.hostent* %21, null
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i8*, i8** %4
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %24)
store i32 -1, i32* %3
br label %58
26: ; preds = %16
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %27, i32* %28
%29 = load i32, i32* %5
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %30, i32* %31
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%33 = load %struct.hostent*, %struct.hostent** %7
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i32 0, i32 0
%35 = load i64, i64* %34
%36 = inttoptr i64 %35 to %struct.in_addr*
%37 = bitcast %struct.in_addr* %32 to i8*
%38 = bitcast %struct.in_addr* %36 to i8*
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%41 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i8* @inet_ntoa(i32 %42)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%45 = load i32, i32* %44
%46 = call i32 @ntohs(i32 %45)
%47 = load i32, i32* %6
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i8* %43, i32 %46, i32 %47)
%49 = load i32, i32* %6
%50 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%51 = call i64 @connect(i32 %49, %struct.sockaddr* %50, i32 12)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %26
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %58
55: ; preds = %26
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%57 = load i32, i32* %6
store i32 %57, i32* %3
br label %58
58: ; preds = %55, %53, %23, %14
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [23 x i8] c" Engaging host %s... \00"
@.str.2 = [17 x i8] c"Unkown host %s\0A.\00"
@AF_INET = global i32 0
@.str.4 = [49 x i8] c" Connecting to %s on bind-port %hu, fifo %d... \00"
@str.6 = [6 x i8] c"Done.\00"
@str.7 = [25 x i8] c"Error trying to connect.\00"
@str.8 = [26 x i8] c" Error creating sock_fd.\00"
define i32 @cl_connect_to_host(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.8, i64 0, i64 0))
br label %37
10: ; preds = %2
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%12 = tail call %struct.hostent* @gethostbyname(i8* %0)
%13 = icmp eq %struct.hostent* %12, null
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %37
16: ; preds = %10
%17 = bitcast %struct.sockaddr_in* %3 to i8*
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %18, i32* %19
%20 = tail call i32 @htons(i32 %1)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 0
%23 = load i64, i64* %22
%24 = inttoptr i64 %23 to i32*
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%26 = load i32, i32* %24
store i32 %26, i32* %25
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.6, i64 0, i64 0))
%28 = tail call i8* @inet_ntoa(i32 %26)
%29 = tail call i32 @ntohs(i32 %20)
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i8* %28, i32 %29, i32 %6)
%31 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%32 = call i64 @connect(i32 %6, %struct.sockaddr* %31, i32 12)
%33 = icmp slt i64 %32, 0
%34 = select i1 %33, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @str.7, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @str.6, i64 0, i64 0)
%35 = select i1 %33, i32 -1, i32 %6
%36 = call i32 @puts(i8* dereferenceable(1) %34)
br label %37
37: ; preds = %14, %16, %8
%38 = phi i32 [ -1, %8 ], [ -1, %14 ], [ %35, %16 ]
ret i32 %38
}