#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 */
typedef int /*<<< orphan*/ val;
typedef int uint32_t;
struct TYPE_2__ {
  int s_addr;
};
struct sockaddr_in {
  TYPE_1__ 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;
scalar_t__ EINPROGRESS;
scalar_t__ EINTR;
int SOCK_CLOEXEC;
int SOCK_NONBLOCK;
int SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int sam3aConnect(uint32_t ip, int port, int *complete) {
  int fd, val = 1;
  if (complete != NULL)
    *complete = 0;
  if (ip == 0 || ip == 0xffffffffUL || port < 1 || port > 65535)
    return -1;
  if ((fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0)) < 0)
    return -1;
  setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
  for (;;) {
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = ip;
    if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
      if (errno == EINPROGRESS)
        break;
      if (errno != EINTR) {
        close(fd);
        return -1;
      }
    } else {
      if (complete != NULL)
        *complete = 1;
      break;
    }
  }
  return fd;
}


x86 assembly (clang, -O0):
<sam3aConnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     118e <sam3aConnect+0x2e>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
cmpl   $0x0,-0x8(%rbp)
je     11c1 <sam3aConnect+0x61>
movslq -0x8(%rbp),%rax
mov    $0xffffffff,%ecx
cmp    %rcx,%rax
je     11c1 <sam3aConnect+0x61>
cmpl   $0x1,-0xc(%rbp)
jl     11c1 <sam3aConnect+0x61>
cmpl   $0xffff,-0xc(%rbp)
jle    11cd <sam3aConnect+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <sam3aConnect+0x193>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
mov    0x2dda(%rip),%rax
or     (%rax),%esi
mov    0x2e01(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1210 <sam3aConnect+0xb0>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <sam3aConnect+0x193>
mov    -0x1c(%rbp),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    0x2d75(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    12ce <sam3aConnect+0x16e>
mov    0x2d2d(%rip),%rax
mov    (%rax),%rax
mov    0x2d43(%rip),%rcx
cmp    (%rcx),%rax
jne    129b <sam3aConnect+0x13b>
jmp    12ed <sam3aConnect+0x18d>
mov    0x2d0e(%rip),%rax
mov    (%rax),%rax
mov    0x2cfc(%rip),%rcx
cmp    (%rcx),%rax
je     12c9 <sam3aConnect+0x169>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <sam3aConnect+0x193>
jmp    12e8 <sam3aConnect+0x188>
cmpq   $0x0,-0x18(%rbp)
je     12e3 <sam3aConnect+0x183>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
jmp    12ed <sam3aConnect+0x18d>
jmp    1234 <sam3aConnect+0xd4>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sam3aConnect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%ebp
movl   $0x1,0x14(%rsp)
test   %rdx,%rdx
je     118a <sam3aConnect+0x2a>
movl   $0x0,(%r14)
test   %ebp,%ebp
je     12c5 <sam3aConnect+0x165>
lea    -0x10000(%r15),%eax
cmp    $0xffff0001,%eax
jb     12c5 <sam3aConnect+0x165>
mov    0x2e2d(%rip),%r13
mov    0x2e06(%rip),%rax
mov    0x2e07(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x0(%r13),%edi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     12c5 <sam3aConnect+0x165>
mov    %eax,%ebx
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    0x2dad(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
mov    %ebx,%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    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %ebp,0x8(%rsp)
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
test   %rax,%rax
js     1251 <sam3aConnect+0xf1>
test   %r14,%r14
je     12ca <sam3aConnect+0x16a>
movl   $0x1,(%r14)
jmp    12ca <sam3aConnect+0x16a>
mov    0x2d78(%rip),%r13
lea    0x8(%rsp),%r12
nopl   (%rax)
mov    0x2d49(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
je     12ca <sam3aConnect+0x16a>
mov    0x2d31(%rip),%rcx
cmp    (%rcx),%rax
jne    12be <sam3aConnect+0x15e>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2d46(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %ebp,0x8(%rsp)
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
test   %rax,%rax
js     1260 <sam3aConnect+0x100>
jmp    123f <sam3aConnect+0xdf>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%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 { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOCK_NONBLOCK = global i32 0
@SOCK_CLOEXEC = global i32 0
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@EINTR = global i64 0
define i32 @sam3aConnect(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32 1, i32* %9
%11 = load i32*, i32** %7
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = load i32*, i32** %7
store i32 0, i32* %14
br label %15
15: ; preds = %13, %3
%16 = load i32, i32* %5
%17 = icmp eq i32 %16, 0
br i1 %17, label %28, label %18
18: ; preds = %15
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = icmp eq i64 %20, 4294967295
br i1 %21, label %28, label %22
22: ; preds = %18
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 1
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = load i32, i32* %6
%27 = icmp sgt i32 %26, 65535
br i1 %27, label %28, label %29
28: ; preds = %25, %22, %18, %15
store i32 -1, i32* %4
br label %80
29: ; preds = %25
%30 = load i32, i32* @AF_INET
%31 = load i32, i32* @SOCK_STREAM
%32 = load i32, i32* @SOCK_NONBLOCK
%33 = or i32 %31, %32
%34 = load i32, i32* @SOCK_CLOEXEC
%35 = or i32 %33, %34
%36 = call i32 @socket(i32 %30, i32 %35, i32 0)
store i32 %36, i32* %8
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %29
store i32 -1, i32* %4
br label %80
39: ; preds = %29
%40 = load i32, i32* %8
%41 = load i32, i32* @SOL_SOCKET
%42 = load i32, i32* @SO_KEEPALIVE
%43 = call i32 @setsockopt(i32 %40, i32 %41, i32 %42, i32* %9, i32 4)
br label %44
44: ; preds = %77, %39
%45 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%46 = load i32, i32* @AF_INET
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %46, i32* %47
%48 = load i32, i32* %6
%49 = call i32 @htons(i32 %48)
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %49, i32* %50
%51 = load i32, i32* %5
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%53 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %52, i32 0, i32 0
store i32 %51, i32* %53
%54 = load i32, i32* %8
%55 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%56 = call i64 @connect(i32 %54, %struct.sockaddr* %55, i32 12)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %71
58: ; preds = %44
%59 = load i64, i64* @errno
%60 = load i64, i64* @EINPROGRESS
%61 = icmp eq i64 %59, %60
br i1 %61, label %62, label %63
62: ; preds = %58
br label %78
63: ; preds = %58
%64 = load i64, i64* @errno
%65 = load i64, i64* @EINTR
%66 = icmp ne i64 %64, %65
br i1 %66, label %67, label %70
67: ; preds = %63
%68 = load i32, i32* %8
%69 = call i32 @close(i32 %68)
store i32 -1, i32* %4
br label %80
70: ; preds = %63
br label %77
71: ; preds = %44
%72 = load i32*, i32** %7
%73 = icmp ne i32* %72, null
br i1 %73, label %74, label %76
74: ; preds = %71
%75 = load i32*, i32** %7
store i32 1, i32* %75
br label %76
76: ; preds = %74, %71
br label %78
77: ; preds = %70
br label %44
78: ; preds = %76, %62
%79 = load i32, i32* %8
store i32 %79, i32* %4
br label %80
80: ; preds = %78, %67, %38, %28
%81 = load i32, i32* %4
ret i32 %81
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOCK_NONBLOCK = global i32 0
@SOCK_CLOEXEC = global i32 0
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@EINTR = global i64 0
define i32 @sam3aConnect(i32 %0, i32 %1, i32* writeonly %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%7 = icmp eq i32* %2, null
br i1 %7, label %9, label %8
8: ; preds = %3
store i32 0, i32* %2
br label %9
9: ; preds = %8, %3
%10 = icmp eq i32 %0, 0
%11 = add i32 %1, -65536
%12 = icmp ult i32 %11, -65535
%13 = or i1 %10, %12
br i1 %13, label %55, label %14
14: ; preds = %9
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = load i32, i32* @SOCK_NONBLOCK
%18 = or i32 %17, %16
%19 = load i32, i32* @SOCK_CLOEXEC
%20 = or i32 %18, %19
%21 = tail call i32 @socket(i32 %15, i32 %20, i32 0)
%22 = icmp slt i32 %21, 0
br i1 %22, label %55, label %23
23: ; preds = %14
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_KEEPALIVE
%26 = call i32 @setsockopt(i32 %21, i32 %24, i32 %25, i32* %4, i32 4)
%27 = bitcast %struct.sockaddr_in* %5 to i8*
%28 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %29, i32* %30
%31 = call i32 @htons(i32 %1)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i32 %0, i32* %33
%34 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%35 = call i64 @connect(i32 %21, %struct.sockaddr* %34, i32 12)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %46
37: ; preds = %23, %48
%38 = load i64, i64* @errno
%39 = load i64, i64* @EINPROGRESS
%40 = icmp eq i64 %38, %39
br i1 %40, label %54, label %41
41: ; preds = %37
%42 = load i64, i64* @EINTR
%43 = icmp eq i64 %38, %42
br i1 %43, label %48, label %44
44: ; preds = %41
%45 = call i32 @close(i32 %21)
br label %55
46: ; preds = %48, %23
br i1 %7, label %54, label %47
47: ; preds = %46
store i32 1, i32* %2
br label %54
48: ; preds = %41
%49 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%50 = load i32, i32* @AF_INET
store i32 %50, i32* %30
%51 = call i32 @htons(i32 %1)
store i32 %51, i32* %32
store i32 %0, i32* %33
%52 = call i64 @connect(i32 %21, %struct.sockaddr* %34, i32 12)
%53 = icmp slt i64 %52, 0
br i1 %53, label %37, label %46
54: ; preds = %37, %47, %46
br label %55
55: ; preds = %44, %14, %9, %54
%56 = phi i32 [ %21, %54 ], [ -1, %9 ], [ -1, %14 ], [ -1, %44 ]
ret i32 %56
}