#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 */
scalar_t__ EEXIST;
int IPC_CREAT;
int IPC_EXCL;
int /*<<< orphan*/ bzero(char *, int);
scalar_t__ errno;
int /*<<< orphan*/ printf(char *);
char *shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
int shmget(int, int, int);
char *get_shared_mem() {
  int iExits = 0;
  errno = 0;
  int shmid = shmget(0x2235, 1024, IPC_CREAT | IPC_EXCL);
  if (shmid < 0) {
    if (EEXIST == errno) {
      shmid = shmget(0x2235, 1024, IPC_CREAT);
      iExits = 1;
    } else {
      printf("create shared memory faild\n");
      return NULL;
    }
  }
  char *ptr = shmat(shmid, 0, 0);
  if (NULL == ptr) {
    printf("get shared memory address faild\n");
    return NULL;
  }
  if (0 == iExits) {
    bzero(ptr, 1024);
  }
  return ptr;
}


x86 assembly (clang, -O0):
<get_shared_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0xc(%rbp)
mov    0x2e7a(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e64(%rip),%rax
mov    (%rax),%edx
mov    0x2e83(%rip),%rax
or     (%rax),%edx
mov    $0x2235,%edi
mov    $0x400,%esi
call   1060 <shmget@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11ea <get_shared_mem+0xaa>
mov    0x2e46(%rip),%rax
mov    (%rax),%rax
mov    0x2e34(%rip),%rcx
cmp    (%rcx),%rax
jne    11cc <get_shared_mem+0x8c>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
mov    $0x2235,%edi
mov    $0x400,%esi
call   1060 <shmget@plt>
mov    %eax,-0x10(%rbp)
movl   $0x1,-0xc(%rbp)
jmp    11e5 <get_shared_mem+0xa5>
lea    0xe2d(%rip),%rdi
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1240 <get_shared_mem+0x100>
jmp    11ea <get_shared_mem+0xaa>
mov    -0x10(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1050 <shmat@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    121f <get_shared_mem+0xdf>
lea    0xe0f(%rip),%rdi
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1240 <get_shared_mem+0x100>
xor    %eax,%eax
cmp    -0xc(%rbp),%eax
jne    1238 <get_shared_mem+0xf8>
mov    -0x18(%rbp),%rdi
mov    $0x400,%esi
call   1040 <bzero@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_shared_mem>:
push   %r14
push   %rbx
push   %rax
mov    0x2e85(%rip),%rbx
movq   $0x0,(%rbx)
mov    0x2e6f(%rip),%r14
mov    0x2e90(%rip),%rax
mov    (%rax),%edx
or     (%r14),%edx
mov    $0x2235,%edi
mov    $0x400,%esi
call   1060 <shmget@plt>
test   %eax,%eax
js     119a <get_shared_mem+0x5a>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
test   %rax,%rax
je     11ce <get_shared_mem+0x8e>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x400,%esi
call   1040 <bzero@plt>
jmp    11e5 <get_shared_mem+0xa5>
mov    0x2e37(%rip),%rax
mov    (%rax),%rax
cmp    (%rbx),%rax
jne    11d7 <get_shared_mem+0x97>
mov    (%r14),%edx
mov    $0x2235,%edi
mov    $0x400,%esi
call   1060 <shmget@plt>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11e5 <get_shared_mem+0xa5>
lea    0xe2b(%rip),%rdi
jmp    11de <get_shared_mem+0x9e>
lea    0xe42(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@errno = global i64 0
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@EEXIST = global i64 0
@.str = [28 x i8] c"create shared memory faild\0A\00"
@.str.1 = [33 x i8] c"get shared memory address faild\0A\00"
define i8* @get_shared_mem() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %2
store i64 0, i64* @errno
%5 = load i32, i32* @IPC_CREAT
%6 = load i32, i32* @IPC_EXCL
%7 = or i32 %5, %6
%8 = call i32 @shmget(i32 8757, i32 1024, i32 %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %0
%12 = load i64, i64* @EEXIST
%13 = load i64, i64* @errno
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* @IPC_CREAT
%17 = call i32 @shmget(i32 8757, i32 1024, i32 %16)
store i32 %17, i32* %3
store i32 1, i32* %2
br label %20
18: ; preds = %11
%19 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %1
br label %36
20: ; preds = %15
br label %21
21: ; preds = %20, %0
%22 = load i32, i32* %3
%23 = call i8* @shmat(i32 %22, i32 0, i32 0)
store i8* %23, i8** %4
%24 = load i8*, i8** %4
%25 = icmp eq i8* null, %24
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %1
br label %36
28: ; preds = %21
%29 = load i32, i32* %2
%30 = icmp eq i32 0, %29
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i8*, i8** %4
%33 = call i32 @bzero(i8* %32, i32 1024)
br label %34
34: ; preds = %31, %28
%35 = load i8*, i8** %4
store i8* %35, i8** %1
br label %36
36: ; preds = %34, %26, %18
%37 = load i8*, i8** %1
ret i8* %37
}

LLVM IR (-O2):
@errno = global i64 0
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@EEXIST = global i64 0
@str = [32 x i8] c"get shared memory address faild\00"
@str.2 = [27 x i8] c"create shared memory faild\00"
define i8* @get_shared_mem() {
store i64 0, i64* @errno
%1 = load i32, i32* @IPC_CREAT
%2 = load i32, i32* @IPC_EXCL
%3 = or i32 %2, %1
%4 = tail call i32 @shmget(i32 8757, i32 1024, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %17
6: ; preds = %0
%7 = load i64, i64* @EEXIST
%8 = load i64, i64* @errno
%9 = icmp eq i64 %7, %8
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.2, i64 0, i64 0))
br label %24
12: ; preds = %6
%13 = load i32, i32* @IPC_CREAT
%14 = tail call i32 @shmget(i32 8757, i32 1024, i32 %13)
%15 = tail call i8* @shmat(i32 %14, i32 0, i32 0)
%16 = icmp eq i8* %15, null
br i1 %16, label %20, label %24
17: ; preds = %0
%18 = tail call i8* @shmat(i32 %4, i32 0, i32 0)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %17, %12
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
br label %24
22: ; preds = %17
%23 = tail call i32 @bzero(i8* %18, i32 1024)
br label %24
24: ; preds = %12, %20, %22, %10
%25 = phi i8* [ null, %10 ], [ null, %20 ], [ %18, %22 ], [ %15, %12 ]
ret i8* %25
}