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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int ENAMETOOLONG;
int O_TRUNC;
int /*<<< orphan*/ PF_UNIX;
int SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ listen(int, int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, char const *, size_t);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
size_t strlen(char const *);
int /*<<< orphan*/ unlink(char const *);
int lxc_af_unix_open(const char *path, int type, int flags) {
  int fd;
  size_t len;
  struct sockaddr_un addr;
  if (flags & O_TRUNC)
    unlink(path);
  fd = socket(PF_UNIX, type, 0);
  if (fd < 0)
    return -1;
  memset(&addr, 0, sizeof(addr));
  if (!path)
    return fd;
  addr.sun_family = AF_UNIX;
  len = sizeof(addr.sun_path);
  if (path[0]) {
    len = strlen(path);
    if (len >= sizeof(addr.sun_path)) {
      close(fd);
      errno = ENAMETOOLONG;
      return -1;
    }
  }
  memcpy(addr.sun_path, path, len);
  if (bind(fd, (struct sockaddr *)&addr, sizeof(addr))) {
    int tmp = errno;
    close(fd);
    errno = tmp;
    return -1;
  }
  if (type == SOCK_STREAM && listen(fd, 100)) {
    int tmp = errno;
    close(fd);
    errno = tmp;
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<lxc_af_unix_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2e54(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11b0 <lxc_af_unix_open+0x30>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11dc <lxc_af_unix_open+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1318 <lxc_af_unix_open+0x198>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1202 <lxc_af_unix_open+0x82>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1318 <lxc_af_unix_open+0x198>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movq   $0x4,-0x28(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1266 <lxc_af_unix_open+0xe6>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x4,-0x28(%rbp)
jb     1261 <lxc_af_unix_open+0xe1>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%ecx
mov    0x2d6d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1318 <lxc_af_unix_open+0x198>
jmp    1266 <lxc_af_unix_open+0xe6>
mov    -0x30(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
je     12bd <lxc_af_unix_open+0x13d>
mov    0x2d28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x34(%rbp),%ecx
mov    0x2d11(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1318 <lxc_af_unix_open+0x198>
mov    -0x14(%rbp),%eax
mov    0x2d01(%rip),%rcx
cmp    (%rcx),%eax
jne    1312 <lxc_af_unix_open+0x192>
mov    -0x1c(%rbp),%edi
mov    $0x64,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
je     1312 <lxc_af_unix_open+0x192>
mov    0x2cd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%ecx
mov    0x2cbc(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1318 <lxc_af_unix_open+0x198>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lxc_af_unix_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%rbx
mov    0x2e5c(%rip),%rax
test   %edx,(%rax)
je     11a0 <lxc_af_unix_open+0x20>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    %r15d,%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     126f <lxc_af_unix_open+0xef>
mov    %eax,%ebp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
test   %rbx,%rbx
je     126c <lxc_af_unix_open+0xec>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
cmpb   $0x0,(%rbx)
je     1210 <lxc_af_unix_open+0x90>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%rdx
cmp    $0x4,%rax
jb     1215 <lxc_af_unix_open+0x95>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%ebx
jmp    1244 <lxc_af_unix_open+0xc4>
mov    $0x4,%edx
mov    (%rsp),%edi
mov    %rbx,%rsi
call   1070 <memcpy@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1090 <bind@plt>
test   %rax,%rax
je     124f <lxc_af_unix_open+0xcf>
mov    0x2d85(%rip),%rax
mov    (%rax),%ebx
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2d75(%rip),%rax
mov    %ebx,(%rax)
jmp    126f <lxc_af_unix_open+0xef>
mov    0x2d72(%rip),%rax
cmp    %r15d,(%rax)
jne    126c <lxc_af_unix_open+0xec>
mov    %ebp,%edi
mov    $0x64,%esi
call   1080 <listen@plt>
test   %rax,%rax
jne    1234 <lxc_af_unix_open+0xb4>
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@O_TRUNC = global i32 0
@PF_UNIX = global i32 0
@AF_UNIX = global i32 0
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@SOCK_STREAM = global i32 0
define i32 @lxc_af_unix_open(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_un
%5 = bitcast %struct.sockaddr_un* %4 to i8*
%6 = load i32, i32* @O_TRUNC
%7 = and i32 %6, %2
%8 = icmp eq i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = tail call i32 @unlink(i8* %0)
br label %11
11: ; preds = %9, %3
%12 = load i32, i32* @PF_UNIX
%13 = tail call i32 @socket(i32 %12, i32 %1, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %51, label %15
15: ; preds = %11
%16 = call i32 @memset(%struct.sockaddr_un* %4, i32 0, i32 8)
%17 = icmp eq i8* %0, null
br i1 %17, label %51, label %18
18: ; preds = %15
%19 = load i32, i32* @AF_UNIX
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 1
store i32 %19, i32* %20
%21 = load i8, i8* %0
%22 = icmp eq i8 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %18
%24 = call i64 @strlen(i8* %0)
%25 = icmp ugt i64 %24, 3
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = call i32 @close(i32 %13)
%28 = load i32, i32* @ENAMETOOLONG
br label %49
29: ; preds = %23, %18
%30 = phi i64 [ %24, %23 ], [ 4, %18 ]
%31 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @memcpy(i32 %32, i8* %0, i64 %30)
%34 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%35 = call i64 @bind(i32 %13, %struct.sockaddr* %34, i32 8)
%36 = icmp eq i64 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %29
%38 = load i32, i32* @errno
%39 = call i32 @close(i32 %13)
br label %49
40: ; preds = %29
%41 = load i32, i32* @SOCK_STREAM
%42 = icmp eq i32 %41, %1
br i1 %42, label %43, label %51
43: ; preds = %40
%44 = call i64 @listen(i32 %13, i32 100)
%45 = icmp eq i64 %44, 0
br i1 %45, label %51, label %46
46: ; preds = %43
%47 = load i32, i32* @errno
%48 = call i32 @close(i32 %13)
br label %49
49: ; preds = %26, %37, %46
%50 = phi i32 [ %47, %46 ], [ %38, %37 ], [ %28, %26 ]
store i32 %50, i32* @errno
br label %51
51: ; preds = %49, %40, %43, %15, %11
%52 = phi i32 [ -1, %11 ], [ %13, %15 ], [ %13, %43 ], [ %13, %40 ], [ -1, %49 ]
ret i32 %52
}