#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;
int /*<<< orphan*/ IPC_RMID;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ shmdt(char *);
int shmget(int /*<<< orphan*/, int const, int);
int /*<<< orphan*/ sprintf(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  int segment_id;
  char *shared_memory;
  const int size = 4096;
  segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
  shared_memory = (char *)shmat(segment_id, NULL, 0);
  sprintf(shared_memory, "Hi there!");
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    fprintf(stderr, "Fork Failed");
    exit(-1);
  } else if (pid == 0) {
    printf("*%s\n", shared_memory);
  } else {
    wait(NULL);
    printf("Child Complete\n");
    shmdt(shared_memory);
    shmctl(segment_id, IPC_RMID, NULL);
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x1000,-0x14(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
mov    0x2e18(%rip),%rax
or     (%rax),%edx
mov    $0x1000,%esi
call   1080 <shmget@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe0c(%rip),%rsi
call   1090 <sprintf@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    122e <main+0x8e>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1250 <main+0xb0>
mov    -0x10(%rbp),%rsi
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    128c <main+0xec>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
mov    -0x4(%rbp),%edi
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <shmctl@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    1291 <main+0xf1>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    0x2e0d(%rip),%rax
mov    0x2e26(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    $0x1000,%esi
call   1090 <shmget@plt>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,%rbx
movabs $0x6572656874206948,%rax
65 72 65
mov    %rax,(%rbx)
movw   $0x21,0x8(%rbx)
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1213 <main+0x73>
jne    1232 <main+0x92>
lea    0xe16(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   10b0 <wait@plt>
lea    0xddb(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1070 <shmctl@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@IPC_PRIVATE = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [10 x i8] c"Hi there!\00"
@stderr = global i32 0
@.str.1 = [12 x i8] c"Fork Failed\00"
@.str.2 = [5 x i8] c"*%s\0A\00"
@.str.3 = [16 x i8] c"Child Complete\0A\00"
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i64
store i32 4096, i32* %3
%5 = load i32, i32* @IPC_PRIVATE
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %6, %7
%9 = call i32 @shmget(i32 %5, i32 4096, i32 %8)
store i32 %9, i32* %1
%10 = load i32, i32* %1
%11 = call i64 @shmat(i32 %10, i32* null, i32 0)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %2
%13 = load i8*, i8** %2
%14 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%15 = call i64 (...) @fork()
store i64 %15, i64* %4
%16 = load i64, i64* %4
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %0
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %0
%23 = load i64, i64* %4
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i8*, i8** %2
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %26)
br label %37
28: ; preds = %22
%29 = call i32 @wait(i32* null)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%31 = load i8*, i8** %2
%32 = call i32 @shmdt(i8* %31)
%33 = load i32, i32* %1
%34 = load i32, i32* @IPC_RMID
%35 = call i32 @shmctl(i32 %33, i32 %34, i32* null)
%36 = call i32 @exit(i32 0)
unreachable
37: ; preds = %25
br label %38
38: ; preds = %37
ret i32 0
}

LLVM IR (-O2):
@IPC_PRIVATE = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [10 x i8] c"Hi there!\00"
@stderr = global i32 0
@.str.1 = [12 x i8] c"Fork Failed\00"
@.str.2 = [5 x i8] c"*%s\0A\00"
@IPC_RMID = global i32 0
@str = [15 x i8] c"Child Complete\00"
define i32 @main() {
%1 = load i32, i32* @IPC_PRIVATE
%2 = load i32, i32* @S_IRUSR
%3 = load i32, i32* @S_IWUSR
%4 = or i32 %3, %2
%5 = tail call i32 @shmget(i32 %1, i32 4096, i32 %4)
%6 = tail call i64 @shmat(i32 %5, i32* null, i32 0)
%7 = inttoptr i64 %6 to i8*
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(10) %7, i8* align 1 dereferenceable(10) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i64 10, i1 false)
%8 = tail call i64 (...) @fork()
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 -1)
unreachable
14: ; preds = %0
%15 = icmp eq i64 %8, 0
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %7)
ret i32 0
18: ; preds = %14
%19 = tail call i32 @wait(i32* null)
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%21 = tail call i32 @shmdt(i8* %7)
%22 = load i32, i32* @IPC_RMID
%23 = tail call i32 @shmctl(i32 %5, i32 %22, i32* null)
%24 = tail call i32 @exit(i32 0)
unreachable
}