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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int connect_to_server(int server_socket, char *ip, int port) {
  struct sockaddr_in server_addr;
  struct hostent *hent;
  if ((hent = gethostbyname(ip)) == NULL) {
    perror("gethostbyname() failed!\n");
    return -1;
  }
  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(port);
  memcpy(&server_addr.sin_addr.s_addr, hent->h_addr,
         sizeof(server_addr.sin_addr.s_addr));
  if (connect(server_socket, (struct sockaddr *)&server_addr,
              sizeof(struct sockaddr)) < 0) {
    perror("connect() failed!\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<connect_to_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11a1 <connect_to_server+0x41>
lea    0xe70(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <connect_to_server+0xb6>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    120f <connect_to_server+0xaf>
lea    0xe1b(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <connect_to_server+0xb6>
movl   $0x0,-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    %edx,%ebx
mov    %edi,%r15d
mov    %rsi,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     11e3 <connect_to_server+0x83>
mov    %rax,%rbp
mov    %rsp,%r12
xor    %r14d,%r14d
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x0(%rbp),%esi
mov    %r12,%rdi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
test   %rax,%rax
js     11ec <connect_to_server+0x8c>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe16(%rip),%rdi
jmp    11f3 <connect_to_server+0x93>
lea    0xe26(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11d3 <connect_to_server+0x73>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [25 x i8] c"gethostbyname() failed!\0A\00"
@AF_INET = global i32 0
@.str.1 = [19 x i8] c"connect() failed!\0A\00"
define i32 @connect_to_server(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.hostent*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %6
%11 = call %struct.hostent* @gethostbyname(i8* %10)
store %struct.hostent* %11, %struct.hostent** %9
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %35
15: ; preds = %3
%16 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* %7
%20 = call i32 @htons(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
%24 = load %struct.hostent*, %struct.hostent** %9
%25 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @memcpy(i32* %23, i32 %26, i32 4)
%28 = load i32, i32* %5
%29 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%30 = call i64 @connect(i32 %28, %struct.sockaddr* %29, i32 4)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %15
%33 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %35
34: ; preds = %15
store i32 0, i32* %4
br label %35
35: ; preds = %34, %32, %13
%36 = load i32, i32* %4
ret i32 %36
}

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