#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_zero;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};

/* 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*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int start_tcp_client(char *ip, int port) {
  struct sockaddr_in p_addr;
  int sock;
  inet_pton(AF_INET, ip, &(p_addr.sin_addr));
  p_addr.sin_port = htons(port);
  p_addr.sin_family = AF_INET;
  bzero(&(p_addr.sin_zero), 8);
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("Could not open Socket!");
    return -1;
  }
  if (connect(sock, (struct sockaddr *)&p_addr, sizeof(struct sockaddr)) ==
      -1) {
    perror("Could not connect to socket on!");
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<start_tcp_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
add    $0xc,%rdx
call   1040 <inet_pton@plt>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    -0x28(%rbp),%rdi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    11eb <start_tcp_client+0x8b>
lea    0xe26(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1223 <start_tcp_client+0xc3>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    121d <start_tcp_client+0xbd>
lea    0xe0b(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1223 <start_tcp_client+0xc3>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_tcp_client>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %rdi,%rsi
mov    0x2e75(%rip),%r14
mov    (%r14),%edi
lea    0x14(%rsp),%rdx
call   1040 <inet_pton@plt>
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x10(%rsp)
mov    (%r14),%ebx
mov    %ebx,0xc(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11da <start_tcp_client+0x7a>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     11e3 <start_tcp_client+0x83>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe1f(%rip),%rdi
jmp    11ea <start_tcp_client+0x8a>
lea    0xe2d(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11d0 <start_tcp_client+0x70>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"Could not open Socket!\00"
@.str.1 = [32 x i8] c"Could not connect to socket on!\00"
define i32 @start_tcp_client(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 3
%7 = call i32 @inet_pton(i32 %5, i8* %0, i32* %6)
%8 = call i32 @htons(i32 %1)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %8, i32* %9
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%13 = call i32 @bzero(i32* %12, i32 8)
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %10, i32 %14, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %21, label %17
17: ; preds = %2
%18 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%19 = call i32 @connect(i32 %15, %struct.sockaddr* %18, i32 4)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %17, %2
%22 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), %17 ]
%23 = call i32 @perror(i8* %22)
br label %24
24: ; preds = %21, %17
%25 = phi i32 [ %15, %17 ], [ -1, %21 ]
ret i32 %25
}