#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 O_TRUNC;
int /*<<< orphan*/ PF_UNIX;
int SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, char const *, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int strlen(char const *);
int /*<<< orphan*/ unlink(char const *);
int lxc_af_unix_open(const char *path, int type, int flags) {
  int fd;
  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;
  memcpy(addr.sun_path, path, path[0] ? strlen(path) : sizeof(addr.sun_path));
  if (bind(fd, (struct sockaddr *)&addr, sizeof(addr))) {
    close(fd);
    return -1;
  }
  if (type == SOCK_STREAM && listen(fd, 100)) {
    close(fd);
    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    12d2 <lxc_af_unix_open+0x152>
lea    -0x28(%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    12d2 <lxc_af_unix_open+0x152>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1240 <lxc_af_unix_open+0xc0>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cltq
mov    %rax,-0x40(%rbp)
jmp    124e <lxc_af_unix_open+0xce>
mov    $0x4,%eax
mov    %rax,-0x40(%rbp)
jmp    124e <lxc_af_unix_open+0xce>
mov    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
je     128f <lxc_af_unix_open+0x10f>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <lxc_af_unix_open+0x152>
mov    -0x14(%rbp),%eax
mov    0x2d2f(%rip),%rcx
cmp    (%rcx),%eax
jne    12cc <lxc_af_unix_open+0x14c>
mov    -0x1c(%rbp),%edi
mov    $0x64,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
je     12cc <lxc_af_unix_open+0x14c>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <lxc_af_unix_open+0x152>
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   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e57(%rip),%rax
test   %edx,(%rax)
je     11a5 <lxc_af_unix_open+0x25>
mov    %rbp,%rdi
call   1030 <unlink@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
mov    %r14d,%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     1251 <lxc_af_unix_open+0xd1>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
test   %rbp,%rbp
je     124e <lxc_af_unix_open+0xce>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%r12d
cmpb   $0x0,0x0(%rbp)
je     1202 <lxc_af_unix_open+0x82>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %eax,%edx
jmp    1207 <lxc_af_unix_open+0x87>
mov    $0x4,%edx
mov    %r12d,%edi
mov    %rbp,%rsi
call   1070 <memcpy@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1090 <bind@plt>
test   %rax,%rax
je     1231 <lxc_af_unix_open+0xb1>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1251 <lxc_af_unix_open+0xd1>
mov    0x2d90(%rip),%rax
cmp    %r14d,(%rax)
jne    124e <lxc_af_unix_open+0xce>
mov    %ebx,%edi
mov    $0x64,%esi
call   1080 <listen@plt>
test   %rax,%rax
jne    1228 <lxc_af_unix_open+0xa8>
mov    %ebx,%r15d
mov    %r15d,%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 }
@O_TRUNC = global i32 0
@PF_UNIX = global i32 0
@AF_UNIX = 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 %struct.sockaddr_un
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %7
%11 = load i32, i32* @O_TRUNC
%12 = and i32 %10, %11
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = call i32 @unlink(i8* %15)
br label %17
17: ; preds = %14, %3
%18 = load i32, i32* @PF_UNIX
%19 = load i32, i32* %6
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %17
store i32 -1, i32* %4
br label %70
24: ; preds = %17
%25 = call i32 @memset(%struct.sockaddr_un* %9, i32 0, i32 8)
%26 = load i8*, i8** %5
%27 = icmp ne i8* %26, null
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = load i32, i32* %8
store i32 %29, i32* %4
br label %70
30: ; preds = %24
%31 = load i32, i32* @AF_UNIX
%32 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 1
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%34 = load i32, i32* %33
%35 = load i8*, i8** %5
%36 = load i8*, i8** %5
%37 = getelementptr inbounds i8, i8* %36, i64 0
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %30
%42 = load i8*, i8** %5
%43 = call i32 @strlen(i8* %42)
%44 = sext i32 %43 to i64
br label %46
45: ; preds = %30
br label %46
46: ; preds = %45, %41
%47 = phi i64 [ %44, %41 ], [ 4, %45 ]
%48 = trunc i64 %47 to i32
%49 = call i32 @memcpy(i32 %34, i8* %35, i32 %48)
%50 = load i32, i32* %8
%51 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%52 = call i64 @bind(i32 %50, %struct.sockaddr* %51, i32 8)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %46
%55 = load i32, i32* %8
%56 = call i32 @close(i32 %55)
store i32 -1, i32* %4
br label %70
57: ; preds = %46
%58 = load i32, i32* %6
%59 = load i32, i32* @SOCK_STREAM
%60 = icmp eq i32 %58, %59
br i1 %60, label %61, label %68
61: ; preds = %57
%62 = load i32, i32* %8
%63 = call i64 @listen(i32 %62, i32 100)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %61
%66 = load i32, i32* %8
%67 = call i32 @close(i32 %66)
store i32 -1, i32* %4
br label %70
68: ; preds = %61, %57
%69 = load i32, i32* %8
store i32 %69, i32* %4
br label %70
70: ; preds = %68, %65, %54, %28, %23
%71 = load i32, i32* %4
ret i32 %71
}

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
@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 %41, 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 %41, 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 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 0
%22 = load i32, i32* %21
%23 = load i8, i8* %0
%24 = icmp eq i8 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %18
%26 = call i32 @strlen(i8* %0)
br label %27
27: ; preds = %18, %25
%28 = phi i32 [ %26, %25 ], [ 4, %18 ]
%29 = call i32 @memcpy(i32 %22, i8* %0, i32 %28)
%30 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%31 = call i64 @bind(i32 %13, %struct.sockaddr* %30, i32 8)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %27
%34 = load i32, i32* @SOCK_STREAM
%35 = icmp eq i32 %34, %1
br i1 %35, label %36, label %41
36: ; preds = %33
%37 = call i64 @listen(i32 %13, i32 100)
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %36, %27
%40 = call i32 @close(i32 %13)
br label %41
41: ; preds = %39, %33, %36, %15, %11
%42 = phi i32 [ -1, %11 ], [ %13, %15 ], [ %13, %36 ], [ %13, %33 ], [ -1, %39 ]
ret i32 %42
}