#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
scalar_t__ EINPROGRESS;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ net_err(char *, char *, int /*<<< orphan*/);
int newsocket(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(scalar_t__);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int tcp_nonblock(char *, int);
int unix_connect(char *err, const char *path, int nonblock) {
  int fd = -1;
  struct sockaddr_un sa;
  if ((fd == newsocket(err, AF_LOCAL)) == -1)
    return -1;
  sa.sun_family = AF_LOCAL;
  strncpy(sa.sun_path, path, sizeof(sa.sun_path) - 1);
  if (nonblock == 1)
    if (tcp_nonblock(err, fd) == -1)
      return -1;
  if (connect(fd, (struct sockaddr *)&sa, sizeof sa) == -1) {
    if (errno == EINPROGRESS && nonblock == 1)
      return fd;
    net_err(err, "connect: %s", strerror(errno));
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<unix_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
call   1090 <newsocket@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
cmp    $0xffffffff,%eax
jne    11c6 <unix_connect+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <unix_connect+0x12f>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
cmpl   $0x1,-0x1c(%rbp)
jne    1213 <unix_connect+0xa3>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1070 <tcp_nonblock@plt>
cmp    $0xffffffff,%eax
jne    120e <unix_connect+0x9e>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <unix_connect+0x12f>
jmp    1213 <unix_connect+0xa3>
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    1299 <unix_connect+0x129>
mov    0x2d9c(%rip),%rax
mov    (%rax),%rax
mov    0x2daa(%rip),%rcx
cmp    (%rcx),%rax
jne    125c <unix_connect+0xec>
cmpl   $0x1,-0x1c(%rbp)
jne    125c <unix_connect+0xec>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129f <unix_connect+0x12f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d65(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%edx
lea    0xd80(%rip),%rsi
call   1060 <net_err@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <unix_connect+0x12f>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unix_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e59(%rip),%rbp
mov    0x0(%rbp),%esi
call   1090 <newsocket@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x4(%rsp)
mov    %rbx,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
cmp    $0x1,%r15d
jne    11bb <unix_connect+0x4b>
mov    %r14,%rdi
mov    $0xffffffff,%esi
call   1070 <tcp_nonblock@plt>
cmp    $0xffffffff,%eax
je     120e <unix_connect+0x9e>
mov    %rsp,%rsi
mov    $0xffffffff,%edi
mov    $0x8,%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    120e <unix_connect+0x9e>
mov    0x2df7(%rip),%rax
mov    (%rax),%rdi
cmp    $0x1,%r15d
jne    11ee <unix_connect+0x7e>
mov    0x2dff(%rip),%rax
cmp    (%rax),%rdi
je     120e <unix_connect+0x9e>
call   1080 <strerror@plt>
lea    0xe06(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   1060 <net_err@plt>
mov    $0xffffffff,%edi
call   1040 <close@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str = [12 x i8] c"connect: %s\00"
define i32 @unix_connect(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_un
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 -1, i32* %8
%10 = load i32, i32* %8
%11 = load i8*, i8** %5
%12 = load i32, i32* @AF_LOCAL
%13 = call i32 @newsocket(i8* %11, i32 %12)
%14 = icmp eq i32 %10, %13
%15 = zext i1 %14 to i32
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %57
18: ; preds = %3
%19 = load i32, i32* @AF_LOCAL
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%22 = load i32, i32* %21
%23 = load i8*, i8** %6
%24 = call i32 @strncpy(i32 %22, i8* %23, i32 3)
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %34
27: ; preds = %18
%28 = load i8*, i8** %5
%29 = load i32, i32* %8
%30 = call i32 @tcp_nonblock(i8* %28, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %33
32: ; preds = %27
store i32 -1, i32* %4
br label %57
33: ; preds = %27
br label %34
34: ; preds = %33, %18
%35 = load i32, i32* %8
%36 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%37 = call i32 @connect(i32 %35, %struct.sockaddr* %36, i32 8)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %55
39: ; preds = %34
%40 = load i64, i64* @errno
%41 = load i64, i64* @EINPROGRESS
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %48
43: ; preds = %39
%44 = load i32, i32* %7
%45 = icmp eq i32 %44, 1
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = load i32, i32* %8
store i32 %47, i32* %4
br label %57
48: ; preds = %43, %39
%49 = load i8*, i8** %5
%50 = load i64, i64* @errno
%51 = call i32 @strerror(i64 %50)
%52 = call i32 @net_err(i8* %49, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %51)
%53 = load i32, i32* %8
%54 = call i32 @close(i32 %53)
store i32 -1, i32* %4
br label %57
55: ; preds = %34
%56 = load i32, i32* %8
store i32 %56, i32* %4
br label %57
57: ; preds = %55, %48, %46, %32, %17
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str = [12 x i8] c"connect: %s\00"
define i32 @unix_connect(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_un
%5 = bitcast %struct.sockaddr_un* %4 to i8*
%6 = load i32, i32* @AF_LOCAL
%7 = tail call i32 @newsocket(i8* %0, i32 %6)
%8 = load i32, i32* @AF_LOCAL
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 1
store i32 %8, i32* %9
%10 = tail call i32 @strncpy(i32 undef, i8* %1, i32 3)
%11 = icmp eq i32 %2, 1
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = tail call i32 @tcp_nonblock(i8* %0, i32 -1)
%14 = icmp eq i32 %13, -1
br i1 %14, label %28, label %15
15: ; preds = %12, %3
%16 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%17 = call i32 @connect(i32 -1, %struct.sockaddr* %16, i32 8)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %28
19: ; preds = %15
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINPROGRESS
%22 = icmp eq i64 %20, %21
%23 = and i1 %11, %22
br i1 %23, label %28, label %24
24: ; preds = %19
%25 = call i32 @strerror(i64 %20)
%26 = call i32 @net_err(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = call i32 @close(i32 -1)
br label %28
28: ; preds = %15, %19, %12, %24
ret i32 -1
}