#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;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ PF_UNIX;
int SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ sun_set_path(struct sockaddr_un *, char const *);
int OpenUnixSocket(const char *Path, int Type) {
  int sock;
  struct sockaddr_un sa;
  int val;
  if (Type == 0)
    Type = SOCK_STREAM;
  sock = socket(PF_UNIX, Type, 0);
  if (sock == -1)
    return (-1);
  sun_set_path(&sa, Path);
  val = sizeof(sa);
  if (connect(sock, (struct sockaddr *)&sa, val) == 0)
    return (sock);
  close(sock);
  return (-1);
}


x86 assembly (clang, -O0):
<OpenUnixSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1165 <OpenUnixSocket+0x25>
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e8c(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1191 <OpenUnixSocket+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <OpenUnixSocket+0x98>
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdi
call   1040 <sun_set_path@plt>
movl   $0x4,-0x24(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
jne    11c9 <OpenUnixSocket+0x89>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d8 <OpenUnixSocket+0x98>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<OpenUnixSocket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
test   %esi,%esi
jne    1157 <OpenUnixSocket+0x17>
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
mov    0x2e9a(%rip),%rax
mov    (%rax),%edi
xor    %edx,%edx
call   1060 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11a0 <OpenUnixSocket+0x60>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <sun_set_path@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1050 <connect@plt>
test   %rax,%rax
je     119e <OpenUnixSocket+0x5e>
mov    %ebp,%edi
call   1030 <close@plt>
jmp    11a0 <OpenUnixSocket+0x60>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
@PF_UNIX = global i32 0
define i32 @OpenUnixSocket(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_un
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i32, i32* @SOCK_STREAM
store i32 %12, i32* %5
br label %13
13: ; preds = %11, %2
%14 = load i32, i32* @PF_UNIX
%15 = load i32, i32* %5
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %13
store i32 -1, i32* %3
br label %33
20: ; preds = %13
%21 = load i8*, i8** %4
%22 = call i32 @sun_set_path(%struct.sockaddr_un* %7, i8* %21)
store i32 4, i32* %8
%23 = load i32, i32* %6
%24 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
%25 = load i32, i32* %8
%26 = call i64 @connect(i32 %23, %struct.sockaddr* %24, i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %20
%29 = load i32, i32* %6
store i32 %29, i32* %3
br label %33
30: ; preds = %20
%31 = load i32, i32* %6
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %3
br label %33
33: ; preds = %30, %28, %19
%34 = load i32, i32* %3
ret i32 %34
}

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