#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 {
  unsigned long *h_addr;
  int h_length;
};
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);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
unsigned long inet_addr(char const *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, unsigned long *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
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 {
    hp = gethostbyname(host);
    if (hp == NULL)
      return -1;
    memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
  }
  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    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e2d(%rip),%rcx
cmp    (%rcx),%rax
je     11d7 <Socket+0x67>
lea    -0x30(%rbp),%rdi
add    $0x4,%rdi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1070 <memcpy@plt>
jmp    1216 <Socket+0xa6>
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11fb <Socket+0x8b>
movl   $0xffffffff,-0x4(%rbp)
jmp    127f <Socket+0x10f>
lea    -0x30(%rbp),%rdi
add    $0x4,%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%edx
call   1070 <memcpy@plt>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1252 <Socket+0xe2>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    127f <Socket+0x10f>
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1279 <Socket+0x109>
movl   $0xffffffff,-0x4(%rbp)
jmp    127f <Socket+0x10f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e53(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    %rbx,%rdi
call   1050 <inet_addr@plt>
mov    %rax,0x18(%rsp)
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%rax
jne    11cf <Socket+0x5f>
mov    %rbx,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1225 <Socket+0xb5>
lea    0xc(%rsp),%rdi
mov    (%rax),%rsi
mov    0x8(%rax),%edx
jmp    11de <Socket+0x6e>
lea    0xc(%rsp),%rdi
lea    0x18(%rsp),%rsi
mov    $0x8,%edx
call   1070 <memcpy@plt>
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%r14),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     122a <Socket+0xba>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovs  %eax,%ebx
jmp    122a <Socket+0xba>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i64*, 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 @memcpy(i32* %19, i64* %7, i32 8)
br label %36
21: ; preds = %2
%22 = load i8*, i8** %4
%23 = call %struct.hostent* @gethostbyname(i8* %22)
store %struct.hostent* %23, %struct.hostent** %9
%24 = load %struct.hostent*, %struct.hostent** %9
%25 = icmp eq %struct.hostent* %24, null
br i1 %25, label %26, label %27
26: ; preds = %21
store i32 -1, i32* %3
br label %55
27: ; preds = %21
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%29 = load %struct.hostent*, %struct.hostent** %9
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 0
%31 = load i64*, i64** %30
%32 = load %struct.hostent*, %struct.hostent** %9
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %32, i32 0, i32 1
%34 = load i32, i32* %33
%35 = call i32 @memcpy(i32* %28, i64* %31, i32 %34)
br label %36
36: ; preds = %27, %18
%37 = load i32, i32* %5
%38 = call i32 @htons(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %38, i32* %39
%40 = load i32, i32* @AF_INET
%41 = load i32, i32* @SOCK_STREAM
%42 = call i32 @socket(i32 %40, i32 %41, i32 0)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %36
%46 = load i32, i32* %6
store i32 %46, i32* %3
br label %55
47: ; preds = %36
%48 = load i32, i32* %6
%49 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%50 = call i64 @connect(i32 %48, %struct.sockaddr* %49, i32 12)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %47
store i32 -1, i32* %3
br label %55
53: ; preds = %47
%54 = load i32, i32* %6
store i32 %54, i32* %3
br label %55
55: ; preds = %53, %52, %45, %26
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i64*, 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
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%15 = call i32 @memcpy(i32* %14, i64* %3, i32 8)
br label %26
16: ; preds = %2
%17 = call %struct.hostent* @gethostbyname(i8* %0)
%18 = icmp eq %struct.hostent* %17, null
br i1 %18, label %38, label %19
19: ; preds = %16
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %17, i64 0, i32 0
%22 = load i64*, i64** %21
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %17, i64 0, i32 1
%24 = load i32, i32* %23
%25 = call i32 @memcpy(i32* %20, i64* %22, i32 %24)
br label %26
26: ; preds = %19, %13
%27 = call i32 @htons(i32 %1)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* @AF_INET
%30 = load i32, i32* @SOCK_STREAM
%31 = call i32 @socket(i32 %29, i32 %30, i32 0)
%32 = icmp slt i32 %31, 0
br i1 %32, label %38, label %33
33: ; preds = %26
%34 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%35 = call i64 @connect(i32 %31, %struct.sockaddr* %34, i32 12)
%36 = icmp slt i64 %35, 0
%37 = select i1 %36, i32 -1, i32 %31
br label %38
38: ; preds = %33, %26, %16
%39 = phi i32 [ -1, %16 ], [ %31, %26 ], [ %37, %33 ]
ret i32 %39
}