#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 */
typedef int uint16_t;
struct in_addr {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_zero;
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(int /*<<< orphan*/ *, int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_connect(char *ip, uint16_t port) {
  struct hostent *host;
  int n, sock;
  struct sockaddr_in server_addr;
  host = gethostbyname(ip);
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("Socket");
    exit(1);
  }
  port = 22222;
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(port);
  server_addr.sin_addr = *((struct in_addr *)host->h_addr);
  bzero(&(server_addr.sin_zero), 8);
  if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) ==
      -1) {
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x28(%rbp)
cmp    $0xffffffff,%eax
jne    11c7 <tcp_connect+0x57>
lea    0xe48(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x56ce,-0x14(%rbp)
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    -0x28(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    1225 <tcp_connect+0xb5>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <tcp_connect+0xbb>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_connect>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
call   1040 <gethostbyname@plt>
mov    %rax,%r14
mov    0x2e60(%rip),%r15
mov    (%r15),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     11ef <tcp_connect+0x7f>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    $0x56ce,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%r14),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
cmovne %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe0a(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"Socket\00"
define i32 @tcp_connect(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.hostent*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i8*, i8** %4
%11 = call %struct.hostent* @gethostbyname(i8* %10)
store %struct.hostent* %11, %struct.hostent** %6
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %8
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
store i32 22222, i32* %5
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 3
store i32 %20, i32* %21
%22 = load i32, i32* %5
%23 = call i32 @htons(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%26 = load %struct.hostent*, %struct.hostent** %6
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 0
%28 = load i64, i64* %27
%29 = inttoptr i64 %28 to %struct.in_addr*
%30 = bitcast %struct.in_addr* %25 to i8*
%31 = bitcast %struct.in_addr* %29 to i8*
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%33 = call i32 @bzero(i32* %32, i32 8)
%34 = load i32, i32* %8
%35 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%36 = call i32 @connect(i32 %34, %struct.sockaddr* %35, i32 4)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %39
38: ; preds = %19
store i32 -1, i32* %3
br label %41
39: ; preds = %19
%40 = load i32, i32* %8
store i32 %40, i32* %3
br label %41
41: ; preds = %39, %38
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"Socket\00"
define i32 @tcp_connect(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call %struct.hostent* @gethostbyname(i8* %0)
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 3
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 22222)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%19 = load i64, i64* %18
%20 = inttoptr i64 %19 to i32*
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%22 = load i32, i32* %20
store i32 %22, i32* %21
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%24 = call i32 @bzero(i32* %23, i32 8)
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i32 @connect(i32 %8, %struct.sockaddr* %25, i32 4)
%27 = icmp eq i32 %26, -1
%28 = select i1 %27, i32 -1, i32 %8
ret i32 %28
}