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

/* Variables and functions */
int IPC_CREAT;
int IPC_EXCL;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
char *shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int shmdt(char *);
int shmget(int, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  int shmid;
  pid_t pid;
  int ret;
  shmid = shmget(1234, 1024, IPC_EXCL);
  if (shmid < 0) {
    shmid = shmget(1234, 1024, IPC_CREAT | 0640);
    if (shmid < 0) {
      perror("shmget");
      return -1;
    }
  }
  if (!(pid = fork())) {
    char *childAddr;
    childAddr = shmat(shmid, 0, 0);
    if (childAddr == NULL) {
      perror("shmat");
      return -1;
    }
    strcpy(childAddr, "hello");
    printf("child send a message:%s\n", childAddr);
    ret = shmdt(childAddr);
    return 0;
  }
  sleep(2);
  char *parentAddr;
  parentAddr = shmat(shmid, 0, 0);
  if (parentAddr == NULL) {
    perror("shmat");
    return -1;
  }
  char buff[1024] = {0};
  strcpy(buff, parentAddr);
  printf("in parent recv a message:%s\n", buff);
  ret = shmdt(parentAddr);
  wait(&pid);
  shmctl(shmid, IPC_RMID, NULL);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
mov    $0x4d2,%edi
mov    $0x400,%esi
call   10a0 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    122f <main+0x7f>
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
or     $0x1a0,%edx
mov    $0x4d2,%edi
mov    $0x400,%esi
call   10a0 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    122a <main+0x7a>
lea    0xde7(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1351 <main+0x1a1>
jmp    122f <main+0x7f>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jne    12ad <main+0xfd>
mov    -0x8(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1090 <shmat@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1275 <main+0xc5>
lea    0xda3(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1351 <main+0x1a1>
mov    -0x18(%rbp),%rdi
lea    0xd8d(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rsi
lea    0xd83(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <shmdt@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x4(%rbp)
jmp    1351 <main+0x1a1>
mov    $0x2,%edi
call   10b0 <sleep@plt>
mov    -0x8(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1090 <shmat@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12ea <main+0x13a>
lea    0xd2e(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1351 <main+0x1a1>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x420(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x420(%rbp),%rsi
lea    0xd11(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <shmdt@plt>
mov    %eax,-0x10(%rbp)
lea    -0xc(%rbp),%rdi
call   10c0 <wait@plt>
mov    -0x8(%rbp),%edi
mov    0x2c99(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <shmctl@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
mov    $0x4d2,%edi
mov    $0x400,%esi
call   10a0 <shmget@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1202 <main+0x52>
mov    0x2dee(%rip),%rax
mov    $0x1a0,%edx
or     (%rax),%edx
mov    $0x4d2,%edi
mov    $0x400,%esi
call   10a0 <shmget@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12df <main+0x12f>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
je     1289 <main+0xd9>
mov    $0x2,%edi
call   10b0 <sleep@plt>
xor    %r14d,%r14d
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
test   %rax,%rax
je     12d6 <main+0x126>
mov    %rax,%rbx
lea    0x10(%rsp),%r15
mov    $0x400,%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
lea    0xdd1(%rip),%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
lea    0xc(%rsp),%rdi
call   10c0 <wait@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1070 <shmctl@plt>
jmp    12c5 <main+0x115>
xor    %r14d,%r14d
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
test   %rax,%rax
je     12d6 <main+0x126>
mov    %rax,%rbx
lea    0xd67(%rip),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
lea    0xd5e(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
mov    %r14d,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd2a(%rip),%rdi
jmp    12e6 <main+0x136>
lea    0xd1a(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%r14d
jmp    12c5 <main+0x115>

LLVM IR (-O0):
@IPC_EXCL = global i32 0
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
@.str.2 = [6 x i8] c"hello\00"
@.str.3 = [25 x i8] c"child send a message:%s\0A\00"
@.str.4 = [29 x i8] c"in parent recv a message:%s\0A\00"
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [1024 x i8]
store i32 0, i32* %1
%8 = load i32, i32* @IPC_EXCL
%9 = call i32 @shmget(i32 1234, i32 1024, i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %0
%13 = load i32, i32* @IPC_CREAT
%14 = or i32 %13, 416
%15 = call i32 @shmget(i32 1234, i32 1024, i32 %14)
store i32 %15, i32* %2
%16 = load i32, i32* %2
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %59
20: ; preds = %12
br label %21
21: ; preds = %20, %0
%22 = call i32 (...) @fork()
store i32 %22, i32* %3
%23 = icmp ne i32 %22, 0
br i1 %23, label %38, label %24
24: ; preds = %21
%25 = load i32, i32* %2
%26 = call i8* @shmat(i32 %25, i32 0, i32 0)
store i8* %26, i8** %5
%27 = load i8*, i8** %5
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %59
31: ; preds = %24
%32 = load i8*, i8** %5
%33 = call i32 @strcpy(i8* %32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%34 = load i8*, i8** %5
%35 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%36 = load i8*, i8** %5
%37 = call i32 @shmdt(i8* %36)
store i32 %37, i32* %4
store i32 0, i32* %1
br label %59
38: ; preds = %21
%39 = call i32 @sleep(i32 2)
%40 = load i32, i32* %2
%41 = call i8* @shmat(i32 %40, i32 0, i32 0)
store i8* %41, i8** %6
%42 = load i8*, i8** %6
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %46
44: ; preds = %38
%45 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %59
46: ; preds = %38
%47 = bitcast [1024 x i8]* %7 to i8*
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%49 = load i8*, i8** %6
%50 = call i32 @strcpy(i8* %48, i8* %49)
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%52 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %51)
%53 = load i8*, i8** %6
%54 = call i32 @shmdt(i8* %53)
store i32 %54, i32* %4
%55 = call i32 @wait(i32* %3)
%56 = load i32, i32* %2
%57 = load i32, i32* @IPC_RMID
%58 = call i32 @shmctl(i32 %56, i32 %57, i32* null)
store i32 0, i32* %1
br label %59
59: ; preds = %46, %44, %31, %29, %18
%60 = load i32, i32* %1
ret i32 %60
}

LLVM IR (-O2):
@IPC_EXCL = global i32 0
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
@.str.2 = [6 x i8] c"hello\00"
@.str.3 = [25 x i8] c"child send a message:%s\0A\00"
@.str.4 = [29 x i8] c"in parent recv a message:%s\0A\00"
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = bitcast i32* %1 to i8*
%4 = load i32, i32* @IPC_EXCL
%5 = tail call i32 @shmget(i32 1234, i32 1024, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %0
%8 = load i32, i32* @IPC_CREAT
%9 = or i32 %8, 416
%10 = tail call i32 @shmget(i32 1234, i32 1024, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %41
14: ; preds = %7, %0
%15 = phi i32 [ %10, %7 ], [ %5, %0 ]
%16 = tail call i32 (...) @fork()
store i32 %16, i32* %1
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %14
%19 = tail call i8* @shmat(i32 %15, i32 0, i32 0)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %41
23: ; preds = %18
%24 = tail call i32 @strcpy(i8* %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%25 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%26 = tail call i32 @shmdt(i8* %19)
br label %41
27: ; preds = %14
%28 = tail call i32 @sleep(i32 2)
%29 = tail call i8* @shmat(i32 %15, i32 0, i32 0)
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %41
33: ; preds = %27
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%35 = call i32 @strcpy(i8* %34, i8* %29)
%36 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %34)
%37 = call i32 @shmdt(i8* %29)
%38 = call i32 @wait(i32* %1)
%39 = load i32, i32* @IPC_RMID
%40 = call i32 @shmctl(i32 %15, i32 %39, i32* null)
br label %41
41: ; preds = %31, %33, %21, %23, %12
%42 = phi i32 [ -1, %12 ], [ -1, %21 ], [ 0, %23 ], [ -1, %31 ], [ 0, %33 ]
ret i32 %42
}