#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 */
#define ECONNREFUSED 130
#define ENOENT 129
#define EPERM 128
scalar_t__ connect(int, struct sockaddr const *, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ sleep(int);
int connect_wait(int sock_fd, const struct sockaddr *addr, socklen_t addrlen,
                 unsigned int timeout) {
  unsigned int remain = timeout;
  for (;;) {
    if (connect(sock_fd, addr, addrlen) == 0)
      return 0;
    switch (errno) {
    case ENOENT:
    case EPERM:
    case ECONNREFUSED:
      if (remain == 0)
        return -1;
      sleep(1);
      remain--;
      break;
    default:
      return -1;
    }
  }
}


x86 assembly (clang, -O0):
<connect_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <connect@plt>
cmp    $0x0,%rax
jne    1160 <connect_wait+0x40>
movl   $0x0,-0x4(%rbp)
jmp    11b9 <connect_wait+0x99>
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
sub    $0x2,%eax
ja     11a8 <connect_wait+0x88>
jmp    117a <connect_wait+0x5a>
cmpl   $0x0,-0x1c(%rbp)
jne    1190 <connect_wait+0x70>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <connect_wait+0x99>
mov    $0x1,%edi
call   1040 <sleep@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b4 <connect_wait+0x94>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <connect_wait+0x99>
jmp    113b <connect_wait+0x1b>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_wait>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebp
call   1030 <connect@plt>
test   %rax,%rax
je     1187 <connect_wait+0x67>
mov    0x2e9a(%rip),%r12
mov    $0xffffff7d,%r13d
nopl   0x0(%rax)
mov    (%r12),%edx
add    %r13d,%edx
sub    $0x1,%ebx
setb   %cl
mov    $0xffffffff,%eax
cmp    $0xfffffffd,%edx
jb     1189 <connect_wait+0x69>
test   %cl,%cl
jne    1189 <connect_wait+0x69>
mov    $0x1,%edi
call   1040 <sleep@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <connect@plt>
test   %rax,%rax
jne    1150 <connect_wait+0x30>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@errno = global i32 0
define i32 @connect_wait(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
store i32 %0, i32* %6
store %struct.sockaddr* %1, %struct.sockaddr** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%11 = load i32, i32* %9
store i32 %11, i32* %10
br label %12
12: ; preds = %30, %4
%13 = load i32, i32* %6
%14 = load %struct.sockaddr*, %struct.sockaddr** %7
%15 = load i32, i32* %8
%16 = call i64 @connect(i32 %13, %struct.sockaddr* %14, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %12
store i32 0, i32* %5
br label %31
19: ; preds = %12
%20 = load i32, i32* @errno
switch i32 %20, label %29 [
i32 129, label %21
i32 128, label %21
i32 130, label %21
]
21: ; preds = %19, %19, %19
%22 = load i32, i32* %10
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21
store i32 -1, i32* %5
br label %31
25: ; preds = %21
%26 = call i32 @sleep(i32 1)
%27 = load i32, i32* %10
%28 = add i32 %27, -1
store i32 %28, i32* %10
br label %30
29: ; preds = %19
store i32 -1, i32* %5
br label %31
30: ; preds = %25
br label %12
31: ; preds = %29, %24, %18
%32 = load i32, i32* %5
ret i32 %32
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@errno = global i32 0
define i32 @connect_wait(i32 %0, %struct.sockaddr* %1, i32 %2, i32 %3) {
%5 = tail call i64 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %19, label %7
7: ; preds = %4, %14
%8 = phi i32 [ %16, %14 ], [ %3, %4 ]
%9 = load i32, i32* @errno
%10 = add i32 %9, -131
%11 = icmp ult i32 %10, -3
%12 = icmp eq i32 %8, 0
%13 = select i1 %11, i1 true, i1 %12
br i1 %13, label %19, label %14
14: ; preds = %7
%15 = tail call i32 @sleep(i32 1)
%16 = add i32 %8, -1
%17 = tail call i64 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %7
19: ; preds = %14, %7, %4
%20 = phi i32 [ 0, %4 ], [ -1, %7 ], [ 0, %14 ]
ret i32 %20
}