#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 */
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr const *, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int socket(int, int, int);
int connect_retry(int domain, int type, int protocol,
                  const struct sockaddr *addr, socklen_t alen) {
  int numsec, fd;
  for (numsec = 1; numsec <= 128; numsec <<= 1) {
    if ((fd = socket(domain, type, protocol)) < 0) {
      return -1;
    }
    if (connect(fd, addr, alen) == 0) {
      return fd;
    }
    close(fd);
    if (numsec <= 128 / 2) {
      sleep(numsec);
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<connect_retry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
cmpl   $0x80,-0x20(%rbp)
jg     11e4 <connect_retry+0xa4>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1060 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1193 <connect_retry+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <connect_retry+0xab>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <connect@plt>
cmp    $0x0,%rax
jne    11b7 <connect_retry+0x77>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11eb <connect_retry+0xab>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
cmpl   $0x40,-0x20(%rbp)
jg     11d1 <connect_retry+0x91>
mov    -0x20(%rbp),%edi
call   1050 <sleep@plt>
jmp    11d6 <connect_retry+0x96>
mov    -0x20(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1160 <connect_retry+0x20>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_retry>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,0xc(%rsp)
mov    %rcx,0x10(%rsp)
mov    %edx,%r12d
mov    %esi,%r13d
mov    %edi,%ebp
mov    $0x1,%ebx
mov    $0xffffffff,%r15d
jmp    117a <connect_retry+0x3a>
nopl   (%rax)
add    %ebx,%ebx
cmp    $0x81,%ebx
jge    11bc <connect_retry+0x7c>
mov    %ebp,%edi
mov    %r13d,%esi
mov    %r12d,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11bc <connect_retry+0x7c>
mov    %eax,%r14d
mov    %eax,%edi
mov    0x10(%rsp),%rsi
mov    0xc(%rsp),%edx
call   1040 <connect@plt>
test   %rax,%rax
je     11b9 <connect_retry+0x79>
mov    %r14d,%edi
call   1030 <close@plt>
cmp    $0x40,%ebx
jg     1170 <connect_retry+0x30>
mov    %ebx,%edi
call   1050 <sleep@plt>
jmp    1170 <connect_retry+0x30>
mov    %r14d,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
define i32 @connect_retry(i32 %0, i32 %1, i32 %2, %struct.sockaddr* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store %struct.sockaddr* %3, %struct.sockaddr** %10
store i32 %4, i32* %11
store i32 1, i32* %12
br label %14
14: ; preds = %41, %5
%15 = load i32, i32* %12
%16 = icmp sle i32 %15, 128
br i1 %16, label %17, label %44
17: ; preds = %14
%18 = load i32, i32* %7
%19 = load i32, i32* %8
%20 = load i32, i32* %9
%21 = call i32 @socket(i32 %18, i32 %19, i32 %20)
store i32 %21, i32* %13
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %17
store i32 -1, i32* %6
br label %45
24: ; preds = %17
%25 = load i32, i32* %13
%26 = load %struct.sockaddr*, %struct.sockaddr** %10
%27 = load i32, i32* %11
%28 = call i64 @connect(i32 %25, %struct.sockaddr* %26, i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = load i32, i32* %13
store i32 %31, i32* %6
br label %45
32: ; preds = %24
%33 = load i32, i32* %13
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* %12
%36 = icmp sle i32 %35, 64
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i32, i32* %12
%39 = call i32 @sleep(i32 %38)
br label %40
40: ; preds = %37, %32
br label %41
41: ; preds = %40
%42 = load i32, i32* %12
%43 = shl i32 %42, 1
store i32 %43, i32* %12
br label %14
44: ; preds = %14
store i32 -1, i32* %6
br label %45
45: ; preds = %44, %30, %23
%46 = load i32, i32* %6
ret i32 %46
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
define i32 @connect_retry(i32 %0, i32 %1, i32 %2, %struct.sockaddr* %3, i32 %4) {
br label %6
6: ; preds = %5, %18
%7 = phi i32 [ 1, %5 ], [ %19, %18 ]
%8 = tail call i32 @socket(i32 %0, i32 %1, i32 %2)
%9 = icmp slt i32 %8, 0
br i1 %9, label %21, label %10
10: ; preds = %6
%11 = tail call i64 @connect(i32 %8, %struct.sockaddr* %3, i32 %4)
%12 = icmp eq i64 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %10
%14 = tail call i32 @close(i32 %8)
%15 = icmp slt i32 %7, 65
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @sleep(i32 %7)
br label %18
18: ; preds = %13, %16
%19 = shl i32 %7, 1
%20 = icmp slt i32 %19, 129
br i1 %20, label %6, label %21
21: ; preds = %18, %10, %6
%22 = phi i32 [ -1, %6 ], [ %8, %10 ], [ -1, %18 ]
ret i32 %22
}