#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 */

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int O_RDWR;
int S_IREAD;
int S_IRGRP;
int S_IRUSR;
int S_IWGRP;
int S_IWRITE;
int S_IWUSR;
int fchmod(int, int);
int fchown(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int shm_open(char *, int, int);
int kattach_thedawn_shm_open(char *kattach_shm_path) {
  int kattach_shm_fd = 0;
  int new = 0, res = 0;
  if ((kattach_shm_fd = shm_open(kattach_shm_path, (O_CREAT | O_EXCL | O_RDWR),
                                 (S_IREAD | S_IWRITE))) > 0) {
    new = 1;
  } else if ((kattach_shm_fd = shm_open(kattach_shm_path, (O_CREAT | O_RDWR),
                                        (S_IREAD | S_IWRITE))) < 0) {
    printf("\n [!] Unexpected SHM Error - %s\n", kattach_shm_path);
    return 0;
  }
  if (!new) {
    printf("\n [X] Unexpected SHM State - %s\n", kattach_shm_path);
  }
  res = fchown(kattach_shm_fd, 0, 77);
  res = fchmod(kattach_shm_fd, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
  return (kattach_shm_fd);
}


x86 assembly (clang, -O0):
<kattach_thedawn_shm_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
mov    0x2e43(%rip),%rax
or     (%rax),%esi
mov    0x2e62(%rip),%rax
or     (%rax),%esi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
mov    0x2e40(%rip),%rax
or     (%rax),%edx
call   1060 <shm_open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    11af <kattach_thedawn_shm_open+0x6f>
movl   $0x1,-0x18(%rbp)
jmp    1209 <kattach_thedawn_shm_open+0xc9>
mov    -0x10(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    0x2e1d(%rip),%rax
or     (%rax),%esi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
mov    0x2dfb(%rip),%rax
or     (%rax),%edx
call   1060 <shm_open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1204 <kattach_thedawn_shm_open+0xc4>
mov    -0x10(%rbp),%rsi
lea    0xe0d(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    126a <kattach_thedawn_shm_open+0x12a>
jmp    1209 <kattach_thedawn_shm_open+0xc9>
cmpl   $0x0,-0x18(%rbp)
jne    1223 <kattach_thedawn_shm_open+0xe3>
mov    -0x10(%rbp),%rsi
lea    0xe03(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
mov    $0x4d,%edx
call   1050 <fchown@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
mov    0x2d80(%rip),%rax
or     (%rax),%esi
mov    0x2d5f(%rip),%rax
or     (%rax),%esi
mov    0x2d7e(%rip),%rax
or     (%rax),%esi
call   1040 <fchmod@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kattach_thedawn_shm_open>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2ea3(%rip),%r14
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    0x2e78(%rip),%r12
or     (%r12),%esi
mov    0x2e2d(%rip),%r15
mov    0x2e56(%rip),%r13
mov    0x0(%r13),%edx
or     (%r15),%edx
call   1060 <shm_open@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     11b7 <kattach_thedawn_shm_open+0x77>
mov    (%r12),%esi
or     (%r14),%esi
mov    0x0(%r13),%edx
or     (%r15),%edx
mov    %rbx,%rdi
call   1060 <shm_open@plt>
test   %eax,%eax
js     1201 <kattach_thedawn_shm_open+0xc1>
mov    %eax,%ebp
lea    0xe72(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    %ebp,%edi
xor    %esi,%esi
mov    $0x4d,%edx
call   1050 <fchown@plt>
mov    0x2ddc(%rip),%rax
mov    0x2df5(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dd2(%rip),%rax
or     (%rax),%esi
mov    0x2df1(%rip),%rax
or     (%rax),%esi
mov    %ebp,%edi
call   1040 <fchmod@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf8(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
xor    %ebp,%ebp
jmp    11f0 <kattach_thedawn_shm_open+0xb0>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_RDWR = global i32 0
@S_IREAD = global i32 0
@S_IWRITE = global i32 0
@.str = [33 x i8] c"\0A [!] Unexpected SHM Error - %s\0A\00"
@.str.1 = [33 x i8] c"\0A [X] Unexpected SHM State - %s\0A\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
define i32 @kattach_thedawn_shm_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_EXCL
%10 = or i32 %8, %9
%11 = load i32, i32* @O_RDWR
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IREAD
%14 = load i32, i32* @S_IWRITE
%15 = or i32 %13, %14
%16 = call i32 @shm_open(i8* %7, i32 %12, i32 %15)
store i32 %16, i32* %4
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %1
store i32 1, i32* %5
br label %33
19: ; preds = %1
%20 = load i8*, i8** %3
%21 = load i32, i32* @O_CREAT
%22 = load i32, i32* @O_RDWR
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IREAD
%25 = load i32, i32* @S_IWRITE
%26 = or i32 %24, %25
%27 = call i32 @shm_open(i8* %20, i32 %23, i32 %26)
store i32 %27, i32* %4
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %19
%30 = load i8*, i8** %3
%31 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %30)
store i32 0, i32* %2
br label %52
32: ; preds = %19
br label %33
33: ; preds = %32, %18
%34 = load i32, i32* %5
%35 = icmp ne i32 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = load i8*, i8** %3
%38 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %36, %33
%40 = load i32, i32* %4
%41 = call i32 @fchown(i32 %40, i32 0, i32 77)
store i32 %41, i32* %6
%42 = load i32, i32* %4
%43 = load i32, i32* @S_IRUSR
%44 = load i32, i32* @S_IWUSR
%45 = or i32 %43, %44
%46 = load i32, i32* @S_IRGRP
%47 = or i32 %45, %46
%48 = load i32, i32* @S_IWGRP
%49 = or i32 %47, %48
%50 = call i32 @fchmod(i32 %42, i32 %49)
store i32 %50, i32* %6
%51 = load i32, i32* %4
store i32 %51, i32* %2
br label %52
52: ; preds = %39, %29
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_RDWR = global i32 0
@S_IREAD = global i32 0
@S_IWRITE = global i32 0
@.str = [33 x i8] c"\0A [!] Unexpected SHM Error - %s\0A\00"
@.str.1 = [33 x i8] c"\0A [X] Unexpected SHM State - %s\0A\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
define i32 @kattach_thedawn_shm_open(i8* %0) {
%2 = load i32, i32* @O_CREAT
%3 = load i32, i32* @O_EXCL
%4 = or i32 %3, %2
%5 = load i32, i32* @O_RDWR
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IREAD
%8 = load i32, i32* @S_IWRITE
%9 = or i32 %8, %7
%10 = tail call i32 @shm_open(i8* %0, i32 %6, i32 %9)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %25, label %12
12: ; preds = %1
%13 = load i32, i32* @O_CREAT
%14 = load i32, i32* @O_RDWR
%15 = or i32 %14, %13
%16 = load i32, i32* @S_IREAD
%17 = load i32, i32* @S_IWRITE
%18 = or i32 %17, %16
%19 = tail call i32 @shm_open(i8* %0, i32 %15, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %12
%22 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %36
23: ; preds = %12
%24 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %25
25: ; preds = %1, %23
%26 = phi i32 [ %19, %23 ], [ %10, %1 ]
%27 = tail call i32 @fchown(i32 %26, i32 0, i32 77)
%28 = load i32, i32* @S_IRUSR
%29 = load i32, i32* @S_IWUSR
%30 = or i32 %29, %28
%31 = load i32, i32* @S_IRGRP
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IWGRP
%34 = or i32 %32, %33
%35 = tail call i32 @fchmod(i32 %26, i32 %34)
br label %36
36: ; preds = %25, %21
%37 = phi i32 [ %26, %25 ], [ 0, %21 ]
ret i32 %37
}