#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_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ assert(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char const *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int unix_domain_connect(const char *path) {
  struct sockaddr_un addr;
  int fd = 0;
  assert(NULL != path);
  fd = socket(PF_UNIX, SOCK_STREAM, 0);
  if (fd < 0) {
    return -1;
  }
  memset(&addr, 0, sizeof(struct sockaddr_un));
  addr.sun_family = AF_UNIX;
  strncpy(addr.sun_path, path,
          (((strlen(path)) < (sizeof(addr.sun_path)))
               ? (strlen(path))
               : (sizeof(addr.sun_path))));
  if (0 != connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un))) {
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<unix_domain_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11b8 <unix_domain_connect+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <unix_domain_connect+0xfc>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cltq
cmp    $0x4,%rax
jae    120b <unix_domain_connect+0xab>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cltq
mov    %rax,-0x38(%rbp)
jmp    1219 <unix_domain_connect+0xb9>
mov    $0x4,%eax
mov    %rax,-0x38(%rbp)
jmp    1219 <unix_domain_connect+0xb9>
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edi
mov    -0x38(%rbp),%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1070 <connect@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1256 <unix_domain_connect+0xf6>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <unix_domain_connect+0xfc>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unix_domain_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1060 <assert@plt>
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11fc <unix_domain_connect+0x9c>
mov    %eax,%ebp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%r14d
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    $0x4,%edx
cmp    $0x3,%eax
ja     11d7 <unix_domain_connect+0x77>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1030 <strncpy@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1070 <connect@plt>
neg    %rax
sbb    %eax,%eax
or     %ebp,%eax
jmp    1201 <unix_domain_connect+0xa1>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
define i32 @unix_domain_connect(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %5
%6 = load i8*, i8** %3
%7 = icmp ne i8* null, %6
%8 = zext i1 %7 to i32
%9 = call i32 @assert(i32 %8)
%10 = load i32, i32* @PF_UNIX
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 -1, i32* %2
br label %43
16: ; preds = %1
%17 = call i32 @memset(%struct.sockaddr_un* %4, i32 0, i32 8)
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load i8*, i8** %3
%23 = load i8*, i8** %3
%24 = call i32 @strlen(i8* %23)
%25 = sext i32 %24 to i64
%26 = icmp ult i64 %25, 4
br i1 %26, label %27, label %31
27: ; preds = %16
%28 = load i8*, i8** %3
%29 = call i32 @strlen(i8* %28)
%30 = sext i32 %29 to i64
br label %32
31: ; preds = %16
br label %32
32: ; preds = %31, %27
%33 = phi i64 [ %30, %27 ], [ 4, %31 ]
%34 = trunc i64 %33 to i32
%35 = call i32 @strncpy(i32 %21, i8* %22, i32 %34)
%36 = load i32, i32* %5
%37 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%38 = call i64 @connect(i32 %36, %struct.sockaddr* %37, i32 8)
%39 = icmp ne i64 0, %38
br i1 %39, label %40, label %41
40: ; preds = %32
store i32 -1, i32* %2
br label %43
41: ; preds = %32
%42 = load i32, i32* %5
store i32 %42, i32* %2
br label %43
43: ; preds = %41, %40, %15
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
define i32 @unix_domain_connect(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = icmp ne i8* %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = load i32, i32* @PF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %28, label %11
11: ; preds = %1
%12 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%13 = load i32, i32* @AF_UNIX
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @strlen(i8* %0)
%18 = icmp ult i32 %17, 4
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = call i32 @strlen(i8* %0)
br label %21
21: ; preds = %11, %19
%22 = phi i32 [ %20, %19 ], [ 4, %11 ]
%23 = call i32 @strncpy(i32 %16, i8* %0, i32 %22)
%24 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%25 = call i64 @connect(i32 %9, %struct.sockaddr* %24, i32 8)
%26 = icmp eq i64 %25, 0
%27 = select i1 %26, i32 %9, i32 -1
br label %28
28: ; preds = %21, %1
%29 = phi i32 [ -1, %1 ], [ %27, %21 ]
ret i32 %29
}