#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_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ E2BIG;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(char *);
int openConnection(char *path) {
  int i = 0, conn_fd;
  struct sockaddr_un sa;
  if (path == NULL || strlen(path) == 0)
    return -1;
  if (strlen(path) > 108) {
    errno = E2BIG;
    return -1;
  }
  sa.sun_family = AF_UNIX;
  strcpy(sa.sun_path, path);
  if ((conn_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
    return -1;
  while (i < 3) {
    if (connect(conn_fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
      sleep(1);
      i++;
    } else
      break;
  }
  if (i == 3)
    return -1;
  else
    return conn_fd;
}


x86 assembly (clang, -O0):
<openConnection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1180 <openConnection+0x30>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
jne    118c <openConnection+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <openConnection+0x11c>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x6c,%eax
jle    11bc <openConnection+0x6c>
mov    0x2e43(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1a(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <openConnection+0x11c>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1205 <openConnection+0xb5>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <openConnection+0x11c>
jmp    120a <openConnection+0xba>
cmpl   $0x3,-0x14(%rbp)
jge    1250 <openConnection+0x100>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    1246 <openConnection+0xf6>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    124b <openConnection+0xfb>
jmp    1250 <openConnection+0x100>
jmp    120a <openConnection+0xba>
cmpl   $0x3,-0x14(%rbp)
jne    1266 <openConnection+0x116>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <openConnection+0x11c>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openConnection>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     121b <openConnection+0xcb>
mov    %rdi,%rbx
call   1040 <strlen@plt>
test   %eax,%eax
je     121b <openConnection+0xcb>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    $0x6d,%eax
jl     1191 <openConnection+0x41>
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    0x2e3e(%rip),%rcx
mov    %eax,(%rcx)
jmp    121b <openConnection+0xcb>
mov    0x2e48(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    (%r14),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     121b <openConnection+0xcb>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    1220 <openConnection+0xd0>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    1220 <openConnection+0xd0>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    1220 <openConnection+0xd0>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@E2BIG = global i32 0
@errno = global i32 0
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
define i32 @openConnection(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
store i8* %0, i8** %3
store i32 0, i32* %4
%7 = load i8*, i8** %3
%8 = icmp eq i8* %7, null
br i1 %8, label %13, label %9
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %9, %1
store i32 -1, i32* %2
br label %53
14: ; preds = %9
%15 = load i8*, i8** %3
%16 = call i32 @strlen(i8* %15)
%17 = icmp sgt i32 %16, 108
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = load i32, i32* @E2BIG
store i32 %19, i32* @errno
store i32 -1, i32* %2
br label %53
20: ; preds = %14
%21 = load i32, i32* @AF_UNIX
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i8*, i8** %3
%26 = call i32 @strcpy(i32 %24, i8* %25)
%27 = load i32, i32* @AF_UNIX
%28 = load i32, i32* @SOCK_STREAM
%29 = call i32 @socket(i32 %27, i32 %28, i32 0)
store i32 %29, i32* %5
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %20
store i32 -1, i32* %2
br label %53
32: ; preds = %20
br label %33
33: ; preds = %46, %32
%34 = load i32, i32* %4
%35 = icmp slt i32 %34, 3
br i1 %35, label %36, label %47
36: ; preds = %33
%37 = load i32, i32* %5
%38 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%39 = call i32 @connect(i32 %37, %struct.sockaddr* %38, i32 8)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = call i32 @sleep(i32 1)
%43 = load i32, i32* %4
%44 = add nsw i32 %43, 1
store i32 %44, i32* %4
br label %46
45: ; preds = %36
br label %47
46: ; preds = %41
br label %33
47: ; preds = %45, %33
%48 = load i32, i32* %4
%49 = icmp eq i32 %48, 3
br i1 %49, label %50, label %51
50: ; preds = %47
store i32 -1, i32* %2
br label %53
51: ; preds = %47
%52 = load i32, i32* %5
store i32 %52, i32* %2
br label %53
53: ; preds = %51, %50, %31, %18, %13
%54 = load i32, i32* %2
ret i32 %54
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@E2BIG = global i32 0
@errno = global i32 0
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
define i32 @openConnection(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = icmp eq i8* %0, null
br i1 %4, label %35, label %5
5: ; preds = %1
%6 = tail call i32 @strlen(i8* %0)
%7 = icmp eq i32 %6, 0
br i1 %7, label %35, label %8
8: ; preds = %5
%9 = tail call i32 @strlen(i8* %0)
%10 = icmp sgt i32 %9, 108
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = load i32, i32* @E2BIG
store i32 %12, i32* @errno
br label %35
13: ; preds = %8
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %14, i32* %15
%16 = tail call i32 @strcpy(i32 undef, i8* %0)
%17 = load i32, i32* @AF_UNIX
%18 = load i32, i32* @SOCK_STREAM
%19 = tail call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %35, label %21
21: ; preds = %13
%22 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%23 = call i32 @connect(i32 %19, %struct.sockaddr* %22, i32 8)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %35
25: ; preds = %21
%26 = call i32 @sleep(i32 1)
%27 = call i32 @connect(i32 %19, %struct.sockaddr* %22, i32 8)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %35
29: ; preds = %25
%30 = call i32 @sleep(i32 1)
%31 = call i32 @connect(i32 %19, %struct.sockaddr* %22, i32 8)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @sleep(i32 1)
br label %35
35: ; preds = %21, %25, %29, %33, %13, %1, %5, %11
%36 = phi i32 [ -1, %11 ], [ -1, %5 ], [ -1, %1 ], [ -1, %13 ], [ -1, %33 ], [ %19, %29 ], [ %19, %25 ], [ %19, %21 ]
ret i32 %36
}