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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, char *);
char *strerror(int /*<<< orphan*/);
int client_connect(int sockfd, char *server, int port) {
  struct sockaddr_in cliaddr;
  struct hostent *host;
  if (!(host = gethostbyname(server))) {
    printf("gethostbyname(%s) error:%s\n", server, strerror(errno));
    return (0);
  }
  bzero(&cliaddr, sizeof(struct sockaddr));
  cliaddr.sin_family = AF_INET;
  cliaddr.sin_port = htons(port);
  cliaddr.sin_addr = *((struct in_addr *)host->h_addr);
  if (connect(sockfd, (struct sockaddr *)&cliaddr, sizeof(struct sockaddr)) <
      0) {
    perror("[-] error");
    return (0);
  }
  return (1);
}


x86 assembly (clang, -O0):
<client_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11ce <client_connect+0x5e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
lea    0xe43(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1239 <client_connect+0xc9>
lea    -0x20(%rbp),%rdi
mov    $0x4,%esi
call   1070 <bzero@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1232 <client_connect+0xc2>
lea    0xdfb(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1239 <client_connect+0xc9>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %edi,%r14d
mov    %rsi,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     11f2 <client_connect+0x82>
mov    %rax,%rbx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x4,%esi
call   1070 <bzero@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %r15d,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%rbx),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jns    1214 <client_connect+0xa4>
lea    0xe31(%rip),%rdi
call   1060 <perror@plt>
jmp    1212 <client_connect+0xa2>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdf9(%rip),%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [28 x i8] c"gethostbyname(%s) error:%s\0A\00"
@errno = global i32 0
@AF_INET = global i32 0
@.str.1 = [10 x i8] c"[-] error\00"
define i32 @client_connect(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.hostent*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %6
%11 = call %struct.hostent* @gethostbyname(i8* %10)
store %struct.hostent* %11, %struct.hostent** %9
%12 = icmp ne %struct.hostent* %11, null
br i1 %12, label %18, label %13
13: ; preds = %3
%14 = load i8*, i8** %6
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %16)
store i32 0, i32* %4
br label %39
18: ; preds = %3
%19 = call i32 @bzero(%struct.sockaddr_in* %8, i32 4)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* %7
%23 = call i32 @htons(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%26 = load %struct.hostent*, %struct.hostent** %9
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 0
%28 = load i64, i64* %27
%29 = inttoptr i64 %28 to %struct.in_addr*
%30 = bitcast %struct.in_addr* %25 to i8*
%31 = bitcast %struct.in_addr* %29 to i8*
%32 = load i32, i32* %5
%33 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%34 = call i64 @connect(i32 %32, %struct.sockaddr* %33, i32 4)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %18
%37 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %39
38: ; preds = %18
store i32 1, i32* %4
br label %39
39: ; preds = %38, %36, %13
%40 = load i32, i32* %4
ret i32 %40
}

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 }
@.str = [28 x i8] c"gethostbyname(%s) error:%s\0A\00"
@errno = global i32 0
@AF_INET = global i32 0
@.str.1 = [10 x i8] c"[-] error\00"
define i32 @client_connect(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = tail call %struct.hostent* @gethostbyname(i8* %1)
%7 = icmp eq %struct.hostent* %6, null
br i1 %7, label %8, label %12
8: ; preds = %3
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %10)
br label %28
12: ; preds = %3
%13 = call i32 @bzero(%struct.sockaddr_in* %4, i32 4)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 %2)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %6, i64 0, i32 0
%19 = load i64, i64* %18
%20 = inttoptr i64 %19 to i32*
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
%22 = load i32, i32* %20
store i32 %22, i32* %21
%23 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%24 = call i64 @connect(i32 %0, %struct.sockaddr* %23, i32 4)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %12
%27 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %12, %26, %8
%29 = phi i32 [ 0, %26 ], [ 0, %8 ], [ 1, %12 ]
ret i32 %29
}