#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 <= 128; nsec <<= 1) {
    if (0 == connect(sockfd, addr, alen)) {
      return 0;
    }
    if (nsec <= 128 / 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   $0x80,-0x18(%rbp)
jg     1194 <connect_retry+0x74>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <connect@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    116f <connect_retry+0x4f>
movl   $0x0,-0x4(%rbp)
jmp    119b <connect_retry+0x7b>
cmpl   $0x40,-0x18(%rbp)
jg     1181 <connect_retry+0x61>
mov    -0x18(%rbp),%edi
call   1040 <sleep@plt>
jmp    1186 <connect_retry+0x66>
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,%r15d
mov    %rsi,%rbx
mov    %edi,%ebp
call   1030 <connect@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     120d <connect_retry+0xed>
mov    $0x1,%edi
call   1040 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <connect@plt>
test   %rax,%rax
je     120d <connect_retry+0xed>
mov    $0x2,%edi
call   1040 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <connect@plt>
test   %rax,%rax
je     120d <connect_retry+0xed>
mov    $0x4,%edi
call   1040 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <connect@plt>
test   %rax,%rax
je     120d <connect_retry+0xed>
mov    $0x8,%edi
call   1040 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <connect@plt>
test   %rax,%rax
je     120d <connect_retry+0xed>
mov    $0x10,%edi
call   1040 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <connect@plt>
test   %rax,%rax
je     120d <connect_retry+0xed>
mov    $0x20,%edi
call   1040 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <connect@plt>
test   %rax,%rax
je     120d <connect_retry+0xed>
mov    $0x40,%edi
call   1040 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%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 sle 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 0, %16
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, 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
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 %35, 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 %35, 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 %35, 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 %35, 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 %35, 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 %35, 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 eq i64 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %26
%31 = tail call i32 @sleep(i32 64)
%32 = tail call i64 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%33 = icmp ne i64 %32, 0
%34 = sext i1 %33 to i32
br label %35
35: ; preds = %30, %26, %22, %18, %14, %10, %6, %3
%36 = phi i32 [ 0, %3 ], [ 0, %6 ], [ 0, %10 ], [ 0, %14 ], [ 0, %18 ], [ 0, %22 ], [ 0, %26 ], [ %34, %30 ]
ret i32 %36
}