#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serv;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sapi_inet_tcp_client_create(char *ip, int port) {
  int ret;
  struct sockaddr_in serv = {.sin_family = AF_INET,
                             .sin_addr.s_addr = inet_addr(ip),
                             .sin_port = htons(port)};
  int sock;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0)
    return -1;
  ret = connect(sock, (struct sockaddr *)&serv, sizeof(serv));
  if (ret < 0)
    goto err_connect;
  return sock;
err_connect:
  close(sock);
  return -1;
}


x86 assembly (clang, -O0):
<sapi_inet_tcp_client_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11b4 <sapi_inet_tcp_client_create+0x64>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f1 <sapi_inet_tcp_client_create+0xa1>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1060 <connect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11d7 <sapi_inet_tcp_client_create+0x87>
jmp    11e2 <sapi_inet_tcp_client_create+0x92>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f1 <sapi_inet_tcp_client_create+0xa1>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sapi_inet_tcp_client_create>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
mov    %esi,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %rbx,%rdi
call   1050 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x10(%rsp)
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11b8 <sapi_inet_tcp_client_create+0x68>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1060 <connect@plt>
test   %eax,%eax
js     11b1 <sapi_inet_tcp_client_create+0x61>
mov    %ebp,%ebx
jmp    11b8 <sapi_inet_tcp_client_create+0x68>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @sapi_inet_tcp_client_create(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%10 = load i32, i32* %5
%11 = call i32 @htons(i32 %10)
store i32 %11, i32* %9
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%13 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %12, i32 0, i32 0
%14 = load i8*, i8** %4
%15 = call i32 @inet_addr(i8* %14)
store i32 %15, i32* %13
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
%17 = load i32, i32* @AF_INET
store i32 %17, i32* %16
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %2
store i32 -1, i32* %3
br label %36
24: ; preds = %2
%25 = load i32, i32* %8
%26 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%27 = call i32 @connect(i32 %25, %struct.sockaddr* %26, i32 12)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24
br label %33
31: ; preds = %24
%32 = load i32, i32* %8
store i32 %32, i32* %3
br label %36
33: ; preds = %30
%34 = load i32, i32* %8
%35 = call i32 @close(i32 %34)
store i32 -1, i32* %3
br label %36
36: ; preds = %33, %31, %23
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @sapi_inet_tcp_client_create(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%6 = tail call i32 @htons(i32 %1)
store i32 %6, i32* %5
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%8 = tail call i32 @inet_addr(i8* %0)
store i32 %8, i32* %7
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%10 = load i32, i32* @AF_INET
store i32 %10, i32* %9
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %2
%15 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%16 = call i32 @connect(i32 %12, %struct.sockaddr* %15, i32 12)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @close(i32 %12)
br label %20
20: ; preds = %14, %2, %18
%21 = phi i32 [ -1, %18 ], [ -1, %2 ], [ %12, %14 ]
ret i32 %21
}