#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 */
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int O_CLOEXEC;
int O_DIRECTORY;
int O_NOFOLLOW;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char *, int);
int openat(int, char const *, int, int /*<<< orphan*/);
scalar_t__ strchr(char const *, char);
int shm_open(const char *name, int flag, mode_t mode) {
  int fd, dir;
  while (*name == '/')
    name++;
  if (strchr(name, '/')) {
    errno = EINVAL;
    return -1;
  }
  if ((dir = open("/dev/shm", O_DIRECTORY | O_RDONLY)) < 0)
    return -1;
  fd = openat(dir, name, flag | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK, mode);
  close(dir);
  return fd;
}


x86 assembly (clang, -O0):
<shm_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1173 <shm_open+0x33>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1152 <shm_open+0x12>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     11a9 <shm_open+0x69>
mov    0x2e26(%rip),%rax
mov    (%rax),%ecx
mov    0x2e25(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <shm_open+0xdd>
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    0x2e1f(%rip),%rax
or     (%rax),%esi
lea    0xe3e(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11df <shm_open+0x9f>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <shm_open+0xdd>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    0x2e08(%rip),%rax
or     (%rax),%edx
mov    0x2daf(%rip),%rax
or     (%rax),%edx
mov    0x2dde(%rip),%rax
or     (%rax),%edx
mov    -0x18(%rbp),%ecx
call   1030 <openat@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shm_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
add    $0xffffffffffffffff,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmpb   $0x2f,0x1(%rbx)
lea    0x1(%rbx),%rbx
je     1160 <shm_open+0x20>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1195 <shm_open+0x55>
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    0x2e34(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    11f1 <shm_open+0xb1>
mov    0x2e2c(%rip),%rax
mov    0x2e35(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe52(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     118e <shm_open+0x4e>
mov    %eax,%r15d
mov    0x2e37(%rip),%rax
or     (%rax),%ebp
mov    0x2dde(%rip),%rax
or     (%rax),%ebp
mov    0x2e0d(%rip),%rax
or     (%rax),%ebp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
mov    %r14d,%ecx
call   1030 <openat@plt>
mov    %eax,%ebx
mov    %r15d,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
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
@O_NOFOLLOW = global i32 0
@O_CLOEXEC = global i32 0
@O_NONBLOCK = global i32 0
define i32 @shm_open(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
br label %10
10: ; preds = %15, %3
%11 = load i8*, i8** %5
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 47
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i8*, i8** %5
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %5
br label %10
18: ; preds = %10
%19 = load i8*, i8** %5
%20 = call i64 @strchr(i8* %19, i8 signext 47)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = load i32, i32* @EINVAL
store i32 %23, i32* @errno
store i32 -1, i32* %4
br label %46
24: ; preds = %18
%25 = load i32, i32* @O_DIRECTORY
%26 = load i32, i32* @O_RDONLY
%27 = or i32 %25, %26
%28 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %27)
store i32 %28, i32* %9
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24
store i32 -1, i32* %4
br label %46
31: ; preds = %24
%32 = load i32, i32* %9
%33 = load i8*, i8** %5
%34 = load i32, i32* %6
%35 = load i32, i32* @O_NOFOLLOW
%36 = or i32 %34, %35
%37 = load i32, i32* @O_CLOEXEC
%38 = or i32 %36, %37
%39 = load i32, i32* @O_NONBLOCK
%40 = or i32 %38, %39
%41 = load i32, i32* %7
%42 = call i32 @openat(i32 %32, i8* %33, i32 %40, i32 %41)
store i32 %42, i32* %8
%43 = load i32, i32* %9
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %8
store i32 %45, i32* %4
br label %46
46: ; preds = %31, %30, %22
%47 = load i32, i32* %4
ret i32 %47
}

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
@O_NOFOLLOW = global i32 0
@O_CLOEXEC = global i32 0
@O_NONBLOCK = global i32 0
define i32 @shm_open(i8* %0, i32 %1, i32 %2) {
br label %4
4: ; preds = %4, %3
%5 = phi i8* [ %0, %3 ], [ %8, %4 ]
%6 = load i8, i8* %5
%7 = icmp eq i8 %6, 47
%8 = getelementptr inbounds i8, i8* %5, i64 1
br i1 %7, label %4, label %9
9: ; preds = %4
%10 = tail call i64 @strchr(i8* %5, i8 signext 47)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = load i32, i32* @EINVAL
store i32 %13, i32* @errno
br label %29
14: ; preds = %9
%15 = load i32, i32* @O_DIRECTORY
%16 = load i32, i32* @O_RDONLY
%17 = or i32 %16, %15
%18 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %29, label %20
20: ; preds = %14
%21 = load i32, i32* @O_NOFOLLOW
%22 = or i32 %21, %1
%23 = load i32, i32* @O_CLOEXEC
%24 = or i32 %22, %23
%25 = load i32, i32* @O_NONBLOCK
%26 = or i32 %24, %25
%27 = tail call i32 @openat(i32 %18, i8* %5, i32 %26, i32 %2)
%28 = tail call i32 @close(i32 %18)
br label %29
29: ; preds = %14, %20, %12
%30 = phi i32 [ -1, %12 ], [ %27, %20 ], [ -1, %14 ]
ret i32 %30
}