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

/* Variables and functions */
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, 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, int /*<<< orphan*/);
int socket_network_client(const char *host, int port, int type) {
  struct hostent *hp;
  struct sockaddr_in addr;
  socklen_t alen;
  int s;
  hp = gethostbyname(host);
  if (hp == 0)
    return -1;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = hp->h_addrtype;
  addr.sin_port = htons(port);
  memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
  s = socket(hp->h_addrtype, type, 0);
  if (s < 0)
    return -1;
  if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<socket_network_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a6 <socket_network_client+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    123f <socket_network_client+0xcf>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    -0x20(%rbp),%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    0x8(%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    120a <socket_network_client+0x9a>
movl   $0xffffffff,-0x4(%rbp)
jmp    123f <socket_network_client+0xcf>
mov    -0x38(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1239 <socket_network_client+0xc9>
mov    -0x38(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123f <socket_network_client+0xcf>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_network_client>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r15d
mov    %esi,%ebp
call   1060 <gethostbyname@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11f7 <socket_network_client+0x87>
mov    %rax,%rbx
mov    %rsp,%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x4(%rbx),%edx
mov    0x8(%rbx),%esi
mov    %r12,%rdi
call   1070 <memcpy@plt>
mov    (%rbx),%edi
mov    %r15d,%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     11f7 <socket_network_client+0x87>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
js     11f0 <socket_network_client+0x80>
mov    %ebx,%r14d
jmp    11f7 <socket_network_client+0x87>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32, i32, i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
define i32 @socket_network_client(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.hostent*
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = load i8*, i8** %5
%13 = call %struct.hostent* @gethostbyname(i8* %12)
store %struct.hostent* %13, %struct.hostent** %8
%14 = load %struct.hostent*, %struct.hostent** %8
%15 = icmp eq %struct.hostent* %14, null
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -1, i32* %4
br label %52
17: ; preds = %3
%18 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%19 = load %struct.hostent*, %struct.hostent** %8
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* %6
%24 = call i32 @htons(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%27 = load %struct.hostent*, %struct.hostent** %8
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i32 0, i32 2
%29 = load i32, i32* %28
%30 = load %struct.hostent*, %struct.hostent** %8
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i32 0, i32 1
%32 = load i32, i32* %31
%33 = call i32 @memcpy(i32* %26, i32 %29, i32 %32)
%34 = load %struct.hostent*, %struct.hostent** %8
%35 = getelementptr inbounds %struct.hostent, %struct.hostent* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = load i32, i32* %7
%38 = call i32 @socket(i32 %36, i32 %37, i32 0)
store i32 %38, i32* %11
%39 = load i32, i32* %11
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %17
store i32 -1, i32* %4
br label %52
42: ; preds = %17
%43 = load i32, i32* %11
%44 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%45 = call i64 @connect(i32 %43, %struct.sockaddr* %44, i32 12)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = load i32, i32* %11
%49 = call i32 @close(i32 %48)
store i32 -1, i32* %4
br label %52
50: ; preds = %42
%51 = load i32, i32* %11
store i32 %51, i32* %4
br label %52
52: ; preds = %50, %47, %41, %16
%53 = load i32, i32* %4
ret i32 %53
}

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