#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_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int dummy;
};
typedef int /*<<< orphan*/ inaddr;
typedef int /*<<< orphan*/ ad;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
unsigned long INADDR_NONE;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
unsigned long inet_addr(char const *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, ...);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char const *);
int Socket(const char *host, int clientPort) {
  int sock;
  unsigned long inaddr;
  struct sockaddr_in ad;
  struct hostent *hp;
  memset(&ad, 0, sizeof(ad));
  ad.sin_family = AF_INET;
  inaddr = inet_addr(host);
  if (inaddr != INADDR_NONE)
    memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
  else
    memcpy(&ad.sin_addr, host, strlen(host));
  ad.sin_port = htons(clientPort);
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0)
    return sock;
  if (connect(sock, (struct sockaddr *)&ad, sizeof(ad)) < 0)
    return -1;
  return sock;
}


x86 assembly (clang, -O0):
<Socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e2d(%rip),%rcx
cmp    (%rcx),%rax
je     11d9 <Socket+0x69>
lea    -0x30(%rbp),%rdi
add    $0x4,%rdi
xor    %eax,%eax
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1070 <memcpy@plt>
jmp    1207 <Socket+0x97>
lea    -0x30(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <memcpy@plt>
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1243 <Socket+0xd3>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1270 <Socket+0x100>
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    126a <Socket+0xfa>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <Socket+0x100>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e53(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    %rbx,%rdi
call   1060 <inet_addr@plt>
mov    %rax,0x10(%rsp)
mov    0x2e30(%rip),%rcx
lea    0x4(%rsp),%r14
cmp    (%rcx),%rax
jne    11d3 <Socket+0x63>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <memcpy@plt>
jmp    11e7 <Socket+0x77>
lea    0x10(%rsp),%rsi
mov    $0x8,%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <memcpy@plt>
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    (%r15),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1224 <Socket+0xb4>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovs  %eax,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_NONE = global i64 0
@SOCK_STREAM = global i32 0
define i32 @Socket(i8* %0, i32 %1) {
%3 = alloca i64
%4 = alloca %struct.sockaddr_in
%5 = bitcast i64* %3 to i8*
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %8, i32* %9
%10 = call i64 @inet_addr(i8* %0)
store i64 %10, i64* %3
%11 = load i64, i64* @INADDR_NONE
%12 = icmp eq i64 %10, %11
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
br i1 %12, label %16, label %14
14: ; preds = %2
%15 = call i32 (i32*, ...) @memcpy(i32* %13, i64* %3, i64 8)
br label %19
16: ; preds = %2
%17 = call i32 @strlen(i8* %0)
%18 = call i32 (i32*, ...) @memcpy(i32* %13, i8* %0, i32 %17)
br label %19
19: ; preds = %16, %14
%20 = call i32 @htons(i32 %1)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
%25 = icmp slt i32 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %19
%27 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%28 = call i64 @connect(i32 %24, %struct.sockaddr* %27, i32 12)
%29 = icmp slt i64 %28, 0
%30 = select i1 %29, i32 -1, i32 %24
br label %31
31: ; preds = %26, %19
%32 = phi i32 [ %24, %19 ], [ %30, %26 ]
ret i32 %32
}