#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*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int EAGAIN;
int EINPROGRESS;
int EINTR;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ Sactivate_thread();
int /*<<< orphan*/ Sdeactivate_thread();
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int errno;
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ ss_set_fd_non_blocking(int);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(char const *);
int ss_connect_to_unix_host_impl(const char *pathname, int blocking) {
  struct sockaddr_un addr;
  memset(&addr, 0, sizeof(addr));
  if (strlen(pathname) >= sizeof(addr.sun_path)) {
    errno = 0;
    return -1;
  }
  addr.sun_family = AF_UNIX;
  strcpy(addr.sun_path, pathname);
  Sdeactivate_thread();
  int saved_errno = 0;
  int err = 0;
  int sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock == -1) {
    saved_errno = errno;
    err = -2;
  } else {
    fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC);
    if (!blocking && !(ss_set_fd_non_blocking(sock))) {
      saved_errno = errno;
      close(sock);
      err = -2;
    }
  }
  if (!err) {
    int res;
    do {
      res = connect(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_un));
    } while (res == -1 && errno == EINTR);
    saved_errno = errno;
    if (res == -1 && errno != EINPROGRESS && errno != EAGAIN) {
      close(sock);
      err = -3;
    }
  }
  Sactivate_thread();
  errno = saved_errno;
  if (err)
    return err;
  return sock;
}


