#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;
scalar_t__ EISDIR;
int /*<<< orphan*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *get_shm_name(char const *);
int open(char *, int, int /*<<< orphan*/);
int shm_open(const char *name, int oflag, mode_t mode) {
  int fd;
  char *shm_name = get_shm_name(name);
  if (shm_name == NULL)
    return -1;
  fd = open(shm_name, oflag, mode);
  if (fd >= 0) {
    fcntl(fd, F_SETFD, FD_CLOEXEC);
  }
  if (fd < 0 && errno == EISDIR)
    __set_errno(EINVAL);
  free(shm_name);
  return fd;
}


x86 assembly (clang, -O0):
<shm_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <get_shm_name@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1186 <shm_open+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fd <shm_open+0xad>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11bc <shm_open+0x6c>
mov    -0x1c(%rbp),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
call   1060 <fcntl@plt>
cmpl   $0x0,-0x1c(%rbp)
jge    11ee <shm_open+0x9e>
mov    0x2dfb(%rip),%rax
mov    (%rax),%rax
mov    0x2e11(%rip),%rcx
cmp    (%rcx),%rax
jne    11ee <shm_open+0x9e>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shm_open>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebp
call   1050 <get_shm_name@plt>
test   %rax,%rax
je     1194 <shm_open+0x44>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r14d,%edx
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     119b <shm_open+0x4b>
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1060 <fcntl@plt>
jmp    11bf <shm_open+0x6f>
mov    $0xffffffff,%ebp
jmp    11c7 <shm_open+0x77>
mov    0x2e26(%rip),%rax
mov    (%rax),%rax
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%rax
jne    11bf <shm_open+0x6f>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@errno = global i64 0
@EISDIR = global i64 0
@EINVAL = 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*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = call i8* @get_shm_name(i8* %10)
store i8* %11, i8** %9
%12 = load i8*, i8** %9
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %41
15: ; preds = %3
%16 = load i8*, i8** %9
%17 = load i32, i32* %6
%18 = load i32, i32* %7
%19 = call i32 @open(i8* %16, i32 %17, i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp sge i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %15
%23 = load i32, i32* %8
%24 = load i32, i32* @F_SETFD
%25 = load i32, i32* @FD_CLOEXEC
%26 = call i32 @fcntl(i32 %23, i32 %24, i32 %25)
br label %27
27: ; preds = %22, %15
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %27
%31 = load i64, i64* @errno
%32 = load i64, i64* @EISDIR
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32, i32* @EINVAL
%36 = call i32 @__set_errno(i32 %35)
br label %37
37: ; preds = %34, %30, %27
%38 = load i8*, i8** %9
%39 = call i32 @free(i8* %38)
%40 = load i32, i32* %8
store i32 %40, i32* %4
br label %41
41: ; preds = %37, %14
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@errno = global i64 0
@EISDIR = global i64 0
@EINVAL = global i32 0
define i32 @shm_open(i8* %0, i32 %1, i32 %2) {
%4 = tail call i8* @get_shm_name(i8* %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %22, label %6
6: ; preds = %3
%7 = tail call i32 @open(i8* %4, i32 %1, i32 %2)
%8 = icmp sgt i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %6
%10 = load i32, i32* @F_SETFD
%11 = load i32, i32* @FD_CLOEXEC
%12 = tail call i32 @fcntl(i32 %7, i32 %10, i32 %11)
br label %20
13: ; preds = %6
%14 = load i64, i64* @errno
%15 = load i64, i64* @EISDIR
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* @EINVAL
%19 = tail call i32 @__set_errno(i32 %18)
br label %20
20: ; preds = %9, %17, %13
%21 = tail call i32 @free(i8* %4)
br label %22
22: ; preds = %3, %20
%23 = phi i32 [ %7, %20 ], [ -1, %3 ]
ret i32 %23
}