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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int O_DIRECTORY;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char *, int);
scalar_t__ strchr(char const *, char);
int unlinkat(int, char const *, int /*<<< orphan*/);
int shm_unlink(const char *name) {
  int dir, ret;
  while (*name == '/')
    name++;
  if (strchr(name, '/')) {
    errno = EINVAL;
    return -1;
  }
  if ((dir = open("/dev/shm", O_DIRECTORY | O_RDONLY)) < 0)
    return -1;
  ret = unlinkat(dir, name, 0);
  close(dir);
  return ret;
}


x86 assembly (clang, -O0):
<shm_unlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    116d <shm_unlink+0x2d>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    114c <shm_unlink+0xc>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     11a3 <shm_unlink+0x63>
mov    0x2e3c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11f8 <shm_unlink+0xb8>
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    0x2e35(%rip),%rax
or     (%rax),%esi
lea    0xe44(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11d9 <shm_unlink+0x99>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f8 <shm_unlink+0xb8>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1030 <unlinkat@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shm_unlink>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
add    $0xffffffffffffffff,%rbx
nopw   0x0(%rax,%rax,1)
cmpb   $0x2f,0x1(%rbx)
lea    0x1(%rbx),%rbx
je     1150 <shm_unlink+0x10>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1185 <shm_unlink+0x45>
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    0x2e54(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    11c0 <shm_unlink+0x80>
mov    0x2e4c(%rip),%rax
mov    0x2e55(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe62(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     117e <shm_unlink+0x3e>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <unlinkat@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [9 x i8] c"/dev/shm\00"
@O_DIRECTORY = global i32 0
@O_RDONLY = global i32 0
define i32 @shm_unlink(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
br label %6
6: ; preds = %11, %1
%7 = load i8*, i8** %3
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp eq i32 %9, 47
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = load i8*, i8** %3
%13 = getelementptr inbounds i8, i8* %12, i32 1
store i8* %13, i8** %3
br label %6
14: ; preds = %6
%15 = load i8*, i8** %3
%16 = call i64 @strchr(i8* %15, i8 signext 47)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = load i32, i32* @EINVAL
store i32 %19, i32* @errno
store i32 -1, i32* %2
br label %34
20: ; preds = %14
%21 = load i32, i32* @O_DIRECTORY
%22 = load i32, i32* @O_RDONLY
%23 = or i32 %21, %22
%24 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %23)
store i32 %24, i32* %4
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 -1, i32* %2
br label %34
27: ; preds = %20
%28 = load i32, i32* %4
%29 = load i8*, i8** %3
%30 = call i32 @unlinkat(i32 %28, i8* %29, i32 0)
store i32 %30, i32* %5
%31 = load i32, i32* %4
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %5
store i32 %33, i32* %2
br label %34
34: ; preds = %27, %26, %18
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [9 x i8] c"/dev/shm\00"
@O_DIRECTORY = global i32 0
@O_RDONLY = global i32 0
define i32 @shm_unlink(i8* %0) {
br label %2
2: ; preds = %2, %1
%3 = phi i8* [ %0, %1 ], [ %6, %2 ]
%4 = load i8, i8* %3
%5 = icmp eq i8 %4, 47
%6 = getelementptr inbounds i8, i8* %3, i64 1
br i1 %5, label %2, label %7
7: ; preds = %2
%8 = tail call i64 @strchr(i8* %3, i8 signext 47)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = load i32, i32* @EINVAL
store i32 %11, i32* @errno
br label %21
12: ; preds = %7
%13 = load i32, i32* @O_DIRECTORY
%14 = load i32, i32* @O_RDONLY
%15 = or i32 %14, %13
%16 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %12
%19 = tail call i32 @unlinkat(i32 %16, i8* %3, i32 0)
%20 = tail call i32 @close(i32 %16)
br label %21
21: ; preds = %12, %18, %10
%22 = phi i32 [ -1, %10 ], [ %19, %18 ], [ -1, %12 ]
ret i32 %22
}