x86 assembly (clang, -O0):
<ss_connect_to_unix_host_impl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
cltq
cmp    $0x4,%rax
jb     11ed <ss_connect_to_unix_host_impl+0x4d>
mov    0x2ddd(%rip),%rax
movl   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13ad <ss_connect_to_unix_host_impl+0x20d>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    $0x0,%al
call   1040 <Sdeactivate_thread@plt>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1258 <ss_connect_to_unix_host_impl+0xb8>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
movl   $0xfffffffe,-0x28(%rbp)
jmp    12d0 <ss_connect_to_unix_host_impl+0x130>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1050 <fcntl@plt>
mov    -0x38(%rbp),%edi
mov    -0x34(%rbp),%esi
mov    %eax,%edx
mov    0x2d34(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1050 <fcntl@plt>
cmpl   $0x0,-0x14(%rbp)
jne    12cb <ss_connect_to_unix_host_impl+0x12b>
mov    -0x2c(%rbp),%edi
call   1080 <ss_set_fd_non_blocking@plt>
cmp    $0x0,%eax
jne    12cb <ss_connect_to_unix_host_impl+0x12b>
mov    0x2d01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x2c(%rbp),%edi
call   1090 <close@plt>
movl   $0xfffffffe,-0x28(%rbp)
jmp    12d0 <ss_connect_to_unix_host_impl+0x130>
cmpl   $0x0,-0x28(%rbp)
jne    137f <ss_connect_to_unix_host_impl+0x1df>
jmp    12df <ss_connect_to_unix_host_impl+0x13f>
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   10b0 <connect@plt>
mov    %eax,-0x30(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x30(%rbp)
mov    %al,-0x39(%rbp)
jne    131a <ss_connect_to_unix_host_impl+0x17a>
mov    0x2caf(%rip),%rax
mov    (%rax),%eax
mov    0x2c96(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    12df <ss_connect_to_unix_host_impl+0x13f>
mov    0x2c8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    137a <ss_connect_to_unix_host_impl+0x1da>
mov    0x2c76(%rip),%rax
mov    (%rax),%eax
mov    0x2c9d(%rip),%rcx
cmp    (%rcx),%eax
je     137a <ss_connect_to_unix_host_impl+0x1da>
mov    0x2c5e(%rip),%rax
mov    (%rax),%eax
mov    0x2c35(%rip),%rcx
cmp    (%rcx),%eax
je     137a <ss_connect_to_unix_host_impl+0x1da>
mov    -0x2c(%rbp),%edi
call   1090 <close@plt>
movl   $0xfffffffd,-0x28(%rbp)
jmp    137f <ss_connect_to_unix_host_impl+0x1df>
mov    $0x0,%al
call   10a0 <Sactivate_thread@plt>
mov    -0x24(%rbp),%ecx
mov    0x2c28(%rip),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x28(%rbp)
je     13a7 <ss_connect_to_unix_host_impl+0x207>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13ad <ss_connect_to_unix_host_impl+0x20d>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ss_connect_to_unix_host_impl>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
lea    0x8(%rsp),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    $0xffffffff,%r15d
cmp    $0x3,%eax
ja     12f2 <ss_connect_to_unix_host_impl+0x152>
mov    0x2dfd(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
call   1040 <Sdeactivate_thread@plt>
mov    (%rbx),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     12c9 <ss_connect_to_unix_host_impl+0x129>
mov    %eax,%r15d
mov    0x2daf(%rip),%rax
mov    (%rax),%ebp
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
xor    %eax,%eax
call   1050 <fcntl@plt>
mov    0x2d83(%rip),%rcx
or     (%rcx),%eax
mov    %r15d,%edi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fcntl@plt>
test   %r14d,%r14d
jne    125e <ss_connect_to_unix_host_impl+0xbe>
mov    %r15d,%edi
call   1080 <ss_set_fd_non_blocking@plt>
test   %eax,%eax
je     12d4 <ss_connect_to_unix_host_impl+0x134>
lea    0x8(%rsp),%r14
mov    0x2d4e(%rip),%r12
mov    0x2d37(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   10b0 <connect@plt>
mov    (%r12),%ebx
cmp    $0xffffffff,%eax
jne    129e <ss_connect_to_unix_host_impl+0xfe>
cmp    0x0(%rbp),%ebx
je     1280 <ss_connect_to_unix_host_impl+0xe0>
cmp    $0xffffffff,%eax
jne    12eb <ss_connect_to_unix_host_impl+0x14b>
mov    0x2d3e(%rip),%rax
cmp    (%rax),%ebx
je     12eb <ss_connect_to_unix_host_impl+0x14b>
mov    0x2ce3(%rip),%rax
cmp    (%rax),%ebx
je     12eb <ss_connect_to_unix_host_impl+0x14b>
mov    %r15d,%edi
call   1090 <close@plt>
mov    $0xfffffffd,%r15d
jmp    12eb <ss_connect_to_unix_host_impl+0x14b>
mov    0x2ce8(%rip),%rax
mov    (%rax),%ebx
jmp    12e5 <ss_connect_to_unix_host_impl+0x145>
mov    0x2cdd(%rip),%rax
mov    (%rax),%ebx
mov    %r15d,%edi
call   1090 <close@plt>
mov    $0xfffffffe,%r15d
xor    %eax,%eax
call   10a0 <Sactivate_thread@plt>
mov    0x2cbf(%rip),%rax
mov    %ebx,(%rax)
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@errno = global i32 0
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@EINTR = global i32 0
@EINPROGRESS = global i32 0
@EAGAIN = global i32 0
define i32 @ss_connect_to_unix_host_impl(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 8)
%12 = load i8*, i8** %4
%13 = call i32 @strlen(i8* %12)
%14 = sext i32 %13 to i64
%15 = icmp uge i64 %14, 4
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 0, i32* @errno
store i32 -1, i32* %3
br label %94
17: ; preds = %2
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load i8*, i8** %4
%23 = call i32 @strcpy(i32 %21, i8* %22)
%24 = call i32 (...) @Sdeactivate_thread()
store i32 0, i32* %7
store i32 0, i32* %8
%25 = load i32, i32* @AF_UNIX
%26 = load i32, i32* @SOCK_STREAM
%27 = call i32 @socket(i32 %25, i32 %26, i32 0)
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %17
%31 = load i32, i32* @errno
store i32 %31, i32* %7
store i32 -2, i32* %8
br label %52
32: ; preds = %17
%33 = load i32, i32* %9
%34 = load i32, i32* @F_SETFD
%35 = load i32, i32* %9
%36 = load i32, i32* @F_GETFD
%37 = call i32 (i32, i32, ...) @fcntl(i32 %35, i32 %36)
%38 = load i32, i32* @FD_CLOEXEC
%39 = or i32 %37, %38
%40 = call i32 (i32, i32, ...) @fcntl(i32 %33, i32 %34, i32 %39)
%41 = load i32, i32* %5
%42 = icmp ne i32 %41, 0
br i1 %42, label %51, label %43
43: ; preds = %32
%44 = load i32, i32* %9
%45 = call i32 @ss_set_fd_non_blocking(i32 %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %51, label %47
47: ; preds = %43
%48 = load i32, i32* @errno
store i32 %48, i32* %7
%49 = load i32, i32* %9
%50 = call i32 @close(i32 %49)
store i32 -2, i32* %8
br label %51
51: ; preds = %47, %43, %32
br label %52
52: ; preds = %51, %30
%53 = load i32, i32* %8
%54 = icmp ne i32 %53, 0
br i1 %54, label %85, label %55
55: ; preds = %52
br label %56
56: ; preds = %67, %55
%57 = load i32, i32* %9
%58 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%59 = call i32 @connect(i32 %57, %struct.sockaddr* %58, i32 8)
store i32 %59, i32* %10
br label %60
60: ; preds = %56
%61 = load i32, i32* %10
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %67
63: ; preds = %60
%64 = load i32, i32* @errno
%65 = load i32, i32* @EINTR
%66 = icmp eq i32 %64, %65
br label %67
67: ; preds = %63, %60
%68 = phi i1 [ false, %60 ], [ %66, %63 ]
br i1 %68, label %56, label %69
69: ; preds = %67
%70 = load i32, i32* @errno
store i32 %70, i32* %7
%71 = load i32, i32* %10
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %84
73: ; preds = %69
%74 = load i32, i32* @errno
%75 = load i32, i32* @EINPROGRESS
%76 = icmp ne i32 %74, %75
br i1 %76, label %77, label %84
77: ; preds = %73
%78 = load i32, i32* @errno
%79 = load i32, i32* @EAGAIN
%80 = icmp ne i32 %78, %79
br i1 %80, label %81, label %84
81: ; preds = %77
%82 = load i32, i32* %9
%83 = call i32 @close(i32 %82)
store i32 -3, i32* %8
br label %84
84: ; preds = %81, %77, %73, %69
br label %85
85: ; preds = %84, %52
%86 = call i32 (...) @Sactivate_thread()
%87 = load i32, i32* %7
store i32 %87, i32* @errno
%88 = load i32, i32* %8
%89 = icmp ne i32 %88, 0
br i1 %89, label %90, label %92
90: ; preds = %85
%91 = load i32, i32* %8
store i32 %91, i32* %3
br label %94
92: ; preds = %85
%93 = load i32, i32* %9
store i32 %93, i32* %3
br label %94
94: ; preds = %92, %90, %16
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@errno = global i32 0
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@EINTR = global i32 0
@EINPROGRESS = global i32 0
@EAGAIN = global i32 0
define i32 @ss_connect_to_unix_host_impl(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %3 to i8*
%5 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%6 = call i32 @strlen(i8* %0)
%7 = icmp ugt i32 %6, 3
br i1 %7, label %58, label %8
8: ; preds = %2
%9 = load i32, i32* @AF_UNIX
%10 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @strcpy(i32 %12, i8* %0)
%14 = call i32 (...) @Sdeactivate_thread()
%15 = load i32, i32* @AF_UNIX
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %8
%20 = load i32, i32* @errno
br label %54
21: ; preds = %8
%22 = load i32, i32* @F_SETFD
%23 = load i32, i32* @F_GETFD
%24 = call i32 (i32, i32, ...) @fcntl(i32 %17, i32 %23)
%25 = load i32, i32* @FD_CLOEXEC
%26 = or i32 %25, %24
%27 = call i32 (i32, i32, ...) @fcntl(i32 %17, i32 %22, i32 %26)
%28 = icmp eq i32 %1, 0
br i1 %28, label %29, label %35
29: ; preds = %21
%30 = call i32 @ss_set_fd_non_blocking(i32 %17)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* @errno
%34 = call i32 @close(i32 %17)
br label %54
35: ; preds = %21, %29
%36 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
br label %37
37: ; preds = %35, %37
%38 = call i32 @connect(i32 %17, %struct.sockaddr* %36, i32 8)
%39 = icmp eq i32 %38, -1
%40 = load i32, i32* @errno
%41 = load i32, i32* @EINTR
%42 = icmp eq i32 %40, %41
%43 = select i1 %39, i1 %42, i1 false
br i1 %43, label %37, label %44
44: ; preds = %37
%45 = xor i1 %39, true
%46 = load i32, i32* @EINPROGRESS
%47 = icmp eq i32 %40, %46
%48 = select i1 %45, i1 true, i1 %47
%49 = load i32, i32* @EAGAIN
%50 = icmp eq i32 %40, %49
%51 = select i1 %48, i1 true, i1 %50
br i1 %51, label %54, label %52
52: ; preds = %44
%53 = call i32 @close(i32 %17)
br label %54
54: ; preds = %44, %32, %19, %52
%55 = phi i32 [ %40, %52 ], [ %33, %32 ], [ %20, %19 ], [ %40, %44 ]
%56 = phi i32 [ -3, %52 ], [ -2, %32 ], [ -2, %19 ], [ %17, %44 ]
%57 = call i32 (...) @Sactivate_thread()
br label %58
58: ; preds = %54, %2
%59 = phi i32 [ 0, %2 ], [ %55, %54 ]
%60 = phi i32 [ -1, %2 ], [ %56, %54 ]
store i32 %59, i32* @errno
ret i32 %60
}