#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*/ printf(char *);
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 (connect(sockfd, addr, alen) == 0) {
      printf("connect ok\n");
      return 0;
    }
    printf("connect failed, retry...\n");
    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     11b8 <connect_retry+0x88>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <connect@plt>
cmp    $0x0,%rax
jne    1187 <connect_retry+0x57>
lea    0xe8a(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11bf <connect_retry+0x8f>
lea    0xe7e(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x40,-0x18(%rbp)
jg     11a5 <connect_retry+0x75>
mov    -0x18(%rbp),%edi
call   1050 <sleep@plt>
jmp    11aa <connect_retry+0x7a>
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1149 <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   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,%rbx
mov    %edi,%ebp
call   1040 <connect@plt>
xor    %r14d,%r14d
lea    0xec7(%rip),%r15
test   %rax,%rax
je     1295 <connect_retry+0x165>
lea    0xe9e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1040 <connect@plt>
test   %rax,%rax
je     1295 <connect_retry+0x165>
lea    0xe72(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1050 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1040 <connect@plt>
test   %rax,%rax
je     1295 <connect_retry+0x165>
lea    0xe46(%rip),%rdi
call   1030 <puts@plt>
mov    $0x4,%edi
call   1050 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1040 <connect@plt>
test   %rax,%rax
je     1295 <connect_retry+0x165>
lea    0xe1a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x8,%edi
call   1050 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1040 <connect@plt>
test   %rax,%rax
je     1295 <connect_retry+0x165>
lea    0xdee(%rip),%rdi
call   1030 <puts@plt>
mov    $0x10,%edi
call   1050 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1040 <connect@plt>
test   %rax,%rax
je     1295 <connect_retry+0x165>
lea    0xdc6(%rip),%r13
mov    %r13,%rdi
call   1030 <puts@plt>
mov    $0x20,%edi
call   1050 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1040 <connect@plt>
test   %rax,%rax
je     1295 <connect_retry+0x165>
mov    %r13,%rdi
call   1030 <puts@plt>
mov    $0x40,%edi
call   1050 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1040 <connect@plt>
xor    %r14d,%r14d
test   %rax,%rax
setne  %r14b
lea    0xd8b(%rip),%r15
cmovne %r13,%r15
neg    %r14d
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@.str = [12 x i8] c"connect ok\0A\00"
@.str.1 = [26 x i8] c"connect failed, retry...\0A\00"
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 = %28, %3
%10 = load i32, i32* %8
%11 = icmp sle i32 %10, 128
br i1 %11, label %12, label %31
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 %20
18: ; preds = %12
%19 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %32
20: ; preds = %12
%21 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* %8
%23 = icmp sle i32 %22, 64
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* %8
%26 = call i32 @sleep(i32 %25)
br label %27
27: ; preds = %24, %20
br label %28
28: ; preds = %27
%29 = load i32, i32* %8
%30 = shl i32 %29, 1
store i32 %30, i32* %8
br label %9
31: ; preds = %9
store i32 -1, i32* %4
br label %32
32: ; preds = %31, %18
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@str = [25 x i8] c"connect failed, retry...\00"
@str.2 = [11 x i8] c"connect ok\00"
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 %44, label %6
6: ; preds = %3
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @sleep(i32 1)
%9 = tail call i64 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%10 = icmp eq i64 %9, 0
br i1 %10, label %44, label %11
11: ; preds = %6
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 @sleep(i32 2)
%14 = tail call i64 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%15 = icmp eq i64 %14, 0
br i1 %15, label %44, label %16
16: ; preds = %11
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%18 = tail call i32 @sleep(i32 4)
%19 = tail call i64 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%20 = icmp eq i64 %19, 0
br i1 %20, label %44, label %21
21: ; preds = %16
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%23 = tail call i32 @sleep(i32 8)
%24 = tail call i64 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%25 = icmp eq i64 %24, 0
br i1 %25, label %44, label %26
26: ; preds = %21
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%28 = tail call i32 @sleep(i32 16)
%29 = tail call i64 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%30 = icmp eq i64 %29, 0
br i1 %30, label %44, label %31
31: ; preds = %26
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%33 = tail call i32 @sleep(i32 32)
%34 = tail call i64 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%35 = icmp eq i64 %34, 0
br i1 %35, label %44, label %36
36: ; preds = %31
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%38 = tail call i32 @sleep(i32 64)
%39 = tail call i64 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%40 = icmp eq i64 %39, 0
%41 = select i1 %40, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @str.2, i64 0, i64 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0)
%42 = xor i1 %40, true
%43 = sext i1 %42 to i32
br label %44
44: ; preds = %36, %3, %6, %11, %16, %21, %26, %31
%45 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @str.2, i64 0, i64 0), %31 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @str.2, i64 0, i64 0), %26 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @str.2, i64 0, i64 0), %21 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @str.2, i64 0, i64 0), %16 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @str.2, i64 0, i64 0), %11 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @str.2, i64 0, i64 0), %6 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @str.2, i64 0, i64 0), %3 ], [ %41, %36 ]
%46 = phi i32 [ 0, %31 ], [ 0, %26 ], [ 0, %21 ], [ 0, %16 ], [ 0, %11 ], [ 0, %6 ], [ 0, %3 ], [ %43, %36 ]
%47 = tail call i32 @puts(i8* dereferenceable(1) %45)
ret i32 %46
}