#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 FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ close(int);
int errno;
int fcntl(int, int /*<<< orphan*/, int);
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, old_errno;
  char *shm_name = get_shm_name(name);
  if (shm_name == NULL)
    return -1;
  fd = open(shm_name, oflag, mode);
  if (fd >= 0) {
    int fdflags = fcntl(fd, F_GETFD, 0);
    if (fdflags >= 0)
      fdflags = fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
    if (fdflags < 0) {
      close(fd);
      fd = -1;
    }
  }
  old_errno = errno;
  free(shm_name);
  errno = old_errno;
  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   1040 <get_shm_name@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1186 <shm_open+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <shm_open+0xd7>
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     1200 <shm_open+0xb0>
mov    -0x1c(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     11e2 <shm_open+0x92>
mov    -0x1c(%rbp),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    -0x2c(%rbp),%edx
mov    0x2e00(%rip),%rax
or     (%rax),%edx
call   1050 <fcntl@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11fb <shm_open+0xab>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    1200 <shm_open+0xb0>
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%ecx
mov    0x2db1(%rip),%rax
mov    %ecx,(%rax)
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   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%ebx
call   1040 <get_shm_name@plt>
test   %rax,%rax
je     11d2 <shm_open+0x82>
mov    %rax,%r14
mov    %rax,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11bb <shm_open+0x6b>
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
test   %eax,%eax
js     11af <shm_open+0x5f>
mov    0x2e49(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e38(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %eax,%edx
call   1050 <fcntl@plt>
test   %eax,%eax
jns    11bb <shm_open+0x6b>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebp
mov    0x2e0e(%rip),%r15
mov    (%r15),%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,(%r15)
jmp    11d7 <shm_open+0x87>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@errno = 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 %25, 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 %21
9: ; preds = %6
%10 = load i32, i32* @F_GETFD
%11 = tail call i32 @fcntl(i32 %7, i32 %10, i32 0)
%12 = icmp sgt i32 %11, -1
br i1 %12, label %13, label %19
13: ; preds = %9
%14 = load i32, i32* @F_SETFD
%15 = load i32, i32* @FD_CLOEXEC
%16 = or i32 %15, %11
%17 = tail call i32 @fcntl(i32 %7, i32 %14, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %9, %13
%20 = tail call i32 @close(i32 %7)
br label %21
21: ; preds = %13, %19, %6
%22 = phi i32 [ %7, %6 ], [ -1, %19 ], [ %7, %13 ]
%23 = load i32, i32* @errno
%24 = tail call i32 @free(i8* %4)
store i32 %23, i32* @errno
br label %25
25: ; preds = %3, %21
%26 = phi i32 [ %22, %21 ], [ -1, %3 ]
ret i32 %26
}