#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 dummy;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
scalar_t__ EINPROGRESS;
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ errno;
scalar_t__ sockaddrpathfill(struct sockaddr_un *, char const *);
int unixconnect(int sock, const char *path) {
  struct sockaddr_un sa;
  if (sockaddrpathfill(&sa, path) < 0) {
    return -1;
  }
  if (connect(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr_un)) >= 0) {
    return 0;
  }
  if (errno == EINPROGRESS) {
    return 1;
  }
  return -1;
}


x86 assembly (clang, -O0):
<unixconnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdi
call   1030 <sockaddrpathfill@plt>
cmp    $0x0,%rax
jge    1152 <unixconnect+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a6 <unixconnect+0x86>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1040 <connect@plt>
cmp    $0x0,%rax
jl     1179 <unixconnect+0x59>
movl   $0x0,-0x4(%rbp)
jmp    11a6 <unixconnect+0x86>
mov    0x2e58(%rip),%rax
mov    (%rax),%rax
mov    0x2e5e(%rip),%rcx
cmp    (%rcx),%rax
jne    119f <unixconnect+0x7f>
movl   $0x1,-0x4(%rbp)
jmp    11a6 <unixconnect+0x86>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unixconnect>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
call   1030 <sockaddrpathfill@plt>
test   %rax,%rax
js     1172 <unixconnect+0x52>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1040 <connect@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    1177 <unixconnect+0x57>
mov    0x2e80(%rip),%rax
mov    (%rax),%rax
mov    0x2e86(%rip),%rcx
xor    %edx,%edx
cmp    (%rcx),%rax
sete   %dl
lea    (%rdx,%rdx,1),%eax
add    $0xffffffff,%eax
jmp    1177 <unixconnect+0x57>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINPROGRESS = global i64 0
define i32 @unixconnect(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.sockaddr_un
store i32 %0, i32* %4
store i8* %1, i8** %5
%7 = load i8*, i8** %5
%8 = call i64 @sockaddrpathfill(%struct.sockaddr_un* %6, i8* %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %23
11: ; preds = %2
%12 = load i32, i32* %4
%13 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%14 = call i64 @connect(i32 %12, %struct.sockaddr* %13, i32 4)
%15 = icmp sge i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %11
store i32 0, i32* %3
br label %23
17: ; preds = %11
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINPROGRESS
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 1, i32* %3
br label %23
22: ; preds = %17
store i32 -1, i32* %3
br label %23
23: ; preds = %22, %21, %16, %10
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINPROGRESS = global i64 0
define i32 @unixconnect(i32 %0, i8* %1) {
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %3 to i8*
%5 = call i64 @sockaddrpathfill(%struct.sockaddr_un* %3, i8* %1)
%6 = icmp slt i64 %5, 0
br i1 %6, label %16, label %7
7: ; preds = %2
%8 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%9 = call i64 @connect(i32 %0, %struct.sockaddr* %8, i32 4)
%10 = icmp sgt i64 %9, -1
br i1 %10, label %16, label %11
11: ; preds = %7
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINPROGRESS
%14 = icmp eq i64 %12, %13
%15 = select i1 %14, i32 1, i32 -1
br label %16
16: ; preds = %11, %7, %2
%17 = phi i32 [ -1, %2 ], [ 0, %7 ], [ %15, %11 ]
ret i32 %17
}