#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;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ *h_addr_list;
};
typedef int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int client2server_socket(const char *gateway, int port) {
  int clientfd = socket(AF_INET, SOCK_STREAM, 0);
  struct hostent *server;
  struct sockaddr_in servaddr;
  if (clientfd < 0) {
    perror("Client Socket");
    return clientfd;
  }
  server = gethostbyname(gateway);
  if (server == NULL) {
    perror("Server not found %s\n", gateway);
    close(clientfd);
    return -1;
  }
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  memcpy((char *)&servaddr.sin_addr.s_addr, (char *)server->h_addr_list[0],
         server->h_length);
  servaddr.sin_port = htons(port);
  if (connect(clientfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    perror("Client connect");
    close(clientfd);
    return -1;
  }
  return clientfd;
}


x86 assembly (clang, -O0):
<client2server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ce <client2server_socket+0x4e>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1080 <perror@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1294 <client2server_socket+0x114>
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120c <client2server_socket+0x8c>
mov    -0x10(%rbp),%rsi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1080 <perror@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <client2server_socket+0x114>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
add    $0x4,%rdi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1070 <memcpy@plt>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    128e <client2server_socket+0x10e>
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1080 <perror@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <client2server_socket+0x114>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client2server_socket>:
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
test   %eax,%eax
js     1221 <client2server_socket+0xa1>
mov    %rbp,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1231 <client2server_socket+0xb1>
mov    %rax,%rbx
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
lea    0x4(%rsp),%rdi
mov    0x8(%rbx),%rax
mov    (%rax),%rsi
mov    (%rbx),%edx
call   1070 <memcpy@plt>
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     1244 <client2server_socket+0xc4>
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd8(%rip),%rdi
xor    %eax,%eax
call   1080 <perror@plt>
jmp    1211 <client2server_socket+0x91>
lea    0xdd6(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1080 <perror@plt>
jmp    1252 <client2server_socket+0xd2>
lea    0xdd8(%rip),%rdi
xor    %eax,%eax
call   1080 <perror@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    $0xffffffff,%r15d
jmp    1211 <client2server_socket+0x91>

LLVM IR (-O0):
%struct.hostent = type { i32, i64* }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [14 x i8] c"Client Socket\00"
@.str.1 = [21 x i8] c"Server not found %s\0A\00"
@.str.2 = [15 x i8] c"Client connect\00"
define i32 @client2server_socket(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.hostent*
%8 = alloca %struct.sockaddr_in
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 slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 (i8*, ...) @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* %6
store i32 %16, i32* %3
br label %57
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call %struct.hostent* @gethostbyname(i8* %18)
store %struct.hostent* %19, %struct.hostent** %7
%20 = load %struct.hostent*, %struct.hostent** %7
%21 = icmp eq %struct.hostent* %20, null
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = load i8*, i8** %4
%24 = call i32 (i8*, ...) @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = load i32, i32* %6
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %3
br label %57
27: ; preds = %17
%28 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%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 = getelementptr inbounds i64, i64* %36, i64 0
%38 = load i64, i64* %37
%39 = inttoptr i64 %38 to i8*
%40 = load %struct.hostent*, %struct.hostent** %7
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @memcpy(i8* %33, i8* %39, i32 %42)
%44 = load i32, i32* %5
%45 = call i32 @htons(i32 %44)
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %45, i32* %46
%47 = load i32, i32* %6
%48 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%49 = call i64 @connect(i32 %47, %struct.sockaddr* %48, i32 12)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %27
%52 = call i32 (i8*, ...) @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%53 = load i32, i32* %6
%54 = call i32 @close(i32 %53)
store i32 -1, i32* %3
br label %57
55: ; preds = %27
%56 = load i32, i32* %6
store i32 %56, i32* %3
br label %57
57: ; preds = %55, %51, %22, %14
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, 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 = [14 x i8] c"Client Socket\00"
@.str.1 = [21 x i8] c"Server not found %s\0A\00"
@.str.2 = [15 x i8] c"Client connect\00"
define i32 @client2server_socket(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = icmp slt i32 %6, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 (i8*, ...) @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %38
11: ; preds = %2
%12 = tail call %struct.hostent* @gethostbyname(i8* %0)
%13 = icmp eq %struct.hostent* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = tail call i32 (i8*, ...) @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%16 = tail call i32 @close(i32 %6)
br label %38
17: ; preds = %11
%18 = call i32 @memset(%struct.sockaddr_in* %3, 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 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%22 = bitcast i32* %21 to i8*
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 1
%24 = load i64*, i64** %23
%25 = load i64, i64* %24
%26 = inttoptr i64 %25 to i8*
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @memcpy(i8* %22, i8* %26, i32 %28)
%30 = call i32 @htons(i32 %1)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %30, i32* %31
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%33 = call i64 @connect(i32 %6, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %17
%36 = call i32 (i8*, ...) @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @close(i32 %6)
br label %38
38: ; preds = %17, %35, %14, %9
%39 = phi i32 [ %6, %9 ], [ -1, %14 ], [ -1, %35 ], [ %6, %17 ]
ret i32 %39
}