#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ IPC_PRIVATE;
scalar_t__ fork();
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
char *shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ shmdt(char *);
int shmget(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(void) {
  int shmid;
  char *p_addr, *c_addr;
  if ((shmid = shmget(IPC_PRIVATE, 1024, 0755)) == -1) {
    perror("Create share memory error");
    return 1;
  }
  char buffer[] = "Hello World!";
  pid_t pid = fork();
  if (pid > 0) {
    puts("parent process!");
    p_addr = shmat(shmid, 0, 0);
    memset(p_addr, '\0', 1024);
    strcpy(p_addr, buffer);
    printf("parent process write to share memory: %s\n", p_addr);
    wait(NULL);
    shmdt(p_addr);
  } else {
    puts("child process!");
    sleep(1);
    c_addr = shmat(shmid, 0, 0);
    printf("child process read from share memory: %s\n", c_addr);
    shmdt(c_addr);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    $0x400,%esi
mov    $0x1ed,%edx
call   10a0 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11fb <main+0x4b>
lea    0xe16(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d4 <main+0x124>
mov    0xe18(%rip),%rax
mov    %rax,-0x25(%rbp)
mov    0xe16(%rip),%eax
mov    %eax,-0x1d(%rbp)
mov    0xe11(%rip),%al
mov    %al,-0x19(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jle    128e <main+0xde>
lea    0xdf2(%rip),%rdi
call   1040 <puts@plt>
mov    -0x8(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1090 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x25(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rsi
lea    0xdc5(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
mov    -0x10(%rbp),%rdi
call   1050 <shmdt@plt>
jmp    12cd <main+0x11d>
lea    0xdcc(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    -0x8(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1090 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xdb1(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <shmdt@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
mov    $0x400,%esi
mov    $0x1ed,%edx
call   10a0 <shmget@plt>
cmp    $0xffffffff,%eax
je     1290 <main+0xe0>
mov    %eax,%ebx
movabs $0x21646c726f5720,%rax
mov    %rax,0x5(%rsp)
movabs $0x6f57206f6c6c6548,%rax
20 57 6f
mov    %rax,(%rsp)
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
jle    124d <main+0x9d>
lea    0xe1f(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xdfb(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
xor    %edi,%edi
call   10c0 <wait@plt>
jmp    1280 <main+0xd0>
lea    0xe0d(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
mov    %rax,%rbx
lea    0xdf8(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <shmdt@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xd69(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    128a <main+0xda>

LLVM IR (-O0):
@IPC_PRIVATE = global i32 0
@.str = [26 x i8] c"Create share memory error\00"
@__const.main.buffer = [13 x i8] c"Hello World!\00"
@.str.1 = [16 x i8] c"parent process!\00"
@.str.2 = [42 x i8] c"parent process write to share memory: %s\0A\00"
@.str.3 = [15 x i8] c"child process!\00"
@.str.4 = [42 x i8] c"child process read from share memory: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [13 x i8]
%6 = alloca i64
store i32 0, i32* %1
%7 = load i32, i32* @IPC_PRIVATE
%8 = call i32 @shmget(i32 %7, i32 1024, i32 493)
store i32 %8, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %41
12: ; preds = %0
%13 = bitcast [13 x i8]* %5 to i8*
%14 = call i64 (...) @fork()
store i64 %14, i64* %6
%15 = load i64, i64* %6
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %31
17: ; preds = %12
%18 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* %2
%20 = call i8* @shmat(i32 %19, i32 0, i32 0)
store i8* %20, i8** %3
%21 = load i8*, i8** %3
%22 = call i32 @memset(i8* %21, i8 signext 0, i32 1024)
%23 = load i8*, i8** %3
%24 = getelementptr inbounds [13 x i8], [13 x i8]* %5, i64 0, i64 0
%25 = call i32 @strcpy(i8* %23, i8* %24)
%26 = load i8*, i8** %3
%27 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%28 = call i32 @wait(i32* null)
%29 = load i8*, i8** %3
%30 = call i32 @shmdt(i8* %29)
br label %40
31: ; preds = %12
%32 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @sleep(i32 1)
%34 = load i32, i32* %2
%35 = call i8* @shmat(i32 %34, i32 0, i32 0)
store i8* %35, i8** %4
%36 = load i8*, i8** %4
%37 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %36)
%38 = load i8*, i8** %4
%39 = call i32 @shmdt(i8* %38)
br label %40
40: ; preds = %31, %17
store i32 0, i32* %1
br label %41
41: ; preds = %40, %10
%42 = load i32, i32* %1
ret i32 %42
}

LLVM IR (-O2):
@IPC_PRIVATE = global i32 0
@.str = [26 x i8] c"Create share memory error\00"
@__const.main.buffer = [13 x i8] c"Hello World!\00"
@.str.1 = [16 x i8] c"parent process!\00"
@.str.2 = [42 x i8] c"parent process write to share memory: %s\0A\00"
@.str.3 = [15 x i8] c"child process!\00"
@.str.4 = [42 x i8] c"child process read from share memory: %s\0A\00"
define i32 @main() {
%1 = alloca [13 x i8]
%2 = load i32, i32* @IPC_PRIVATE
%3 = tail call i32 @shmget(i32 %2, i32 1024, i32 493)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %26
7: ; preds = %0
%8 = getelementptr inbounds [13 x i8], [13 x i8]* %1, i64 0, i64 0
%9 = tail call i64 (...) @fork()
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %19
11: ; preds = %7
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i8* @shmat(i32 %3, i32 0, i32 0)
%14 = tail call i32 @memset(i8* %13, i8 signext 0, i32 1024)
%15 = call i32 @strcpy(i8* %13, i8* %8)
%16 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%17 = call i32 @wait(i32* null)
%18 = call i32 @shmdt(i8* %13)
br label %25
19: ; preds = %7
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%21 = tail call i32 @sleep(i32 1)
%22 = tail call i8* @shmat(i32 %3, i32 0, i32 0)
%23 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %22)
%24 = tail call i32 @shmdt(i8* %22)
br label %25
25: ; preds = %19, %11
br label %26
26: ; preds = %25, %5
%27 = phi i32 [ 1, %5 ], [ 0, %25 ]
ret i32 %27
}