#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*/ sin;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int fcntl(int, int /*<<< orphan*/, ...);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int getconnect(char *servername, int port) {
  struct sockaddr_in sin;
  struct hostent *h2;
  int fd;
  if ((h2 = gethostbyname(servername)) == NULL)
    sin.sin_addr.s_addr = inet_addr(servername);
  else
    memcpy(&sin.sin_addr.s_addr, h2->h_addr, h2->h_length);
  sin.sin_family = AF_INET;
  sin.sin_port = htons(port);
  fd = socket(PF_INET, SOCK_STREAM, 0);
  if (fd < 0)
    return -1;
  if (connect(fd, (struct sockaddr *)&sin, sizeof(sin))) {
    close(fd);
    return -1;
  }
  fcntl(fd, F_SETFL, O_NONBLOCK | fcntl(fd, F_GETFL));
  return fd;
}


x86 assembly (clang, -O0):
<getconnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11b7 <getconnect+0x37>
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x18(%rbp)
jmp    11d1 <getconnect+0x51>
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1080 <memcpy@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    121a <getconnect+0x9a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <getconnect+0x112>
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
je     1249 <getconnect+0xc9>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <getconnect+0x112>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x38(%rbp),%edx
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%esi
or     %eax,%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getconnect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %rdi,%rbp
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     11a8 <getconnect+0x28>
lea    0x8(%rsp),%rdi
mov    (%rax),%edx
mov    0x4(%rax),%esi
call   1080 <memcpy@plt>
jmp    11b4 <getconnect+0x34>
mov    %rbp,%rdi
call   1060 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     1243 <getconnect+0xc3>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
je     120c <getconnect+0x8c>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1243 <getconnect+0xc3>
mov    0x2dbd(%rip),%rax
mov    (%rax),%r14d
mov    0x2dc3(%rip),%rax
mov    (%rax),%ebp
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
or     %eax,%ebp
mov    %ebx,%edi
mov    %r14d,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
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
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_GETFL = global i32 0
define i32 @getconnect(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
%8 = alloca i32
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 %17
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i32 @inet_addr(i8* %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i32 %14, i32* %16
br label %27
17: ; preds = %2
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
%20 = load %struct.hostent*, %struct.hostent** %7
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i32 0, i32 1
%22 = load i32, i32* %21
%23 = load %struct.hostent*, %struct.hostent** %7
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @memcpy(i32* %19, i32 %22, i32 %25)
br label %27
27: ; preds = %17, %12
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %28, i32* %29
%30 = load i32, i32* %5
%31 = call i32 @htons(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* @PF_INET
%34 = load i32, i32* @SOCK_STREAM
%35 = call i32 @socket(i32 %33, i32 %34, i32 0)
store i32 %35, i32* %8
%36 = load i32, i32* %8
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %27
store i32 -1, i32* %3
br label %57
39: ; preds = %27
%40 = load i32, i32* %8
%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 %47
44: ; preds = %39
%45 = load i32, i32* %8
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %3
br label %57
47: ; preds = %39
%48 = load i32, i32* %8
%49 = load i32, i32* @F_SETFL
%50 = load i32, i32* @O_NONBLOCK
%51 = load i32, i32* %8
%52 = load i32, i32* @F_GETFL
%53 = call i32 (i32, i32, ...) @fcntl(i32 %51, i32 %52)
%54 = or i32 %50, %53
%55 = call i32 (i32, i32, ...) @fcntl(i32 %48, i32 %49, i32 %54)
%56 = load i32, i32* %8
store i32 %56, i32* %3
br label %57
57: ; preds = %47, %44, %38
%58 = load i32, i32* %3
ret i32 %58
}

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
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_GETFL = global i32 0
define i32 @getconnect(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 %7, label %10
7: ; preds = %2
%8 = tail call i32 @inet_addr(i8* %0)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2, i32 0
store i32 %8, i32* %9
br label %17
10: ; preds = %2
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2, i32 0
%12 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 1
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @memcpy(i32* %11, i32 %13, i32 %15)
br label %17
17: ; preds = %10, %7
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %18, i32* %19
%20 = call i32 @htons(i32 %1)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* @PF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
%25 = icmp slt i32 %24, 0
br i1 %25, label %39, label %26
26: ; preds = %17
%27 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%28 = call i64 @connect(i32 %24, %struct.sockaddr* %27, i32 12)
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %26
%31 = call i32 @close(i32 %24)
br label %39
32: ; preds = %26
%33 = load i32, i32* @F_SETFL
%34 = load i32, i32* @O_NONBLOCK
%35 = load i32, i32* @F_GETFL
%36 = call i32 (i32, i32, ...) @fcntl(i32 %24, i32 %35)
%37 = or i32 %36, %34
%38 = call i32 (i32, i32, ...) @fcntl(i32 %24, i32 %33, i32 %37)
br label %39
39: ; preds = %17, %32, %30
%40 = phi i32 [ -1, %30 ], [ %24, %32 ], [ -1, %17 ]
ret i32 %40
}