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

/* Variables and functions */
int O_CLOEXEC;
int O_NOFOLLOW;
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ _shm_get_path(char *, int, char const *);
int open(char *, int, int /*<<< orphan*/);
int shm_open(const char *name, int oflag, mode_t mode) {
  char path[PATH_MAX + 1];
  if (!_shm_get_path(path, sizeof(path), name)) {
    return -1;
  }
  return open(path, oflag | O_CLOEXEC | O_NOFOLLOW, mode);
}


x86 assembly (clang, -O0):
<shm_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    0x2eaf(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
call   1040 <_shm_get_path@plt>
cmp    $0x0,%eax
jne    1188 <shm_open+0x68>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    11b3 <shm_open+0x93>
mov    -0x38(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e32(%rip),%rax
or     (%rax),%esi
mov    0x2e59(%rip),%rax
or     (%rax),%esi
mov    -0x18(%rbp),%edx
call   1030 <open@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shm_open>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%rdx
mov    0x2eae(%rip),%rax
mov    (%rax),%esi
add    $0x1,%esi
mov    %rsp,%rbx
lea    0xf(%rsi),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
call   1040 <_shm_get_path@plt>
test   %eax,%eax
je     1180 <shm_open+0x60>
mov    0x2e65(%rip),%rax
or     (%rax),%r15d
mov    0x2e8b(%rip),%rax
or     (%rax),%r15d
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14d,%edx
call   1030 <open@plt>
jmp    1185 <shm_open+0x65>
mov    $0xffffffff,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@O_CLOEXEC = global i32 0
@O_NOFOLLOW = 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 i8*
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%11 = load i32, i32* @PATH_MAX
%12 = add nsw i32 %11, 1
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %8
%15 = alloca i8, i64 %13
store i64 %13, i64* %9
%16 = trunc i64 %13 to i32
%17 = load i8*, i8** %5
%18 = call i32 @_shm_get_path(i8* %15, i32 %16, i8* %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %3
store i32 -1, i32* %4
store i32 1, i32* %10
br label %29
21: ; preds = %3
%22 = load i32, i32* %6
%23 = load i32, i32* @O_CLOEXEC
%24 = or i32 %22, %23
%25 = load i32, i32* @O_NOFOLLOW
%26 = or i32 %24, %25
%27 = load i32, i32* %7
%28 = call i32 @open(i8* %15, i32 %26, i32 %27)
store i32 %28, i32* %4
store i32 1, i32* %10
br label %29
29: ; preds = %21, %20
%30 = load i8*, i8** %8
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@O_CLOEXEC = global i32 0
@O_NOFOLLOW = global i32 0
define i32 @shm_open(i8* %0, i32 %1, i32 %2) {
%4 = load i32, i32* @PATH_MAX
%5 = add nsw i32 %4, 1
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = call i32 @_shm_get_path(i8* %7, i32 %5, i8* %0)
%9 = icmp eq i32 %8, 0
br i1 %9, label %16, label %10
10: ; preds = %3
%11 = load i32, i32* @O_CLOEXEC
%12 = or i32 %11, %1
%13 = load i32, i32* @O_NOFOLLOW
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %7, i32 %14, i32 %2)
br label %16
16: ; preds = %3, %10
%17 = phi i32 [ %15, %10 ], [ -1, %3 ]
ret i32 %17
}