#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 const *, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int connect_retry(int sockfd, const struct sockaddr *addr, socklen_t alen) {
  int nsec;
  for (nsec = 1; nsec <= 10; nsec <<= 1) {
    if (connect(sockfd, addr, alen) == 0) {
      return 0;
    }
    if (nsec <= 10 / 2)
      sleep(nsec);
  }
  return -1;
}


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   $0xa,-0x18(%rbp)
jg     118d <connect_retry+0x6d>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <connect@plt>
cmp    $0x0,%rax
jne    1168 <connect_retry+0x48>
movl   $0x0,-0x4(%rbp)
jmp    1194 <connect_retry+0x74>
cmpl   $0x5,-0x18(%rbp)
jg     117a <connect_retry+0x5a>
mov    -0x18(%rbp),%edi
call   1040 <sleep@plt>
jmp    117f <connect_retry+0x5f>
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1139 <connect_retry+0x19>
movl   $0xffffffff,-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,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
call   1030 <connect@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     1191 <connect_retry+0x71>
mov    $0x1,%edi
call   1040 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <connect@plt>
test   %rax,%rax
je     1191 <connect_retry+0x71>
mov    $0x2,%edi
call   1040 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <connect@plt>
test   %rax,%rax
je     1191 <connect_retry+0x71>
mov    $0x4,%edi
call   1040 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <connect@plt>
neg    %rax
sbb    %r15d,%r15d
mov    %r15d,%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 sle i32 %10, 10
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 %30
19: ; preds = %12
%20 = load i32, i32* %8
%21 = icmp sle i32 %20, 5
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
store i32 -1, i32* %4
br label %30
30: ; preds = %29, %18
%31 = load i32, i32* %4
ret i32 %31
}

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 %19, 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 %19, 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 %19, 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 ne i64 %16, 0
%18 = sext i1 %17 to i32
br label %19
19: ; preds = %14, %10, %6, %3
%20 = phi i32 [ 0, %3 ], [ 0, %6 ], [ 0, %10 ], [ %18, %14 ]
ret i32 %20
}