#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 {
  char *sun_path;
  void *sun_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
void *AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int unsocket(int socktype, char *srcpath, char *dstpath) {
  struct sockaddr_un dst;
  struct sockaddr_un src;
  int fd;
  int oerr;
  memset(&dst, 0, sizeof dst);
  if ((fd = socket(PF_UNIX, socktype, 0)) == -1) {
    oerr = errno;
    fprintf(stderr, "unsocket: socket: %s\n", strerror(errno));
    goto err_out;
  }
  if (srcpath != NULL) {
    memset(&src, 0, sizeof src);
    src.sun_family = AF_UNIX;
    strncpy(src.sun_path, srcpath, sizeof src.sun_path - 1);
    if (bind(fd, (struct sockaddr *)&src, sizeof src) == -1) {
      oerr = errno;
      fprintf(stderr, "unsocket: bind %s: %s\n", src.sun_path, strerror(errno));
      goto err_out;
    }
  }
  if (dstpath != NULL) {
    dst.sun_family = AF_UNIX;
    strncpy(dst.sun_path, dstpath, sizeof dst.sun_path - 1);
    if (connect(fd, (struct sockaddr *)&dst, sizeof dst) == -1) {
      oerr = errno;
      fprintf(stderr, "unsocket: connect %s: %s\n", dst.sun_path,
              strerror(errno));
      goto err_out;
    }
  }
  return fd;
err_out:
  if (fd != -1)
    close(fd);
  errno = oerr;
  return -1;
}


