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

/* Variables and functions */
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int ProcSocket(char *sname) {
  int sock;
  struct sockaddr_un adr;
  memset(&adr, 0, sizeof(adr));
  adr.sun_family = PF_UNIX;
  strcpy(adr.sun_path, sname);
  sock = socket(PF_UNIX, SOCK_STREAM, 0);
  if (sock < 0)
    return sock;
  if (connect(sock, (struct sockaddr *)&adr, sizeof(adr)) < 0) {
    perror("UNIX connect");
    close(sock);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<ProcSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c5 <ProcSocket+0x65>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1206 <ProcSocket+0xa6>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    1200 <ProcSocket+0xa0>
lea    0xe19(%rip),%rdi
call   1060 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <ProcSocket+0xa6>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ProcSocket>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e7b(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    (%r14),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11bc <ProcSocket+0x5c>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1070 <connect@plt>
test   %rax,%rax
js     11c6 <ProcSocket+0x66>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe33(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
jmp    11bc <ProcSocket+0x5c>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [13 x i8] c"UNIX connect\00"
define i32 @ProcSocket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
store i8* %0, i8** %3
%6 = call i32 @memset(%struct.sockaddr_un* %5, i32 0, i32 8)
%7 = load i32, i32* @PF_UNIX
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%10 = load i32, i32* %9
%11 = load i8*, i8** %3
%12 = call i32 @strcpy(i32 %10, i8* %11)
%13 = load i32, i32* @PF_UNIX
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %1
%19 = load i32, i32* %4
store i32 %19, i32* %2
br label %31
20: ; preds = %1
%21 = load i32, i32* %4
%22 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%23 = call i64 @connect(i32 %21, %struct.sockaddr* %22, i32 8)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%27 = load i32, i32* %4
%28 = call i32 @close(i32 %27)
store i32 -1, i32* %2
br label %31
29: ; preds = %20
%30 = load i32, i32* %4
store i32 %30, i32* %2
br label %31
31: ; preds = %29, %25, %18
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [13 x i8] c"UNIX connect\00"
define i32 @ProcSocket(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%5 = load i32, i32* @PF_UNIX
%6 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %5, i32* %6
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = call i32 @strcpy(i32 %8, i8* %0)
%10 = load i32, i32* @PF_UNIX
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %21, label %14
14: ; preds = %1
%15 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%16 = call i64 @connect(i32 %12, %struct.sockaddr* %15, i32 8)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @close(i32 %12)
br label %21
21: ; preds = %14, %1, %18
%22 = phi i32 [ -1, %18 ], [ %12, %1 ], [ %12, %14 ]
ret i32 %22
}