#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*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int close(int);
void *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ printDots(int);
int /*<<< orphan*/ printWithStatus(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int shm_open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int openSharedMemory(char *name, void **p) {
  int fd, err = 0;
  if (!err) {
    fd = shm_open(name, O_RDWR, (mode_t)0777);
    err = fd < 0;
  }
  if (!err) {
    *p = mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    err = (*p == (void *)-1);
  }
  if (!err) {
    err = close(fd);
  }
  char printStr[128];
  printStr[0] = '\0';
  strcat(printStr, "Opening shared memory at ");
  strcat(printStr, name);
  printf("%s", printStr);
  if (strlen(printStr) < 70)
    printDots(70 - strlen(printStr));
  printWithStatus("", err);
  printf("\n");
  return err;
}


x86 assembly (clang, -O0):
<openSharedMemory>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11cd <openSharedMemory+0x4d>
mov    -0x8(%rbp),%rdi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    $0x1ff,%edx
call   10a0 <shm_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1229 <openSharedMemory+0xa9>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
mov    0x2de1(%rip),%rax
or     (%rax),%edx
mov    0x2df8(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x1000,%esi
mov    %r9d,%edi
call   1060 <mmap@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,(%rax)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    123e <openSharedMemory+0xbe>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    %eax,-0x18(%rbp)
movb   $0x0,-0xa0(%rbp)
lea    -0xa0(%rbp),%rdi
lea    0xdad(%rip),%rsi
call   1090 <strcat@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1090 <strcat@plt>
lea    -0xa0(%rbp),%rsi
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0xa0(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x46,%eax
jge    12aa <openSharedMemory+0x12a>
lea    -0xa0(%rbp),%rdi
call   1050 <strlen@plt>
mov    $0x46,%edi
sub    %eax,%edi
call   1040 <printDots@plt>
mov    -0x18(%rbp),%esi
lea    0xd6a(%rip),%rdi
call   1030 <printWithStatus@plt>
lea    0xd5d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openSharedMemory>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    $0x1ff,%edx
call   10b0 <shm_open@plt>
mov    $0x1,%r15d
test   %eax,%eax
js     1202 <openSharedMemory+0x72>
mov    %eax,%ebp
mov    0x2e2f(%rip),%rax
mov    0x2df8(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0d(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1070 <mmap@plt>
mov    %rax,(%r14)
cmp    $0xffffffffffffffff,%rax
je     1202 <openSharedMemory+0x72>
mov    %ebp,%edi
call   1090 <close@plt>
mov    %eax,%r15d
movb   $0x0,(%rsp)
lea    0xdf3(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
call   10a0 <strcat@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   10a0 <strcat@plt>
lea    0xdf0(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
cmp    $0x45,%eax
jg     1255 <openSharedMemory+0xc5>
mov    %rsp,%rdi
call   1060 <strlen@plt>
mov    $0x46,%edi
sub    %eax,%edi
call   1050 <printDots@plt>
lea    0xdbd(%rip),%rdi
mov    %r15d,%esi
call   1040 <printWithStatus@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r15d,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str = [26 x i8] c"Opening shared memory at \00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [2 x i8] c"\0A\00"
define i32 @openSharedMemory(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca [128 x i8]
store i8* %0, i8** %3
store i8** %1, i8*** %4
store i32 0, i32* %6
%8 = load i32, i32* %6
%9 = icmp ne i32 %8, 0
br i1 %9, label %17, label %10
10: ; preds = %2
%11 = load i8*, i8** %3
%12 = load i32, i32* @O_RDWR
%13 = call i32 @shm_open(i8* %11, i32 %12, i32 511)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp slt i32 %14, 0
%16 = zext i1 %15 to i32
store i32 %16, i32* %6
br label %17
17: ; preds = %10, %2
%18 = load i32, i32* %6
%19 = icmp ne i32 %18, 0
br i1 %19, label %32, label %20
20: ; preds = %17
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @PROT_WRITE
%23 = or i32 %21, %22
%24 = load i32, i32* @MAP_SHARED
%25 = load i32, i32* %5
%26 = call i8* @mmap(i32 0, i32 4096, i32 %23, i32 %24, i32 %25, i32 0)
%27 = load i8**, i8*** %4
store i8* %26, i8** %27
%28 = load i8**, i8*** %4
%29 = load i8*, i8** %28
%30 = icmp eq i8* %29, inttoptr (i64 -1 to i8*)
%31 = zext i1 %30 to i32
store i32 %31, i32* %6
br label %32
32: ; preds = %20, %17
%33 = load i32, i32* %6
%34 = icmp ne i32 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %32
%36 = load i32, i32* %5
%37 = call i32 @close(i32 %36)
store i32 %37, i32* %6
br label %38
38: ; preds = %35, %32
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
store i8 0, i8* %39
%40 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%41 = call i32 @strcat(i8* %40, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%42 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%43 = load i8*, i8** %3
%44 = call i32 @strcat(i8* %42, i8* %43)
%45 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %45)
%47 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%48 = call i32 @strlen(i8* %47)
%49 = icmp slt i32 %48, 70
br i1 %49, label %50, label %55
50: ; preds = %38
%51 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%52 = call i32 @strlen(i8* %51)
%53 = sub nsw i32 70, %52
%54 = call i32 @printDots(i32 %53)
br label %55
55: ; preds = %50, %38
%56 = load i32, i32* %6
%57 = call i32 @printWithStatus(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i32 %56)
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%59 = load i32, i32* %6
ret i32 %59
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str = [26 x i8] c"Opening shared memory at \00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [1 x i8] zeroinitializer
define i32 @openSharedMemory(i8* %0, i8** nocapture writeonly %1) {
%3 = alloca [128 x i8]
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @shm_open(i8* %0, i32 %4, i32 511)
%6 = icmp sgt i32 %5, -1
br i1 %6, label %7, label %16
7: ; preds = %2
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %9, %8
%11 = load i32, i32* @MAP_SHARED
%12 = tail call i8* @mmap(i32 0, i32 4096, i32 %10, i32 %11, i32 %5, i32 0)
store i8* %12, i8** %1
%13 = icmp eq i8* %12, inttoptr (i64 -1 to i8*)
br i1 %13, label %16, label %14
14: ; preds = %7
%15 = tail call i32 @close(i32 %5)
br label %16
16: ; preds = %2, %14, %7
%17 = phi i32 [ 1, %7 ], [ %15, %14 ], [ 1, %2 ]
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
store i8 0, i8* %18
%19 = call i32 @strcat(i8* %18, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @strcat(i8* %18, i8* %0)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%22 = call i32 @strlen(i8* %18)
%23 = icmp slt i32 %22, 70
br i1 %23, label %24, label %28
24: ; preds = %16
%25 = call i32 @strlen(i8* %18)
%26 = sub nsw i32 70, %25
%27 = call i32 @printDots(i32 %26)
br label %28
28: ; preds = %24, %16
%29 = call i32 @printWithStatus(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%30 = call i32 @putchar(i32 10)
ret i32 %17
}