#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 */
typedef int /*<<< orphan*/ uint32_t;
typedef int /*<<< orphan*/ uint16_t;
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
scalar_t__ EINPROGRESS;
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ sockaddrnumfill(struct sockaddr_in *, int /*<<< orphan*/,
                                   int /*<<< orphan*/);
int tcpnumconnect(int sock, uint32_t ip, uint16_t port) {
  struct sockaddr_in sa;
  sockaddrnumfill(&sa, ip, port);
  if (connect(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) >= 0) {
    return 0;
  }
  if (errno == EINPROGRESS) {
    return 1;
  }
  return -1;
}


x86 assembly (clang, -O0):
<tcpnumconnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    -0x18(%rbp),%rdi
call   1030 <sockaddrnumfill@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1040 <connect@plt>
cmp    $0x0,%rax
jl     1167 <tcpnumconnect+0x47>
movl   $0x0,-0x4(%rbp)
jmp    1194 <tcpnumconnect+0x74>
mov    0x2e6a(%rip),%rax
mov    (%rax),%rax
mov    0x2e70(%rip),%rcx
cmp    (%rcx),%rax
jne    118d <tcpnumconnect+0x6d>
movl   $0x1,-0x4(%rbp)
jmp    1194 <tcpnumconnect+0x74>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpnumconnect>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
call   1030 <sockaddrnumfill@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <connect@plt>
mov    0x2e91(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e97(%rip),%rdx
xor    %esi,%esi
cmp    (%rdx),%rcx
sete   %sil
xor    %ecx,%ecx
test   %rax,%rax
lea    -0x1(%rsi,%rsi,1),%eax
cmovns %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINPROGRESS = global i64 0
define i32 @tcpnumconnect(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i32, i32* %6
%10 = load i32, i32* %7
%11 = call i32 @sockaddrnumfill(%struct.sockaddr_in* %8, i32 %9, i32 %10)
%12 = load i32, i32* %5
%13 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%14 = call i64 @connect(i32 %12, %struct.sockaddr* %13, i32 4)
%15 = icmp sge i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 0, i32* %4
br label %23
17: ; preds = %3
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINPROGRESS
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 1, i32* %4
br label %23
22: ; preds = %17
store i32 -1, i32* %4
br label %23
23: ; preds = %22, %21, %16
%24 = load i32, i32* %4
ret i32 %24
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINPROGRESS = global i64 0
define i32 @tcpnumconnect(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = call i32 @sockaddrnumfill(%struct.sockaddr_in* %4, i32 %1, i32 %2)
%7 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%8 = call i64 @connect(i32 %0, %struct.sockaddr* %7, i32 4)
%9 = icmp sgt i64 %8, -1
%10 = load i64, i64* @errno
%11 = load i64, i64* @EINPROGRESS
%12 = icmp eq i64 %10, %11
%13 = select i1 %12, i32 1, i32 -1
%14 = select i1 %9, i32 0, i32 %13
ret i32 %14
}