#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr const *, int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_host(const char *host, int port) {
  int sd;
  struct hostent *hostname;
  struct sockaddr_in addr;
  if ((sd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
    return 0;
  if (!(hostname = gethostbyname(host)))
    return 0;
  memset(&addr, 0, sizeof(struct sockaddr_in));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  memcpy(&addr.sin_addr.s_addr, hostname->h_addr, hostname->h_length);
  if (connect(sd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_in)) ==
      -1)
    return 0;
  return sd;
}


x86 assembly (clang, -O0):
<connect_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11a0 <connect_host+0x40>
movl   $0x0,-0x4(%rbp)
jmp    122c <connect_host+0xcc>
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11c3 <connect_host+0x63>
movl   $0x0,-0x4(%rbp)
jmp    122c <connect_host+0xcc>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    1226 <connect_host+0xc6>
movl   $0x0,-0x4(%rbp)
jmp    122c <connect_host+0xcc>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_host>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11f9 <connect_host+0x99>
mov    %eax,%ebp
mov    %r15,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     11f9 <connect_host+0x99>
mov    %rax,%r15
mov    %rsp,%rbx
xor    %r12d,%r12d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r15),%edx
mov    0x4(%r15),%esi
mov    %rbx,%rdi
call   1060 <memcpy@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
cmove  %r12d,%ebp
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
define i32 @connect_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 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 0, i32* %3
br label %42
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call %struct.hostent* @gethostbyname(i8* %15)
store %struct.hostent* %16, %struct.hostent** %7
%17 = icmp ne %struct.hostent* %16, null
br i1 %17, label %19, label %18
18: ; preds = %14
store i32 0, i32* %3
br label %42
19: ; preds = %14
%20 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* %5
%24 = call i32 @htons(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
%28 = load %struct.hostent*, %struct.hostent** %7
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = load %struct.hostent*, %struct.hostent** %7
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @memcpy(i32* %27, i32 %30, i32 %33)
%35 = load i32, i32* %6
%36 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%37 = call i32 @connect(i32 %35, %struct.sockaddr* %36, i32 12)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %40
39: ; preds = %19
store i32 0, i32* %3
br label %42
40: ; preds = %19
%41 = load i32, i32* %6
store i32 %41, i32* %3
br label %42
42: ; preds = %40, %39, %18, %13
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
define i32 @connect_host(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %28, label %9
9: ; preds = %2
%10 = tail call %struct.hostent* @gethostbyname(i8* %0)
%11 = icmp eq %struct.hostent* %10, null
br i1 %11, label %28, label %12
12: ; preds = %9
%13 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %14, i32* %15
%16 = call i32 @htons(i32 %1)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 1
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @memcpy(i32* %18, i32 %20, i32 %22)
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i32 @connect(i32 %7, %struct.sockaddr* %24, i32 12)
%26 = icmp eq i32 %25, -1
%27 = select i1 %26, i32 0, i32 %7
br label %28
28: ; preds = %12, %9, %2
%29 = phi i32 [ 0, %2 ], [ 0, %9 ], [ %27, %12 ]
ret i32 %29
}