#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 /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_un *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int errno;
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int opensock(const char *path, int *fd) {
  struct sockaddr_un sa;
  bzero(&sa, sizeof(sa));
  sa.sun_family = AF_UNIX;
  strcpy(sa.sun_path, path);
  if ((*fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
    return (errno);
  if (connect(*fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_un)) != 0) {
    (void)close(*fd);
    *fd = -1;
    return (errno);
  }
  return (0);
}


x86 assembly (clang, -O0):
<opensock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x8,%esi
call   1050 <bzero@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    -0x18(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0xffffffff,%eax
jne    11bf <opensock+0x6f>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    120a <opensock+0xba>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
je     1203 <opensock+0xb3>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
movl   $0xffffffff,(%rax)
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    120a <opensock+0xba>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opensock>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    %rsp,%rdi
mov    $0x8,%esi
call   1050 <bzero@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,(%rbx)
cmp    $0xffffffff,%eax
je     11c1 <opensock+0x71>
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1060 <connect@plt>
test   %rax,%rax
je     11ca <opensock+0x7a>
mov    (%rbx),%edi
call   1040 <close@plt>
movl   $0xffffffff,(%rbx)
mov    0x2e00(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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