#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;
};

/* Variables and functions */
#define EALREADY 130
#define EINPROGRESS 129
#define EISCONN 128
int connect(int, struct sockaddr *, int);
int errno;
int sane_connect(int, struct sockaddr *, int);
int ztimed_read_write_wait(int, int, int *, int *);
int connect_and_wait_ok(int sock, struct sockaddr *sa, int len, int timeout) {
  int ret = sane_connect(sock, sa, len);
  if (ret < 0) {
    return ret;
  }
  int readable = 0, writeable = 0;
  ret = ztimed_read_write_wait(sock, timeout, &readable, &writeable);
  if (ret < 1) {
    return -1;
  }
  if (writeable == 0) {
    return -1;
  }
  if (readable == 0) {
    return 0;
  }
  ret = connect(sock, sa, len);
  if (ret == 0) {
    return -1;
  }
  switch (errno) {
  case EISCONN:
    return 0;
    break;
  case EALREADY:
    return -1;
    break;
  case EINPROGRESS:
    return -1;
    break;
  default:
    return -1;
    break;
  }
  return -1;
}


x86 assembly (clang, -O0):
<connect_and_wait_ok>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <sane_connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    116c <connect_and_wait_ok+0x3c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1267 <connect_and_wait_ok+0x137>
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%esi
lea    -0x20(%rbp),%rdx
lea    -0x24(%rbp),%rcx
call   1030 <ztimed_read_write_wait@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jge    11a6 <connect_and_wait_ok+0x76>
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <connect_and_wait_ok+0x137>
cmpl   $0x0,-0x24(%rbp)
jne    11bc <connect_and_wait_ok+0x8c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <connect_and_wait_ok+0x137>
cmpl   $0x0,-0x20(%rbp)
jne    11d2 <connect_and_wait_ok+0xa2>
movl   $0x0,-0x4(%rbp)
jmp    1267 <connect_and_wait_ok+0x137>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11fa <connect_and_wait_ok+0xca>
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <connect_and_wait_ok+0x137>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
sub    $0x80,%eax
je     123c <connect_and_wait_ok+0x10c>
jmp    1216 <connect_and_wait_ok+0xe6>
mov    -0x28(%rbp),%eax
sub    $0x81,%eax
je     1254 <connect_and_wait_ok+0x124>
jmp    1229 <connect_and_wait_ok+0xf9>
mov    -0x28(%rbp),%eax
sub    $0x82,%eax
je     1248 <connect_and_wait_ok+0x118>
jmp    1260 <connect_and_wait_ok+0x130>
movl   $0x0,-0x4(%rbp)
jmp    1267 <connect_and_wait_ok+0x137>
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <connect_and_wait_ok+0x137>
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <connect_and_wait_ok+0x137>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_and_wait_ok>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r12d
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebp
call   1050 <sane_connect@plt>
test   %eax,%eax
js     11bb <connect_and_wait_ok+0x8b>
movl   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %ebp,%edi
mov    %r12d,%esi
call   1030 <ztimed_read_write_wait@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
jle    11c1 <connect_and_wait_ok+0x91>
cmpl   $0x0,0x8(%rsp)
je     11c1 <connect_and_wait_ok+0x91>
cmpl   $0x0,0xc(%rsp)
je     11bf <connect_and_wait_ok+0x8f>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1040 <connect@plt>
test   %eax,%eax
je     11c1 <connect_and_wait_ok+0x91>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
cmp    $0x2,%eax
ja     11c1 <connect_and_wait_ok+0x91>
cltq
lea    0xe4a(%rip),%rcx
mov    (%rcx,%rax,4),%ebx
jmp    11c1 <connect_and_wait_ok+0x91>
mov    %eax,%ebx
jmp    11c1 <connect_and_wait_ok+0x91>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@errno = global i32 0
define i32 @connect_and_wait_ok(i32 %0, %struct.sockaddr* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store %struct.sockaddr* %1, %struct.sockaddr** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = load i32, i32* %6
%14 = load %struct.sockaddr*, %struct.sockaddr** %7
%15 = load i32, i32* %8
%16 = call i32 @sane_connect(i32 %13, %struct.sockaddr* %14, i32 %15)
store i32 %16, i32* %10
%17 = load i32, i32* %10
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %4
%20 = load i32, i32* %10
store i32 %20, i32* %5
br label %50
21: ; preds = %4
store i32 0, i32* %11
store i32 0, i32* %12
%22 = load i32, i32* %6
%23 = load i32, i32* %9
%24 = call i32 @ztimed_read_write_wait(i32 %22, i32 %23, i32* %11, i32* %12)
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = icmp slt i32 %25, 1
br i1 %26, label %27, label %28
27: ; preds = %21
store i32 -1, i32* %5
br label %50
28: ; preds = %21
%29 = load i32, i32* %12
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28
store i32 -1, i32* %5
br label %50
32: ; preds = %28
%33 = load i32, i32* %11
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %32
store i32 0, i32* %5
br label %50
36: ; preds = %32
%37 = load i32, i32* %6
%38 = load %struct.sockaddr*, %struct.sockaddr** %7
%39 = load i32, i32* %8
%40 = call i32 @connect(i32 %37, %struct.sockaddr* %38, i32 %39)
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %36
store i32 -1, i32* %5
br label %50
44: ; preds = %36
%45 = load i32, i32* @errno
switch i32 %45, label %49 [
i32 128, label %46
i32 130, label %47
i32 129, label %48
]
46: ; preds = %44
store i32 0, i32* %5
br label %50
47: ; preds = %44
store i32 -1, i32* %5
br label %50
48: ; preds = %44
store i32 -1, i32* %5
br label %50
49: ; preds = %44
store i32 -1, i32* %5
br label %50
50: ; preds = %49, %48, %47, %46, %43, %35, %31, %27, %19
%51 = load i32, i32* %5
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@errno = global i32 0
@switch.table.connect_and_wait_ok = [3 x i32] [i32 0, i32 -1, i32 -1]
define i32 @connect_and_wait_ok(i32 %0, %struct.sockaddr* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = tail call i32 @sane_connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%8 = icmp slt i32 %7, 0
br i1 %8, label %33, label %9
9: ; preds = %4
%10 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%11 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%12 = call i32 @ztimed_read_write_wait(i32 %0, i32 %3, i32* %5, i32* %6)
%13 = icmp slt i32 %12, 1
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, 0
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %31, label %17
17: ; preds = %9
%18 = load i32, i32* %5
%19 = icmp eq i32 %18, 0
br i1 %19, label %31, label %20
20: ; preds = %17
%21 = call i32 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%22 = icmp eq i32 %21, 0
br i1 %22, label %31, label %23
23: ; preds = %20
%24 = load i32, i32* @errno
%25 = add i32 %24, -128
%26 = icmp ult i32 %25, 3
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = sext i32 %25 to i64
%29 = getelementptr inbounds [3 x i32], [3 x i32]* @switch.table.connect_and_wait_ok, i64 0, i64 %28
%30 = load i32, i32* %29
br label %31
31: ; preds = %23, %27, %20, %17, %9
%32 = phi i32 [ -1, %9 ], [ 0, %17 ], [ -1, %20 ], [ %30, %27 ], [ -1, %23 ]
br label %33
33: ; preds = %4, %31
%34 = phi i32 [ %32, %31 ], [ %7, %4 ]
ret i32 %34
}