x86 assembly (clang, -O0):
<unsocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
jne    1201 <unsocket+0x81>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xe0b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1346 <unsocket+0x1c6>
cmpq   $0x0,-0x10(%rbp)
je     12a6 <unsocket+0x126>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    -0x3c(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x10,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    12a1 <unsocket+0x121>
mov    0x2d6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xd81(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1346 <unsocket+0x1c6>
jmp    12a6 <unsocket+0x126>
cmpq   $0x0,-0x18(%rbp)
je     133b <unsocket+0x1bb>
mov    0x2d28(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    -0x3c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    1336 <unsocket+0x1b6>
mov    0x2cd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2cd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    %rax,%rcx
lea    0xd03(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1346 <unsocket+0x1c6>
jmp    133b <unsocket+0x1bb>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    136b <unsocket+0x1eb>
cmpl   $0xffffffff,-0x3c(%rbp)
je     1358 <unsocket+0x1d8>
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
mov    -0x40(%rbp),%ecx
mov    0x2c66(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unsocket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     127e <unsocket+0xfe>
mov    %eax,%ebx
test   %rbp,%rbp
je     1212 <unsocket+0x92>
lea    0x18(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x20(%rsp)
mov    0x18(%rsp),%rdi
mov    %rbp,%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x10,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     12ac <unsocket+0x12c>
test   %r14,%r14
je     12f7 <unsocket+0x177>
mov    0x2dbe(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
mov    0x8(%rsp),%rdi
mov    %r14,%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    12f7 <unsocket+0x177>
mov    0x2d6b(%rip),%rax
mov    (%rax),%ebp
mov    0x2d6a(%rip),%rax
mov    (%rax),%r14d
mov    0x8(%rsp),%r15
mov    %ebp,%edi
call   1090 <strerror@plt>
lea    0xdb1(%rip),%rsi
jmp    12d2 <unsocket+0x152>
mov    0x2d43(%rip),%rax
mov    (%rax),%ebp
mov    0x2d42(%rip),%rax
mov    (%rax),%ebx
mov    %ebp,%edi
call   1090 <strerror@plt>
lea    0xd62(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12e9 <unsocket+0x169>
mov    0x2d15(%rip),%rax
mov    (%rax),%ebp
mov    0x2d14(%rip),%rax
mov    (%rax),%r14d
mov    0x18(%rsp),%r15
mov    %ebp,%edi
call   1090 <strerror@plt>
lea    0xd44(%rip),%rsi
mov    %r14d,%edi
mov    %r15,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2cd8(%rip),%rax
mov    %ebp,(%rax)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i8* }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"unsocket: socket: %s\0A\00"
@AF_UNIX = global i8* null
@.str.1 = [23 x i8] c"unsocket: bind %s: %s\0A\00"
@.str.2 = [26 x i8] c"unsocket: connect %s: %s\0A\00"
define i32 @unsocket(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.sockaddr_un
%9 = alloca %struct.sockaddr_un
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%12 = call i32 @memset(%struct.sockaddr_un* %8, i32 0, i32 16)
%13 = load i32, i32* @PF_UNIX
%14 = load i32, i32* %5
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %10
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %23
17: ; preds = %3
%18 = load i32, i32* @errno
store i32 %18, i32* %11
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %21)
br label %72
23: ; preds = %3
%24 = load i8*, i8** %6
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %47
26: ; preds = %23
%27 = call i32 @memset(%struct.sockaddr_un* %9, i32 0, i32 16)
%28 = load i8*, i8** @AF_UNIX
%29 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 1
store i8* %28, i8** %29
%30 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = load i8*, i8** %6
%33 = call i32 @strncpy(i8* %31, i8* %32, i32 7)
%34 = load i32, i32* %10
%35 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%36 = call i32 @bind(i32 %34, %struct.sockaddr* %35, i32 16)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %46
38: ; preds = %26
%39 = load i32, i32* @errno
store i32 %39, i32* %11
%40 = load i32, i32* @stderr
%41 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%42 = load i8*, i8** %41
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %42, i8* %44)
br label %72
46: ; preds = %26
br label %47
47: ; preds = %46, %23
%48 = load i8*, i8** %7
%49 = icmp ne i8* %48, null
br i1 %49, label %50, label %70
50: ; preds = %47
%51 = load i8*, i8** @AF_UNIX
%52 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 1
store i8* %51, i8** %52
%53 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%54 = load i8*, i8** %53
%55 = load i8*, i8** %7
%56 = call i32 @strncpy(i8* %54, i8* %55, i32 7)
%57 = load i32, i32* %10
%58 = bitcast %struct.sockaddr_un* %8 to %struct.sockaddr*
%59 = call i32 @connect(i32 %57, %struct.sockaddr* %58, i32 16)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %69
61: ; preds = %50
%62 = load i32, i32* @errno
store i32 %62, i32* %11
%63 = load i32, i32* @stderr
%64 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%65 = load i8*, i8** %64
%66 = load i32, i32* @errno
%67 = call i8* @strerror(i32 %66)
%68 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %65, i8* %67)
br label %72
69: ; preds = %50
br label %70
70: ; preds = %69, %47
%71 = load i32, i32* %10
store i32 %71, i32* %4
br label %80
72: ; preds = %61, %38, %17
%73 = load i32, i32* %10
%74 = icmp ne i32 %73, -1
br i1 %74, label %75, label %78
75: ; preds = %72
%76 = load i32, i32* %10
%77 = call i32 @close(i32 %76)
br label %78
78: ; preds = %75, %72
%79 = load i32, i32* %11
store i32 %79, i32* @errno
store i32 -1, i32* %4
br label %80
80: ; preds = %78, %70
%81 = load i32, i32* %4
ret i32 %81
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i8* }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"unsocket: socket: %s\0A\00"
@AF_UNIX = global i8* null
@.str.1 = [23 x i8] c"unsocket: bind %s: %s\0A\00"
@.str.2 = [26 x i8] c"unsocket: connect %s: %s\0A\00"
define i32 @unsocket(i32 %0, i8* %1, i8* %2) {
%4 = alloca %struct.sockaddr_un
%5 = alloca %struct.sockaddr_un
%6 = bitcast %struct.sockaddr_un* %4 to i8*
%7 = bitcast %struct.sockaddr_un* %5 to i8*
%8 = call i32 @memset(%struct.sockaddr_un* %4, i32 0, i32 16)
%9 = load i32, i32* @PF_UNIX
%10 = call i32 @socket(i32 %9, i32 %0, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %47, label %12
12: ; preds = %3
%13 = icmp eq i8* %1, null
br i1 %13, label %30, label %14
14: ; preds = %12
%15 = call i32 @memset(%struct.sockaddr_un* %5, i32 0, i32 16)
%16 = load i8*, i8** @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i64 0, i32 1
store i8* %16, i8** %17
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = call i32 @strncpy(i8* %19, i8* %1, i32 7)
%21 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%22 = call i32 @bind(i32 %10, %struct.sockaddr* %21, i32 16)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %30
24: ; preds = %14
%25 = load i32, i32* @errno
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %18
%28 = call i8* @strerror(i32 %25)
%29 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* %28)
br label %52
30: ; preds = %14, %12
%31 = icmp eq i8* %2, null
br i1 %31, label %57, label %32
32: ; preds = %30
%33 = load i8*, i8** @AF_UNIX
%34 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 1
store i8* %33, i8** %34
%35 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 0
%36 = load i8*, i8** %35
%37 = call i32 @strncpy(i8* %36, i8* %2, i32 7)
%38 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%39 = call i32 @connect(i32 %10, %struct.sockaddr* %38, i32 16)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %57
41: ; preds = %32
%42 = load i32, i32* @errno
%43 = load i32, i32* @stderr
%44 = load i8*, i8** %35
%45 = call i8* @strerror(i32 %42)
%46 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %44, i8* %45)
br label %52
47: ; preds = %3
%48 = load i32, i32* @errno
%49 = load i32, i32* @stderr
%50 = call i8* @strerror(i32 %48)
%51 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %50)
br label %55
52: ; preds = %41, %24
%53 = phi i32 [ %42, %41 ], [ %25, %24 ]
%54 = call i32 @close(i32 %10)
br label %55
55: ; preds = %47, %52
%56 = phi i32 [ %53, %52 ], [ %48, %47 ]
store i32 %56, i32* @errno
br label %57
57: ; preds = %30, %32, %55
%58 = phi i32 [ -1, %55 ], [ %10, %32 ], [ %10, %30 ]
ret i32 %58
}