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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ assert(char const *);
int /*<<< orphan*/ close(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 *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int check_unix(const char *sockfile) {
  assert(sockfile);
  int rv = -1;
  int sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock == -1)
    return rv;
  struct sockaddr_un remote;
  memset(&remote, 0, sizeof(struct sockaddr_un));
  remote.sun_family = AF_UNIX;
  strncpy(remote.sun_path, sockfile, sizeof(remote.sun_path) - 1);
  int len = strlen(remote.sun_path) + sizeof(remote.sun_family);
  if (*sockfile == '@')
    remote.sun_path[0] = '\0';
  if (connect(sock, (struct sockaddr *)&remote, len) == 0)
    rv = 0;
  close(sock);
  return rv;
}


x86 assembly (clang, -O0):
<check_unix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <assert@plt>
movl   $0xffffffff,-0x14(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11bd <check_unix+0x4d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1242 <check_unix+0xd2>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x40,%eax
jne    1214 <check_unix+0xa4>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jne    1234 <check_unix+0xc4>
movl   $0x0,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_unix>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1070 <assert@plt>
mov    0x2e61(%rip),%r14
mov    (%r14),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     11fa <check_unix+0x8a>
mov    %eax,%ebp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
mov    (%rsp),%rdi
mov    %rbx,%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    (%rsp),%rdi
call   1040 <strlen@plt>
add    $0x4,%eax
cmpb   $0x40,(%rbx)
jne    11e0 <check_unix+0x70>
mov    (%rsp),%rcx
movb   $0x0,(%rcx)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1080 <connect@plt>
neg    %rax
sbb    %ebx,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
jmp    11ff <check_unix+0x8f>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
define i32 @check_unix(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @assert(i8* %8)
store i32 -1, i32* %4
%10 = load i32, i32* @AF_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 eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = load i32, i32* %4
store i32 %16, i32* %2
br label %50
17: ; preds = %1
%18 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 16)
%19 = load i32, i32* @AF_UNIX
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = load i8*, i8** %3
%24 = call i32 @strncpy(i8* %22, i8* %23, i32 7)
%25 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = call i32 @strlen(i8* %26)
%28 = sext i32 %27 to i64
%29 = add i64 %28, 4
%30 = trunc i64 %29 to i32
store i32 %30, i32* %7
%31 = load i8*, i8** %3
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 64
br i1 %34, label %35, label %39
35: ; preds = %17
%36 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = getelementptr inbounds i8, i8* %37, i64 0
store i8 0, i8* %38
br label %39
39: ; preds = %35, %17
%40 = load i32, i32* %5
%41 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%42 = load i32, i32* %7
%43 = call i64 @connect(i32 %40, %struct.sockaddr* %41, i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %39
store i32 0, i32* %4
br label %46
46: ; preds = %45, %39
%47 = load i32, i32* %5
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %4
store i32 %49, i32* %2
br label %50
50: ; preds = %46, %15
%51 = load i32, i32* %2
ret i32 %51
}

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