#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_port;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
scalar_t__ EISCONN;
int /*<<< orphan*/ assert(int);
int connect(int, struct sockaddr *, int);
scalar_t__ errno;
scalar_t__ ignoreErrno(scalar_t__);
scalar_t__ ntohs(int /*<<< orphan*/);
int comm_connect_addr(int sock, const struct sockaddr_in *address) {
  int status = (0);
  int x;
  assert(ntohs(address->sin_port) != 0);
  errno = 0;
  x = connect(sock, (struct sockaddr *)address, sizeof(*address));
  if (errno == 0 || errno == EISCONN)
    status = (0);
  else if (ignoreErrno(errno))
    status = (-6);
  else
    return (-1);
  return status;
}


x86 assembly (clang, -O0):
<comm_connect_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    0x2e5f(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e3d(%rip),%rax
cmpq   $0x0,(%rax)
je     11bf <comm_connect_addr+0x7f>
mov    0x2e2c(%rip),%rax
mov    (%rax),%rax
mov    0x2e32(%rip),%rcx
cmp    (%rcx),%rax
jne    11cb <comm_connect_addr+0x8b>
movl   $0x0,-0x14(%rbp)
jmp    1201 <comm_connect_addr+0xc1>
mov    0x2e06(%rip),%rax
mov    (%rax),%rdi
call   1050 <ignoreErrno@plt>
cmp    $0x0,%rax
je     11f0 <comm_connect_addr+0xb0>
movl   $0xfffffffa,-0x14(%rbp)
jmp    11fc <comm_connect_addr+0xbc>
movl   $0xffffffff,-0x4(%rbp)
jmp    1207 <comm_connect_addr+0xc7>
jmp    1201 <comm_connect_addr+0xc1>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<comm_connect_addr>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    (%rsi),%edi
call   1040 <ntohs@plt>
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1030 <assert@plt>
mov    0x2e73(%rip),%r14
movq   $0x0,(%r14)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
mov    (%r14),%rdi
xor    %eax,%eax
test   %rdi,%rdi
je     11a8 <comm_connect_addr+0x68>
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%rdi
je     11a8 <comm_connect_addr+0x68>
call   1050 <ignoreErrno@plt>
mov    %rax,%rcx
mov    $0xfffffffa,%eax
test   %rcx,%rcx
jne    11a8 <comm_connect_addr+0x68>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EISCONN = global i64 0
define i32 @comm_connect_addr(i32 %0, %struct.sockaddr_in* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %5
store i32 0, i32* %6
%8 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%10 = load i32, i32* %9
%11 = call i64 @ntohs(i32 %10)
%12 = icmp ne i64 %11, 0
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
store i64 0, i64* @errno
%15 = load i32, i32* %4
%16 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%17 = bitcast %struct.sockaddr_in* %16 to %struct.sockaddr*
%18 = call i32 @connect(i32 %15, %struct.sockaddr* %17, i32 4)
store i32 %18, i32* %7
%19 = load i64, i64* @errno
%20 = icmp eq i64 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %2
%22 = load i64, i64* @errno
%23 = load i64, i64* @EISCONN
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21, %2
store i32 0, i32* %6
br label %33
26: ; preds = %21
%27 = load i64, i64* @errno
%28 = call i64 @ignoreErrno(i64 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 -6, i32* %6
br label %32
31: ; preds = %26
store i32 -1, i32* %3
br label %35
32: ; preds = %30
br label %33
33: ; preds = %32, %25
%34 = load i32, i32* %6
store i32 %34, i32* %3
br label %35
35: ; preds = %33, %31
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EISCONN = global i64 0
define i32 @comm_connect_addr(i32 %0, %struct.sockaddr_in* %1) {
%3 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%4 = load i32, i32* %3
%5 = tail call i64 @ntohs(i32 %4)
%6 = icmp ne i64 %5, 0
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
store i64 0, i64* @errno
%9 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%10 = tail call i32 @connect(i32 %0, %struct.sockaddr* %9, i32 4)
%11 = load i64, i64* @errno
%12 = icmp eq i64 %11, 0
%13 = load i64, i64* @EISCONN
%14 = icmp eq i64 %11, %13
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = tail call i64 @ignoreErrno(i64 %11)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16, %2
%20 = phi i32 [ 0, %2 ], [ -6, %16 ]
br label %21
21: ; preds = %16, %19
%22 = phi i32 [ %20, %19 ], [ -1, %16 ]
ret i32 %22
}