#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 */
scalar_t__ connect(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int connect_retry(int fd, struct sockaddr *paddr, socklen_t len) {
  int nsec;
  for (nsec = 1; nsec < 128; nsec <<= 1) {
    if (connect(fd, paddr, len) == 0)
      return 0;
    if (nsec < 128 / 2)
      sleep(nsec);
  }
  if (nsec >= 128)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<connect_retry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
cmpl   $0x80,-0x18(%rbp)
jge    1190 <connect_retry+0x70>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <connect@plt>
cmp    $0x0,%rax
jne    116b <connect_retry+0x4b>
movl   $0x0,-0x4(%rbp)
jmp    11b0 <connect_retry+0x90>
cmpl   $0x40,-0x18(%rbp)
jge    117d <connect_retry+0x5d>
mov    -0x18(%rbp),%edi
call   1040 <sleep@plt>
jmp    1182 <connect_retry+0x62>
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1139 <connect_retry+0x19>
cmpl   $0x80,-0x18(%rbp)
jl     11a9 <connect_retry+0x89>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b0 <connect_retry+0x90>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_retry>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %edi,%ebx
call   1030 <connect@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     11ed <connect_retry+0xcd>
mov    $0x1,%edi
call   1040 <sleep@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1030 <connect@plt>
test   %rax,%rax
je     11ed <connect_retry+0xcd>
mov    $0x2,%edi
call   1040 <sleep@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1030 <connect@plt>
test   %rax,%rax
je     11ed <connect_retry+0xcd>
mov    $0x4,%edi
call   1040 <sleep@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1030 <connect@plt>
test   %rax,%rax
je     11ed <connect_retry+0xcd>
mov    $0x8,%edi
call   1040 <sleep@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1030 <connect@plt>
test   %rax,%rax
je     11ed <connect_retry+0xcd>
mov    $0x10,%edi
call   1040 <sleep@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1030 <connect@plt>
test   %rax,%rax
je     11ed <connect_retry+0xcd>
mov    $0x20,%edi
call   1040 <sleep@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1030 <connect@plt>
neg    %rax
sbb    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
define i32 @connect_retry(i32 %0, %struct.sockaddr* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store %struct.sockaddr* %1, %struct.sockaddr** %6
store i32 %2, i32* %7
store i32 1, i32* %8
br label %9
9: ; preds = %26, %3
%10 = load i32, i32* %8
%11 = icmp slt i32 %10, 128
br i1 %11, label %12, label %29
12: ; preds = %9
%13 = load i32, i32* %5
%14 = load %struct.sockaddr*, %struct.sockaddr** %6
%15 = load i32, i32* %7
%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* %4
br label %34
19: ; preds = %12
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 64
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* %8
%24 = call i32 @sleep(i32 %23)
br label %25
25: ; preds = %22, %19
br label %26
26: ; preds = %25
%27 = load i32, i32* %8
%28 = shl i32 %27, 1
store i32 %28, i32* %8
br label %9
29: ; preds = %9
%30 = load i32, i32* %8
%31 = icmp sge i32 %30, 128
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 -1, i32* %4
br label %34
33: ; preds = %29
store i32 0, i32* %4
br label %34
34: ; preds = %33, %32, %18
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
define i32 @connect_retry(i32 %0, %struct.sockaddr* %1, i32 %2) {
%4 = tail call i64 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %31, label %6
6: ; preds = %3
%7 = tail call i32 @sleep(i32 1)
%8 = tail call i64 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %31, label %10
10: ; preds = %6
%11 = tail call i32 @sleep(i32 2)
%12 = tail call i64 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%13 = icmp eq i64 %12, 0
br i1 %13, label %31, label %14
14: ; preds = %10
%15 = tail call i32 @sleep(i32 4)
%16 = tail call i64 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%17 = icmp eq i64 %16, 0
br i1 %17, label %31, label %18
18: ; preds = %14
%19 = tail call i32 @sleep(i32 8)
%20 = tail call i64 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%21 = icmp eq i64 %20, 0
br i1 %21, label %31, label %22
22: ; preds = %18
%23 = tail call i32 @sleep(i32 16)
%24 = tail call i64 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%25 = icmp eq i64 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %22
%27 = tail call i32 @sleep(i32 32)
%28 = tail call i64 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%29 = icmp ne i64 %28, 0
%30 = sext i1 %29 to i32
br label %31
31: ; preds = %26, %22, %18, %14, %10, %6, %3
%32 = phi i32 [ 0, %3 ], [ 0, %6 ], [ 0, %10 ], [ 0, %14 ], [ 0, %18 ], [ 0, %22 ], [ %30, %26 ]
ret i32 %32
}