#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ server_address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sockerror(char *);
int /*<<< orphan*/ stderr;
void client_connect(int sockfdes, char *ip, char *usr, int port) {
  struct sockaddr_in server_address;
  struct hostent *server = 0;
  server = gethostbyname(ip);
  if (server == NULL) {
    fprintf(stderr, "ERROR, invalid host: %s \n", ip);
    exit(0);
  }
  memset((char *)&server_address, 0, sizeof(server_address));
  server_address.sin_family = AF_INET;
  bcopy((char *)server->h_addr, (char *)&server_address.sin_addr.s_addr,
        server->h_length);
  server_address.sin_port = htons(port);
  if (connect(sockfdes, (struct sockaddr *)&server_address,
              sizeof(server_address)) < 0) {
    printf("HOST IP %s", ip);
    sockerror("ERROR connecting");
  } else {
    printf("Connection Initiated with %s\n", usr);
  }
}


x86 assembly (clang, -O0):
<client_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11e6 <client_connect+0x56>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe26(%rip),%rsi
call   1070 <fprintf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1080 <bcopy@plt>
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    1264 <client_connect+0xd4>
mov    -0x10(%rbp),%rsi
lea    0xdcc(%rip),%rdi
call   1040 <printf@plt>
lea    0xdcb(%rip),%rdi
call   10b0 <sockerror@plt>
jmp    1274 <client_connect+0xe4>
mov    -0x18(%rbp),%rsi
lea    0xdc7(%rip),%rdi
call   1040 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_connect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r12d
mov    %rsi,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     124a <client_connect+0xba>
mov    %rax,%rbp
lea    0x8(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rbp),%rsi
lea    0xc(%rsp),%rdi
mov    0x0(%rbp),%edx
call   1080 <memmove@plt>
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %rax,%rax
js     1220 <client_connect+0x90>
lea    0xe20(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
jmp    123b <client_connect+0xab>
lea    0xdf3(%rip),%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
lea    0xdef(%rip),%rdi
call   10b0 <sockerror@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
lea    0xda6(%rip),%rsi
mov    %r15,%rdx
call   1070 <fprintf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"ERROR, invalid host: %s \0A\00"
@AF_INET = global i32 0
@.str.1 = [11 x i8] c"HOST IP %s\00"
@.str.2 = [17 x i8] c"ERROR connecting\00"
@.str.3 = [30 x i8] c"Connection Initiated with %s\0A\00"
define void @client_connect(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.hostent*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store %struct.hostent* null, %struct.hostent** %10
%11 = load i8*, i8** %6
%12 = call %struct.hostent* @gethostbyname(i8* %11)
store %struct.hostent* %12, %struct.hostent** %10
%13 = load %struct.hostent*, %struct.hostent** %10
%14 = icmp eq %struct.hostent* %13, null
br i1 %14, label %15, label %20
15: ; preds = %4
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %6
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %4
%21 = bitcast %struct.sockaddr_in* %9 to i8*
%22 = call i32 @memset(i8* %21, i32 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %23, i32* %24
%25 = load %struct.hostent*, %struct.hostent** %10
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i32 0, i32 1
%27 = load i64, i64* %26
%28 = inttoptr i64 %27 to i8*
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
%31 = bitcast i32* %30 to i8*
%32 = load %struct.hostent*, %struct.hostent** %10
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @bcopy(i8* %28, i8* %31, i32 %34)
%36 = load i32, i32* %8
%37 = call i32 @htons(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %37, i32* %38
%39 = load i32, i32* %5
%40 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%41 = call i64 @connect(i32 %39, %struct.sockaddr* %40, i32 12)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %20
%44 = load i8*, i8** %6
%45 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %44)
%46 = call i32 @sockerror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %50
47: ; preds = %20
%48 = load i8*, i8** %7
%49 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %48)
br label %50
50: ; preds = %47, %43
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"ERROR, invalid host: %s \0A\00"
@AF_INET = global i32 0
@.str.1 = [11 x i8] c"HOST IP %s\00"
@.str.2 = [17 x i8] c"ERROR connecting\00"
@.str.3 = [30 x i8] c"Connection Initiated with %s\0A\00"
define void @client_connect(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = bitcast %struct.sockaddr_in* %5 to i8*
%7 = tail call %struct.hostent* @gethostbyname(i8* %1)
%8 = icmp eq %struct.hostent* %7, null
br i1 %8, label %9, label %13
9: ; preds = %4
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %1)
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %4
%14 = call i32 @memset(i8* %6, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 1
%18 = load i64, i64* %17
%19 = inttoptr i64 %18 to i8*
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
%21 = bitcast i32* %20 to i8*
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @htons(i32 %3)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %24, i32* %25
%26 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%27 = call i64 @connect(i32 %0, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %13
%30 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%31 = call i32 @sockerror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %34
32: ; preds = %13
%33 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %2)
br label %34
34: ; preds = %32, %29
ret void
}