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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int inet_pton(int /*<<< orphan*/, char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcpconn(const char *ip, int port) {
  int sock;
  struct sockaddr_in addr;
  int reuse = 1;
  if (ip == NULL || port <= 0)
    return -1;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock == -1)
    goto err;
  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int));
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  if (inet_pton(AF_INET, ip, &addr.sin_addr) != 1)
    goto err;
  if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1)
    goto err;
  return sock;
err:
  if (sock != -1)
    close(sock);
  return -1;
}


x86 assembly (clang, -O0):
<tcpconn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x2c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119b <tcpconn+0x2b>
cmpl   $0x0,-0x14(%rbp)
jg     11a7 <tcpconn+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <tcpconn+0x114>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11d2 <tcpconn+0x62>
jmp    126b <tcpconn+0xfb>
mov    -0x18(%rbp),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1070 <inet_pton@plt>
cmp    $0x1,%eax
je     1241 <tcpconn+0xd1>
jmp    126b <tcpconn+0xfb>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    1260 <tcpconn+0xf0>
jmp    126b <tcpconn+0xfb>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1284 <tcpconn+0x114>
cmpl   $0xffffffff,-0x18(%rbp)
je     127d <tcpconn+0x10d>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpconn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movl   $0x1,0x4(%rsp)
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     124d <tcpconn+0xdd>
mov    %esi,%ebp
test   %esi,%esi
jle    124d <tcpconn+0xdd>
mov    %rdi,%rbx
mov    0x2e37(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     124d <tcpconn+0xdd>
mov    %eax,%r15d
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    0x2def(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %r15d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x0(%r13),%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1070 <inet_pton@plt>
cmp    $0x1,%eax
jne    1245 <tcpconn+0xd5>
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
je     1245 <tcpconn+0xd5>
mov    %r15d,%r14d
jmp    124d <tcpconn+0xdd>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %r14d,%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, i32, 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
define i32 @tcpconn(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
store i32 1, i32* %8
%9 = load i8*, i8** %4
%10 = icmp eq i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i32, i32* %5
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %11, %2
store i32 -1, i32* %3
br label %54
15: ; preds = %11
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %15
br label %47
22: ; preds = %15
%23 = load i32, i32* %6
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_REUSEADDR
%26 = call i32 @setsockopt(i32 %23, i32 %24, i32 %25, i32* %8, i32 4)
%27 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %28, i32* %29
%30 = load i32, i32* %5
%31 = call i32 @htons(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %31, i32* %32
%33 = load i32, i32* @AF_INET
%34 = load i8*, i8** %4
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%36 = call i32 @inet_pton(i32 %33, i8* %34, i32* %35)
%37 = icmp ne i32 %36, 1
br i1 %37, label %38, label %39
38: ; preds = %22
br label %47
39: ; preds = %22
%40 = load i32, i32* %6
%41 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%42 = call i32 @connect(i32 %40, %struct.sockaddr* %41, i32 12)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %45
44: ; preds = %39
br label %47
45: ; preds = %39
%46 = load i32, i32* %6
store i32 %46, i32* %3
br label %54
47: ; preds = %44, %38, %21
%48 = load i32, i32* %6
%49 = icmp ne i32 %48, -1
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i32, i32* %6
%52 = call i32 @close(i32 %51)
br label %53
53: ; preds = %50, %47
store i32 -1, i32* %3
br label %54
54: ; preds = %53, %45, %14
%55 = load i32, i32* %3
ret i32 %55
}

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