#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*/ NAME_MAX;
int O_CLOEXEC;
int O_NOFOLLOW;
int O_NONBLOCK;
int PTHREAD_CANCEL_DISABLE;
char *__shm_mapname(char const *, char *);
int open(char const *, int, int /*<<< orphan*/);
int /*<<< orphan*/ pthread_setcancelstate(int, int *);
int shm_open(const char *name, int flag, mode_t mode) {
  int cs;
  char buf[NAME_MAX + 10];
  if (!(name = __shm_mapname(name, buf)))
    return -1;
  pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
  int fd = open(name, flag | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK, mode);
  pthread_setcancelstate(cs, 0);
  return fd;
}


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    0x2e6f(%rip),%rax
mov    (%rax),%eax
add    $0xa,%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <__shm_mapname@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1197 <shm_open+0x67>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    11ef <shm_open+0xbf>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    -0x1c(%rbp),%rsi
call   1030 <pthread_setcancelstate@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e41(%rip),%rax
or     (%rax),%esi
mov    0x2e00(%rip),%rax
or     (%rax),%esi
mov    0x2e17(%rip),%rax
or     (%rax),%esi
mov    -0x18(%rbp),%edx
call   1050 <open@plt>
mov    %eax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <pthread_setcancelstate@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x28(%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   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %esi,%ebx
mov    %rsp,%r12
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
add    $0xa,%eax
mov    %rsp,%rsi
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rsi
mov    %rsi,%rsp
call   1040 <__shm_mapname@plt>
test   %rax,%rax
je     11b9 <shm_open+0x89>
mov    %rax,%r15
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
call   1030 <pthread_setcancelstate@plt>
mov    0x2e6e(%rip),%rax
or     (%rax),%ebx
mov    0x2e2d(%rip),%rax
or     (%rax),%ebx
mov    0x2e44(%rip),%rax
or     (%rax),%ebx
mov    %r15,%rdi
mov    %ebx,%esi
mov    %r14d,%edx
call   1050 <open@plt>
mov    %eax,%ebx
mov    -0x24(%rbp),%edi
xor    %esi,%esi
call   1030 <pthread_setcancelstate@plt>
jmp    11be <shm_open+0x8e>
mov    $0xffffffff,%ebx
mov    %r12,%rsp
mov    %ebx,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@NAME_MAX = global i32 0
@PTHREAD_CANCEL_DISABLE = 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 = bitcast i32* %4 to i8*
%6 = load i32, i32* @NAME_MAX
%7 = add nsw i32 %6, 10
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = call i8* @__shm_mapname(i8* %0, i8* %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %25, label %13
13: ; preds = %3
%14 = load i32, i32* @PTHREAD_CANCEL_DISABLE
%15 = call i32 @pthread_setcancelstate(i32 %14, i32* %4)
%16 = load i32, i32* @O_NOFOLLOW
%17 = or i32 %16, %1
%18 = load i32, i32* @O_CLOEXEC
%19 = or i32 %17, %18
%20 = load i32, i32* @O_NONBLOCK
%21 = or i32 %19, %20
%22 = call i32 @open(i8* %11, i32 %21, i32 %2)
%23 = load i32, i32* %4
%24 = call i32 @pthread_setcancelstate(i32 %23, i32* null)
br label %25
25: ; preds = %3, %13
%26 = phi i32 [ %22, %13 ], [ -1, %3 ]
ret i32 %26
}