#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 s_addr;
};
struct sockaddr_in {
  unsigned short sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_open(int ip, unsigned short port) {
  struct sockaddr_in addr;
  int sox;
  memset(&addr, 0, sizeof addr);
  addr.sin_family = PF_INET;
  addr.sin_addr.s_addr = ip;
  addr.sin_port = port;
  sox = socket(AF_INET, SOCK_STREAM, 0);
  if (connect(sox, (struct sockaddr *)&addr, sizeof addr) == 0)
    return sox;
  close(sox);
  return -1;
}


x86 assembly (clang, -O0):
<tcp_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %si,%ax
mov    %edi,-0x8(%rbp)
mov    %ax,-0xa(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1030 <memset@plt>
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xa(%rbp),%ax
mov    %ax,-0x18(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
jne    11be <tcp_open+0x7e>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11cd <tcp_open+0x8d>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1030 <memset@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,0x4(%rsp)
mov    %bx,(%rsp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1050 <connect@plt>
test   %rax,%rax
je     11ae <tcp_open+0x6e>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i16, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @tcp_open(i32 %0, i16 zeroext %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i16
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i32 %0, i32* %4
store i16 %1, i16* %5
%8 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%9 = load i32, i32* @PF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* %4
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%13 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %12, i32 0, i32 0
store i32 %11, i32* %13
%14 = load i16, i16* %5
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i16 %14, i16* %15
%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* %7
%19 = load i32, i32* %7
%20 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%21 = call i64 @connect(i32 %19, %struct.sockaddr* %20, i32 12)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = load i32, i32* %7
store i32 %24, i32* %3
br label %28
25: ; preds = %2
%26 = load i32, i32* %7
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %3
br label %28
28: ; preds = %25, %23
%29 = load i32, i32* %3
ret i32 %29
}

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