#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;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ memset(int /*<<< orphan*/, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int trysocket(char *name, int type) {
  int fd;
  struct sockaddr_un addr = {.sun_family = AF_UNIX};
  memset(addr.sun_path, 0, 108);
  strncpy(addr.sun_path, name, 108 - 1);
  if ((fd = socket(AF_UNIX, type, 0)) < 0)
    return -1;
  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)))
    return -1;
  return fd;
}


x86 assembly (clang, -O0):
<trysocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2e8b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
xor    %esi,%esi
mov    $0x6c,%edx
call   1040 <memset@plt>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x6b,%edx
call   1030 <strncpy@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11ad <trysocket+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11da <trysocket+0x9a>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
je     11d4 <trysocket+0x94>
movl   $0xffffffff,-0x4(%rbp)
jmp    11da <trysocket+0x9a>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trysocket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
movl   $0x0,0x8(%rsp)
mov    0x2e8c(%rip),%r14
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
xor    %edi,%edi
xor    %esi,%esi
mov    $0x6c,%edx
call   1040 <memset@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x6b,%edx
call   1030 <strncpy@plt>
mov    (%r14),%edi
mov    %ebp,%esi
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11ac <trysocket+0x6c>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1050 <connect@plt>
neg    %rax
sbb    %eax,%eax
or     %ebx,%eax
jmp    11b1 <trysocket+0x71>
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 }
@AF_UNIX = global i32 0
define i32 @trysocket(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_un
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
store i32 0, i32* %8
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 1
%10 = load i32, i32* @AF_UNIX
store i32 %10, i32* %9
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @memset(i32 %12, i32 0, i32 108)
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i8*, i8** %4
%17 = call i32 @strncpy(i32 %15, i8* %16, i32 107)
%18 = load i32, i32* @AF_UNIX
%19 = load i32, i32* %5
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %2
store i32 -1, i32* %3
br label %31
23: ; preds = %2
%24 = load i32, i32* %6
%25 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
%26 = call i64 @connect(i32 %24, %struct.sockaddr* %25, i32 8)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 -1, i32* %3
br label %31
29: ; preds = %23
%30 = load i32, i32* %6
store i32 %30, i32* %3
br label %31
31: ; preds = %29, %28, %22
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
define i32 @trysocket(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %3 to i8*
%5 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
store i32 0, i32* %5
%6 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
%7 = load i32, i32* @AF_UNIX
store i32 %7, i32* %6
%8 = tail call i32 @memset(i32 0, i32 0, i32 108)
%9 = tail call i32 @strncpy(i32 0, i8* %0, i32 107)
%10 = load i32, i32* @AF_UNIX
%11 = tail call i32 @socket(i32 %10, i32 %1, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %2
%14 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%15 = call i64 @connect(i32 %11, %struct.sockaddr* %14, i32 8)
%16 = icmp eq i64 %15, 0
%17 = select i1 %16, i32 %11, i32 -1
br label %18
18: ; preds = %13, %2
%19 = phi i32 [ -1, %2 ], [ %17, %13 ]
ret i32 %19
}