#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_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ error(char *);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int const);
int /*<<< orphan*/ memcpy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ puts(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to_server_sock(const char *server_name, const int port) {
  int sock;
  struct sockaddr_in serv_addr;
  struct hostent *server;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock == -1)
    error("ERROR opening socket");
  puts("Socket created...");
  server = gethostbyname(server_name);
  if (server == NULL)
    error("ERROR resloving host");
  memset((char *)&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(port);
  memcpy((char *)&serv_addr.sin_addr.s_addr, (char *)server->h_addr,
         server->h_length);
  if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1)
    error("ERROR connecting");
  puts("Connection established...");
  return sock;
}


x86 assembly (clang, -O0):
<connect_to_server_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11c1 <connect_to_server_sock+0x41>
lea    0xe44(%rip),%rdi
call   1080 <error@plt>
lea    0xe4d(%rip),%rdi
call   1030 <puts@plt>
mov    -0x8(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f1 <connect_to_server_sock+0x71>
lea    0xe3b(%rip),%rdi
call   1080 <error@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1070 <memcpy@plt>
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    1255 <connect_to_server_sock+0xd5>
lea    0xdec(%rip),%rdi
call   1080 <error@plt>
lea    0xdf1(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_server_sock>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e4f(%rip),%r12
mov    (%r12),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    11c1 <connect_to_server_sock+0x41>
lea    0xe44(%rip),%rdi
call   1080 <error@plt>
lea    0xe4d(%rip),%rdi
call   1030 <puts@plt>
mov    %rbp,%rdi
call   1060 <gethostbyname@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11e9 <connect_to_server_sock+0x69>
lea    0xe43(%rip),%rdi
call   1080 <error@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x8(%rbp),%rsi
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
call   1070 <memcpy@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    123f <connect_to_server_sock+0xbf>
lea    0xe02(%rip),%rdi
call   1080 <error@plt>
lea    0xe07(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@.str.1 = [18 x i8] c"Socket created...\00"
@.str.2 = [21 x i8] c"ERROR resloving host\00"
@.str.3 = [17 x i8] c"ERROR connecting\00"
@.str.4 = [26 x i8] c"Connection established...\00"
define i32 @connect_to_server_sock(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%17 = load i8*, i8** %3
%18 = call %struct.hostent* @gethostbyname(i8* %17)
store %struct.hostent* %18, %struct.hostent** %7
%19 = load %struct.hostent*, %struct.hostent** %7
%20 = icmp eq %struct.hostent* %19, null
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %15
%24 = bitcast %struct.sockaddr_in* %6 to i8*
%25 = call i32 @memset(i8* %24, i32 0, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %26, i32* %27
%28 = load i32, i32* %4
%29 = call i32 @htons(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 0
%33 = bitcast i32* %32 to i8*
%34 = load %struct.hostent*, %struct.hostent** %7
%35 = getelementptr inbounds %struct.hostent, %struct.hostent* %34, i32 0, i32 1
%36 = load i64, i64* %35
%37 = inttoptr i64 %36 to i8*
%38 = load %struct.hostent*, %struct.hostent** %7
%39 = getelementptr inbounds %struct.hostent, %struct.hostent* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @memcpy(i8* %33, i8* %37, i32 %40)
%42 = load i32, i32* %5
%43 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%44 = call i32 @connect(i32 %42, %struct.sockaddr* %43, i32 12)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %23
%47 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %48
48: ; preds = %46, %23
%49 = call i32 @puts(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@.str.1 = [18 x i8] c"Socket created...\00"
@.str.2 = [21 x i8] c"ERROR resloving host\00"
@.str.3 = [17 x i8] c"ERROR connecting\00"
@.str.4 = [26 x i8] c"Connection established...\00"
define i32 @connect_to_server_sock(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 %9, label %11
9: ; preds = %2
%10 = tail call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call %struct.hostent* @gethostbyname(i8* %0)
%14 = icmp eq %struct.hostent* %13, null
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = call i32 @memset(i8* %4, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %19, i32* %20
%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 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i64 0, i32 1
%24 = load i64, i64* %23
%25 = inttoptr i64 %24 to i8*
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i64 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @memcpy(i8* %4, i8* %25, i32 %27)
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i32 @connect(i32 %7, %struct.sockaddr* %29, i32 12)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %17
%33 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %32, %17
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
ret i32 %7
}