#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*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
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);
  }
  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   1040 <get_shm_name@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1176 <shm_open+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bb <shm_open+0x7b>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11ac <shm_open+0x6c>
mov    -0x1c(%rbp),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
call   1050 <fcntl@plt>
mov    -0x28(%rbp),%rdi
call   1030 <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   1040 <get_shm_name@plt>
test   %rax,%rax
je     118c <shm_open+0x4c>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r14d,%edx
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1182 <shm_open+0x42>
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1050 <fcntl@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1191 <shm_open+0x51>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@F_SETFD = global i32 0
@FD_CLOEXEC = 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 %31
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 i8*, i8** %9
%29 = call i32 @free(i8* %28)
%30 = load i32, i32* %8
store i32 %30, i32* %4
br label %31
31: ; preds = %27, %14
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@F_SETFD = global i32 0
@FD_CLOEXEC = 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 %15, 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 %13
13: ; preds = %9, %6
%14 = tail call i32 @free(i8* %4)
br label %15
15: ; preds = %3, %13
%16 = phi i32 [ %7, %13 ], [ -1, %3 ]
ret i32 %16
}