#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*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int SOCK_DGRAM;
int SOCK_STREAM;
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, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int OpenUnixSocket(const char *Path, int Type) {
  int sock;
  struct sockaddr_un sa;
  int val;
  if (Type == 1)
    val = SOCK_STREAM;
  else
    val = SOCK_DGRAM;
  sock = socket(AF_UNIX, val, 0);
  if (sock == -1)
    return (-1);
  memset(&sa, 0, sizeof(struct sockaddr_un));
  sa.sun_family = AF_UNIX;
  strcpy(sa.sun_path, 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   $0x1,-0x14(%rbp)
jne    117a <OpenUnixSocket+0x2a>
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    1186 <OpenUnixSocket+0x36>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11b2 <OpenUnixSocket+0x62>
movl   $0xffffffff,-0x4(%rbp)
jmp    1214 <OpenUnixSocket+0xc4>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x8,-0x24(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jne    1205 <OpenUnixSocket+0xb5>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1214 <OpenUnixSocket+0xc4>
mov    -0x18(%rbp),%edi
call   1050 <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   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
cmp    $0x1,%esi
jne    116d <OpenUnixSocket+0x1d>
mov    0x2e65(%rip),%rax
jmp    1174 <OpenUnixSocket+0x24>
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
mov    0x2e6b(%rip),%r12
mov    (%r12),%edi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11db <OpenUnixSocket+0x8b>
mov    %eax,%ebp
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    (%r12),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1060 <connect@plt>
test   %rax,%rax
je     11d9 <OpenUnixSocket+0x89>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11db <OpenUnixSocket+0x8b>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
@SOCK_DGRAM = global i32 0
@AF_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, 1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i32, i32* @SOCK_STREAM
store i32 %12, i32* %8
br label %15
13: ; preds = %2
%14 = load i32, i32* @SOCK_DGRAM
store i32 %14, i32* %8
br label %15
15: ; preds = %13, %11
%16 = load i32, i32* @AF_UNIX
%17 = load i32, i32* %8
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %15
store i32 -1, i32* %3
br label %40
22: ; preds = %15
%23 = call i32 @memset(%struct.sockaddr_un* %7, i32 0, i32 8)
%24 = load i32, i32* @AF_UNIX
%25 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 1
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%27 = load i32, i32* %26
%28 = load i8*, i8** %4
%29 = call i32 @strcpy(i32 %27, i8* %28)
store i32 8, i32* %8
%30 = load i32, i32* %6
%31 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
%32 = load i32, i32* %8
%33 = call i64 @connect(i32 %30, %struct.sockaddr* %31, i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %22
%36 = load i32, i32* %6
store i32 %36, i32* %3
br label %40
37: ; preds = %22
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
store i32 -1, i32* %3
br label %40
40: ; preds = %37, %35, %21
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
@SOCK_DGRAM = global i32 0
@AF_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, 1
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @SOCK_DGRAM
%8 = select i1 %5, i32 %6, i32 %7
%9 = load i32, i32* @AF_UNIX
%10 = tail call i32 @socket(i32 %9, i32 %8, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %24, label %12
12: ; preds = %2
%13 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @strcpy(i32 %17, i8* %0)
%19 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%20 = call i64 @connect(i32 %10, %struct.sockaddr* %19, i32 8)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %12
%23 = call i32 @close(i32 %10)
br label %24
24: ; preds = %12, %2, %22
%25 = phi i32 [ -1, %22 ], [ -1, %2 ], [ %10, %12 ]
ret i32 %25
}