#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 {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
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*/ inet_addr(char *);
int /*<<< orphan*/ memcpy(TYPE_1__ *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sock_connect(char *hostname, int port) {
  struct sockaddr_in addr;
  int fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0)
    return -1;
  struct hostent *hostent = gethostbyname(hostname);
  if (hostent)
    memcpy(&addr.sin_addr, hostent->h_addr, hostent->h_length);
  else
    addr.sin_addr.s_addr = inet_addr(hostname);
  ;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)))
    return -1;
  ;
  return fd;
}


x86 assembly (clang, -O0):
<sock_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11a1 <sock_connect+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1228 <sock_connect+0xc8>
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11d8 <sock_connect+0x78>
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
jmp    11e4 <sock_connect+0x84>
mov    -0x10(%rbp),%rdi
call   1040 <inet_addr@plt>
mov    %eax,-0x18(%rbp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
je     1222 <sock_connect+0xc2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1228 <sock_connect+0xc8>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e71(%rip),%r15
mov    (%r15),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11ae <sock_connect+0x4e>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     11b5 <sock_connect+0x55>
lea    0x10(%rsp),%rdi
mov    (%rax),%edx
mov    0x4(%rax),%esi
call   1060 <memcpy@plt>
jmp    11c1 <sock_connect+0x61>
mov    $0xffffffff,%eax
jmp    11ec <sock_connect+0x8c>
mov    %rbp,%rdi
call   1040 <inet_addr@plt>
mov    %eax,0x10(%rsp)
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <connect@plt>
neg    %rax
sbb    %eax,%eax
or     %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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