#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 {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int EINPROGRESS;
int connect(int, struct sockaddr const *, int /*<<< orphan*/);
int errno;
int sockem_do_connect(int s, const struct sockaddr *addr, socklen_t addrlen) {
  int r;
  r = connect(s, addr, addrlen);
  if (r == -1) {
    int serr = errno;
    if (serr != EINPROGRESS) {
      errno = serr;
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<sockem_do_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <connect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1179 <sockem_do_connect+0x69>
mov    0x2e93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e94(%rip),%rcx
cmp    (%rcx),%eax
je     1174 <sockem_do_connect+0x64>
mov    -0x1c(%rbp),%ecx
mov    0x2e72(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1180 <sockem_do_connect+0x70>
jmp    1179 <sockem_do_connect+0x69>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sockem_do_connect>:
push   %rax
call   1030 <connect@plt>
cmp    $0xffffffff,%eax
jne    1134 <sockem_do_connect+0x24>
mov    0x2eb6(%rip),%rax
mov    (%rax),%ecx
mov    0x2ebd(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%ecx
jne    1136 <sockem_do_connect+0x26>
xor    %eax,%eax
pop    %rcx
ret

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

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@errno = global i32 0
@EINPROGRESS = global i32 0
define i32 @sockem_do_connect(i32 %0, %struct.sockaddr* %1, i32 %2) {
%4 = tail call i32 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = load i32, i32* @EINPROGRESS
%9 = icmp eq i32 %7, %8
br i1 %9, label %10, label %11
10: ; preds = %6, %3
br label %11
11: ; preds = %6, %10
%12 = phi i32 [ 0, %10 ], [ -1, %6 ]
ret i32 %12
}