#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_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ servadd;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, struct sockaddr *,
                         int /*<<< orphan*/);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to_server(char *host, int portnum) {
  int sock;
  struct sockaddr_in servadd;
  struct hostent *hp;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock == -1)
    return -1;
  bzero(&servadd, sizeof(servadd));
  hp = gethostbyname(host);
  if (hp == NULL)
    return -1;
  bcopy(hp->h_addr, (struct sockaddr *)&servadd.sin_addr, hp->h_length);
  servadd.sin_port = htons(portnum);
  servadd.sin_family = AF_INET;
  if (connect(sock, (struct sockaddr *)&servadd, sizeof(servadd)) != 0)
    return -1;
  return sock;
}


x86 assembly (clang, -O0):
<connect_to_server>:
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,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11a1 <connect_to_server+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <connect_to_server+0xd1>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d3 <connect_to_server+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <connect_to_server+0xd1>
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%edi
lea    -0x28(%rbp),%rsi
add    $0x8,%rsi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1050 <bcopy@plt>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
je     122b <connect_to_server+0xcb>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <connect_to_server+0xd1>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e6f(%rip),%r12
mov    (%r12),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11eb <connect_to_server+0x8b>
mov    %eax,%ebx
mov    %rsp,%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    %r15,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     11eb <connect_to_server+0x8b>
mov    (%rax),%edx
mov    0x4(%rax),%edi
lea    0x8(%rsp),%rsi
call   1050 <bcopy@plt>
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r12),%eax
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <connect@plt>
neg    %rax
sbb    %ebp,%ebp
or     %ebx,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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