#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 *, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int socket(int, int, int);
int retry_connect(int af, int type, int proto, struct sockaddr *addr,
                  socklen_t slen) {
  int sfd, retry, err;
  if ((sfd = socket(af, type, proto)) < 0) {
    perror("cant init socket");
    exit(1);
  }
  for (retry = 0; retry < 4; ++retry) {
    if (0 != connect(sfd, addr, slen)) {
      err = errno;
      close(sfd);
      errno = err;
      sleep(1 << retry);
    } else
      return sfd;
  }
  return -1;
}


x86 assembly (clang, -O0):
<retry_connect>:
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)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1080 <socket@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11a9 <retry_connect+0x49>
lea    0xe66(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x4,-0x24(%rbp)
jge    1229 <retry_connect+0xc9>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <connect@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     120b <retry_connect+0xab>
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%ecx
mov    0x2deb(%rip),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%ecx
mov    $0x1,%edi
shl    %cl,%edi
call   1070 <sleep@plt>
jmp    1216 <retry_connect+0xb6>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1230 <retry_connect+0xd0>
jmp    121b <retry_connect+0xbb>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11b0 <retry_connect+0x50>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<retry_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8d,%r14d
mov    %rcx,%r12
call   1080 <socket@plt>
test   %eax,%eax
js     123c <retry_connect+0xdc>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r12,%rsi
mov    %r14d,%edx
call   1060 <connect@plt>
test   %rax,%rax
je     1231 <retry_connect+0xd1>
mov    0x2e46(%rip),%r15
mov    (%r15),%ebp
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,(%r15)
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %r14d,%edx
call   1060 <connect@plt>
test   %rax,%rax
je     1231 <retry_connect+0xd1>
mov    (%r15),%ebp
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,(%r15)
mov    $0x2,%edi
call   1070 <sleep@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %r14d,%edx
call   1060 <connect@plt>
test   %rax,%rax
je     1231 <retry_connect+0xd1>
mov    (%r15),%ebp
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,(%r15)
mov    $0x4,%edi
call   1070 <sleep@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %r14d,%edx
call   1060 <connect@plt>
test   %rax,%rax
je     1231 <retry_connect+0xd1>
mov    (%r15),%ebp
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,(%r15)
mov    $0x8,%edi
call   1070 <sleep@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdbd(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@.str = [17 x i8] c"cant init socket\00"
@errno = global i32 0
define i32 @retry_connect(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
%14 = 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
%15 = load i32, i32* %7
%16 = load i32, i32* %8
%17 = load i32, i32* %9
%18 = call i32 @socket(i32 %15, i32 %16, i32 %17)
store i32 %18, i32* %12
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %5
%21 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %5
store i32 0, i32* %13
br label %24
24: ; preds = %44, %23
%25 = load i32, i32* %13
%26 = icmp slt i32 %25, 4
br i1 %26, label %27, label %47
27: ; preds = %24
%28 = load i32, i32* %12
%29 = load %struct.sockaddr*, %struct.sockaddr** %10
%30 = load i32, i32* %11
%31 = call i64 @connect(i32 %28, %struct.sockaddr* %29, i32 %30)
%32 = icmp ne i64 0, %31
br i1 %32, label %33, label %41
33: ; preds = %27
%34 = load i32, i32* @errno
store i32 %34, i32* %14
%35 = load i32, i32* %12
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* %14
store i32 %37, i32* @errno
%38 = load i32, i32* %13
%39 = shl i32 1, %38
%40 = call i32 @sleep(i32 %39)
br label %43
41: ; preds = %27
%42 = load i32, i32* %12
store i32 %42, i32* %6
br label %48
43: ; preds = %33
br label %44
44: ; preds = %43
%45 = load i32, i32* %13
%46 = add nsw i32 %45, 1
store i32 %46, i32* %13
br label %24
47: ; preds = %24
store i32 -1, i32* %6
br label %48
48: ; preds = %47, %41
%49 = load i32, i32* %6
ret i32 %49
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [17 x i8] c"cant init socket\00"
@errno = global i32 0
define i32 @retry_connect(i32 %0, i32 %1, i32 %2, %struct.sockaddr* %3, i32 %4) {
%6 = tail call i32 @socket(i32 %0, i32 %1, i32 %2)
%7 = icmp slt i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %5
%9 = tail call i64 @connect(i32 %6, %struct.sockaddr* %3, i32 %4)
%10 = icmp eq i64 %9, 0
br i1 %10, label %36, label %14
11: ; preds = %5
%12 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8
%15 = load i32, i32* @errno
%16 = tail call i32 @close(i32 %6)
store i32 %15, i32* @errno
%17 = tail call i32 @sleep(i32 1)
%18 = tail call i64 @connect(i32 %6, %struct.sockaddr* %3, i32 %4)
%19 = icmp eq i64 %18, 0
br i1 %19, label %36, label %20
20: ; preds = %14
%21 = load i32, i32* @errno
%22 = tail call i32 @close(i32 %6)
store i32 %21, i32* @errno
%23 = tail call i32 @sleep(i32 2)
%24 = tail call i64 @connect(i32 %6, %struct.sockaddr* %3, i32 %4)
%25 = icmp eq i64 %24, 0
br i1 %25, label %36, label %26
26: ; preds = %20
%27 = load i32, i32* @errno
%28 = tail call i32 @close(i32 %6)
store i32 %27, i32* @errno
%29 = tail call i32 @sleep(i32 4)
%30 = tail call i64 @connect(i32 %6, %struct.sockaddr* %3, i32 %4)
%31 = icmp eq i64 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %26
%33 = load i32, i32* @errno
%34 = tail call i32 @close(i32 %6)
store i32 %33, i32* @errno
%35 = tail call i32 @sleep(i32 8)
br label %36
36: ; preds = %32, %26, %20, %14, %8
%37 = phi i32 [ %6, %8 ], [ %6, %14 ], [ %6, %20 ], [ %6, %26 ], [ -1, %32 ]
ret i32 %37
}