#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);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int client_connect(int sockfd, char *server, int port) {
  struct sockaddr_in cliaddr;
  struct hostent *host;
  if (!(host = gethostbyname(server))) {
    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) {
    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   1040 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1175 <client_connect+0x35>
movl   $0x0,-0x4(%rbp)
jmp    11d4 <client_connect+0x94>
lea    -0x20(%rbp),%rdi
mov    $0x4,%esi
call   1050 <bzero@plt>
mov    0x2e5e(%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   1060 <connect@plt>
cmp    $0x0,%rax
jge    11cd <client_connect+0x8d>
movl   $0x0,-0x4(%rbp)
jmp    11d4 <client_connect+0x94>
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,%ebp
mov    %edi,%r14d
mov    %rsi,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     11ab <client_connect+0x6b>
mov    %rax,%rbx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    $0x4,%esi
call   1050 <bzero@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%rbx),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
shr    $0x3f,%rax
xor    $0x1,%eax
jmp    11ad <client_connect+0x6d>
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 }
@AF_INET = global i32 0
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 %14, label %13
13: ; preds = %3
store i32 0, i32* %4
br label %34
14: ; preds = %3
%15 = call i32 @bzero(%struct.sockaddr_in* %8, i32 4)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* %7
%19 = call i32 @htons(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%22 = load %struct.hostent*, %struct.hostent** %9
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %22, i32 0, i32 0
%24 = load i64, i64* %23
%25 = inttoptr i64 %24 to %struct.in_addr*
%26 = bitcast %struct.in_addr* %21 to i8*
%27 = bitcast %struct.in_addr* %25 to i8*
%28 = load i32, i32* %5
%29 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%30 = call i64 @connect(i32 %28, %struct.sockaddr* %29, i32 4)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %14
store i32 0, i32* %4
br label %34
33: ; preds = %14
store i32 1, i32* %4
br label %34
34: ; preds = %33, %32, %13
%35 = load i32, i32* %4
ret i32 %35
}

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 }
@AF_INET = global i32 0
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 %24, label %8
8: ; preds = %3
%9 = call i32 @bzero(%struct.sockaddr_in* %4, i32 4)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %10, i32* %11
%12 = tail call i32 @htons(i32 %2)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %6, i64 0, i32 0
%15 = load i64, i64* %14
%16 = inttoptr i64 %15 to i32*
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
%18 = load i32, i32* %16
store i32 %18, i32* %17
%19 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%20 = call i64 @connect(i32 %0, %struct.sockaddr* %19, i32 4)
%21 = lshr i64 %20, 63
%22 = trunc i64 %21 to i32
%23 = xor i32 %22, 1
br label %24
24: ; preds = %8, %3
%25 = phi i32 [ 0, %3 ], [ %23, %8 ]
ret i32 %25
}