#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_family;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ *h_addr_list;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
scalar_t__ connect(int, struct sockaddr *, int);
void *getenv(char *);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_connect(const char *host, int port) {
  struct sockaddr_in addr;
  struct hostent *h_name;
  char *portenv;
  int sockfd;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    return -1;
  if (host == NULL) {
    host = getenv("HARMONY_S_HOST");
    if (host == NULL)
      host = "localhost";
  }
  h_name = gethostbyname(host);
  if (!h_name)
    return -1;
  memcpy(&addr.sin_addr, h_name->h_addr_list[0], sizeof(struct in_addr));
  if (port == 0) {
    portenv = getenv("HARMONY_S_PORT");
    if (portenv)
      port = atoi(portenv);
    else
      port = 1979;
  }
  addr.sin_port = htons(port);
  addr.sin_family = AF_INET;
  if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    return -1;
  return sockfd;
}


x86 assembly (clang, -O0):
<tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11b1 <tcp_connect+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <tcp_connect+0x13c>
cmpq   $0x0,-0x10(%rbp)
jne    11e7 <tcp_connect+0x77>
lea    0xe3d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e2 <tcp_connect+0x72>
lea    0xe31(%rip),%rax
mov    %rax,-0x10(%rbp)
jmp    11e7 <tcp_connect+0x77>
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    120b <tcp_connect+0x9b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <tcp_connect+0x13c>
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1060 <memcpy@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1268 <tcp_connect+0xf8>
lea    0xde2(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     125c <tcp_connect+0xec>
mov    -0x30(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x14(%rbp)
jmp    1263 <tcp_connect+0xf3>
movl   $0x7bb,-0x14(%rbp)
jmp    1268 <tcp_connect+0xf8>
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    12a6 <tcp_connect+0x136>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <tcp_connect+0x13c>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e5f(%rip),%r12
mov    (%r12),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1248 <tcp_connect+0xd8>
mov    %eax,%r15d
test   %rbp,%rbp
jne    11cc <tcp_connect+0x5c>
lea    0xe47(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe47(%rip),%rbp
cmovne %rax,%rbp
mov    %rbp,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1248 <tcp_connect+0xd8>
lea    0x8(%rsp),%rdi
mov    (%rax),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1060 <memcpy@plt>
test   %r14d,%r14d
jne    1216 <tcp_connect+0xa6>
lea    0xe20(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1210 <tcp_connect+0xa0>
mov    %rax,%rdi
call   1070 <atoi@plt>
mov    %eax,%r14d
jmp    1216 <tcp_connect+0xa6>
mov    $0x7bb,%r14d
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r12),%eax
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovs  %eax,%r15d
mov    %r15d,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
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
@SOCK_STREAM = global i32 0
@.str = [15 x i8] c"HARMONY_S_HOST\00"
@.str.1 = [10 x i8] c"localhost\00"
@.str.2 = [15 x i8] c"HARMONY_S_PORT\00"
define i32 @tcp_connect(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %63
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %25
19: ; preds = %16
%20 = call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %19
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %4
br label %24
24: ; preds = %23, %19
br label %25
25: ; preds = %24, %16
%26 = load i8*, i8** %4
%27 = call %struct.hostent* @gethostbyname(i8* %26)
store %struct.hostent* %27, %struct.hostent** %7
%28 = load %struct.hostent*, %struct.hostent** %7
%29 = icmp ne %struct.hostent* %28, null
br i1 %29, label %31, label %30
30: ; preds = %25
store i32 -1, i32* %3
br label %63
31: ; preds = %25
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%33 = load %struct.hostent*, %struct.hostent** %7
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i32 0, i32 0
%35 = load i32*, i32** %34
%36 = getelementptr inbounds i32, i32* %35, i64 0
%37 = load i32, i32* %36
%38 = call i32 @memcpy(i32* %32, i32 %37, i32 4)
%39 = load i32, i32* %5
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %50
41: ; preds = %31
%42 = call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
store i8* %42, i8** %8
%43 = load i8*, i8** %8
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i8*, i8** %8
%47 = call i32 @atoi(i8* %46)
store i32 %47, i32* %5
br label %49
48: ; preds = %41
store i32 1979, i32* %5
br label %49
49: ; preds = %48, %45
br label %50
50: ; preds = %49, %31
%51 = load i32, i32* %5
%52 = call i32 @htons(i32 %51)
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %52, i32* %53
%54 = load i32, i32* @AF_INET
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %54, i32* %55
%56 = load i32, i32* %9
%57 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%58 = call i64 @connect(i32 %56, %struct.sockaddr* %57, i32 12)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %50
store i32 -1, i32* %3
br label %63
61: ; preds = %50
%62 = load i32, i32* %9
store i32 %62, i32* %3
br label %63
63: ; preds = %61, %60, %30, %15
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [15 x i8] c"HARMONY_S_HOST\00"
@.str.1 = [10 x i8] c"localhost\00"
@.str.2 = [15 x i8] c"HARMONY_S_PORT\00"
define i32 @tcp_connect(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 slt i32 %7, 0
br i1 %8, label %41, label %9
9: ; preds = %2
%10 = icmp eq i8* %0, null
br i1 %10, label %11, label %15
11: ; preds = %9
%12 = tail call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i8* %12, null
%14 = select i1 %13, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %12
br label %15
15: ; preds = %11, %9
%16 = phi i8* [ %14, %11 ], [ %0, %9 ]
%17 = tail call %struct.hostent* @gethostbyname(i8* %16)
%18 = icmp eq %struct.hostent* %17, null
br i1 %18, label %41, label %19
19: ; preds = %15
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %17, i64 0, i32 0
%22 = load i32*, i32** %21
%23 = load i32, i32* %22
%24 = call i32 @memcpy(i32* %20, i32 %23, i32 4)
%25 = icmp eq i32 %1, 0
br i1 %25, label %26, label %31
26: ; preds = %19
%27 = call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp eq i8* %27, null
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = call i32 @atoi(i8* %27)
br label %31
31: ; preds = %26, %29, %19
%32 = phi i32 [ %30, %29 ], [ %1, %19 ], [ 1979, %26 ]
%33 = call i32 @htons(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %33, i32* %34
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %35, i32* %36
%37 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%38 = call i64 @connect(i32 %7, %struct.sockaddr* %37, i32 12)
%39 = icmp slt i64 %38, 0
%40 = select i1 %39, i32 -1, i32 %7
br label %41
41: ; preds = %31, %15, %2
%42 = phi i32 [ -1, %2 ], [ -1, %15 ], [ %40, %31 ]
ret i32 %42
}