#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_family;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_addrtype;
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ socketInfo;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int connect(int, struct sockaddr *, int);
scalar_t__ gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int conecta(char *server, int port) {
  struct hostent *hp;
  struct sockaddr_in socketInfo;
  int socketfd;
  int result;
  socketfd = socket(AF_INET, SOCK_STREAM, 0);
  hp = (struct hostent *)gethostbyname((const char *)server);
  if (!hp) {
    return -1;
  }
  socketInfo.sin_port = htons(port);
  bcopy((char *)hp->h_addr, (char *)&socketInfo.sin_addr, hp->h_length);
  socketInfo.sin_family = hp->h_addrtype;
  result =
      connect(socketfd, (struct sockaddr *)&socketInfo, sizeof(socketInfo));
  if (result == -1) {
    return -1;
  }
  return socketfd;
}


x86 assembly (clang, -O0):
<conecta>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119f <conecta+0x4f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <conecta+0xaf>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x30(%rbp),%rsi
add    $0x4,%rsi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edx
call   1050 <bcopy@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1060 <connect@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    11f9 <conecta+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <conecta+0xaf>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<conecta>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     11c3 <conecta+0x73>
mov    %rax,%rbp
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x8(%rbp),%rsi
lea    0x4(%rsp),%rdi
mov    0x4(%rbp),%edx
call   1050 <memmove@plt>
mov    0x0(%rbp),%eax
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
jmp    11c8 <conecta+0x78>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @conecta(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 = tail call i64 @gethostbyname(i8* %0)
%9 = inttoptr i64 %8 to %struct.hostent*
%10 = icmp eq i64 %8, 0
br i1 %10, label %28, label %11
11: ; preds = %2
%12 = tail call i32 @htons(i32 %1)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i64 0, i32 2
%15 = load i64, i64* %14
%16 = inttoptr i64 %15 to i8*
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%18 = bitcast i32* %17 to i8*
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i64 0, i32 1
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i64 0, i32 0
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %22, i32* %23
%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 -1, i32 %7
br label %28
28: ; preds = %11, %2
%29 = phi i32 [ -1, %2 ], [ %27, %11 ]
ret i32 %29
}