#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*/ on;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ TCP_NODELAY;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int x_create_tcp_client(const char *rip, int rport) {
  struct sockaddr_in addr;
  int result;
  int on = 1;
  int fd;
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd == -1) {
    return -1;
  }
  result = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
  if (result < 0) {
    close(fd);
    fd = -1;
    return -1;
  }
  on = 1;
  result = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&on, sizeof(on));
  result = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof(on));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = inet_addr(rip);
  addr.sin_port = htons(rport);
  result = connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr));
  if (result != 0) {
    close(fd);
    fd = -1;
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<x_create_tcp_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x28(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11a8 <x_create_tcp_client+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ab <x_create_tcp_client+0x14b>
mov    -0x2c(%rbp),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11f4 <x_create_tcp_client+0x94>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x2c(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    12ab <x_create_tcp_client+0x14b>
movl   $0x1,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x24(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12a5 <x_create_tcp_client+0x145>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x2c(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    12ab <x_create_tcp_client+0x14b>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<x_create_tcp_client>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
movl   $0x1,0x4(%rsp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     1264 <x_create_tcp_client+0x104>
mov    %eax,%ebp
mov    0x2e3c(%rip),%r13
mov    0x0(%r13),%esi
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     1259 <x_create_tcp_client+0xf9>
movl   $0x1,0x4(%rsp)
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%r12
mov    %ebp,%edi
mov    %r12,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x0(%r13),%esi
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %r12,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %r15,%rdi
call   1060 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1070 <connect@plt>
test   %eax,%eax
je     1262 <x_create_tcp_client+0x102>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1264 <x_create_tcp_client+0x104>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
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
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@SO_KEEPALIVE = global i32 0
define i32 @x_create_tcp_client(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 1, i32* %8
%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 eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %57
16: ; preds = %2
%17 = load i32, i32* %9
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_REUSEADDR
%20 = bitcast i32* %8 to i8*
%21 = call i32 @setsockopt(i32 %17, i32 %18, i32 %19, i8* %20, i32 4)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = load i32, i32* %9
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %9
store i32 -1, i32* %3
br label %57
27: ; preds = %16
store i32 1, i32* %8
%28 = load i32, i32* %9
%29 = load i32, i32* @IPPROTO_TCP
%30 = load i32, i32* @TCP_NODELAY
%31 = bitcast i32* %8 to i8*
%32 = call i32 @setsockopt(i32 %28, i32 %29, i32 %30, i8* %31, i32 4)
store i32 %32, i32* %7
%33 = load i32, i32* %9
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_KEEPALIVE
%36 = bitcast i32* %8 to i8*
%37 = call i32 @setsockopt(i32 %33, i32 %34, i32 %35, i8* %36, i32 4)
store i32 %37, i32* %7
%38 = load i32, i32* @AF_INET
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %38, i32* %39
%40 = load i8*, i8** %4
%41 = call i32 @inet_addr(i8* %40)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%43 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %42, i32 0, i32 0
store i32 %41, i32* %43
%44 = load i32, i32* %5
%45 = call i32 @htons(i32 %44)
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %45, i32* %46
%47 = load i32, i32* %9
%48 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%49 = call i32 @connect(i32 %47, %struct.sockaddr* %48, i32 4)
store i32 %49, i32* %7
%50 = load i32, i32* %7
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %27
%53 = load i32, i32* %9
%54 = call i32 @close(i32 %53)
store i32 -1, i32* %9
store i32 -1, i32* %3
br label %57
55: ; preds = %27
%56 = load i32, i32* %9
store i32 %56, i32* %3
br label %57
57: ; preds = %55, %52, %24, %15
%58 = load i32, i32* %3
ret i32 %58
}

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
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@SO_KEEPALIVE = global i32 0
define i32 @x_create_tcp_client(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %34, label %11
11: ; preds = %2
%12 = load i32, i32* @SOL_SOCKET
%13 = load i32, i32* @SO_REUSEADDR
%14 = call i32 @setsockopt(i32 %9, i32 %12, i32 %13, i8* %6, i32 4)
%15 = icmp slt i32 %14, 0
br i1 %15, label %32, label %16
16: ; preds = %11
store i32 1, i32* %4
%17 = load i32, i32* @IPPROTO_TCP
%18 = load i32, i32* @TCP_NODELAY
%19 = call i32 @setsockopt(i32 %9, i32 %17, i32 %18, i8* %6, i32 4)
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_KEEPALIVE
%22 = call i32 @setsockopt(i32 %9, i32 %20, i32 %21, i8* %6, i32 4)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %23, i32* %24
%25 = call i32 @inet_addr(i8* %0)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %25, i32* %26
%27 = call i32 @htons(i32 %1)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %27, i32* %28
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i32 @connect(i32 %9, %struct.sockaddr* %29, i32 4)
%31 = icmp eq i32 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %16, %11
%33 = call i32 @close(i32 %9)
br label %34
34: ; preds = %32, %16, %2
%35 = phi i32 [ -1, %2 ], [ %9, %16 ], [ -1, %32 ]
ret i32 %35
}