#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;
  int /*<<< orphan*/ h_addrtype;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
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 coap_net_tcp_connect(char *hostname, int port) {
  int netd;
  struct hostent *hp;
  struct sockaddr_in addr;
  if ((hp = gethostbyname(hostname)) == NULL) {
    return -1;
  }
  if ((netd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    return -1;
  }
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = hp->h_addrtype;
  addr.sin_port = htons(port);
  memcpy(&addr.sin_addr.s_addr, hp->h_addr, hp->h_length);
  if (connect(netd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    return -1;
  }
  return netd;
}


x86 assembly (clang, -O0):
<coap_net_tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1192 <coap_net_tcp_connect+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <coap_net_tcp_connect+0xcb>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11c3 <coap_net_tcp_connect+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <coap_net_tcp_connect+0xcb>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    -0x20(%rbp),%rax
mov    0x8(%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    $0x0,%rax
jge    1225 <coap_net_tcp_connect+0xc5>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <coap_net_tcp_connect+0xcb>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<coap_net_tcp_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
call   1050 <gethostbyname@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     11f0 <coap_net_tcp_connect+0x90>
mov    %rax,%rbp
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11f0 <coap_net_tcp_connect+0x90>
mov    %eax,%r15d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x8(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
mov    %rbx,%rdi
call   1060 <memcpy@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
test   %rax,%rax
mov    $0xffffffff,%ebx
cmovns %r15d,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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