#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 */
struct shmid_ds {
  int dummy;
};
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int IPC_CREAT;
int IPC_EXCL;
int /*<<< orphan*/ IPC_PRIVATE;
int /*<<< orphan*/ IPC_RMID;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ fork();
int getpagesize();
int /*<<< orphan*/ print_hundred(int);
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, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main() {
  pid_t child_pid;
  int segment_id;
  char *shared_memory;
  struct shmid_ds shmbuffer;
  int segment_size;
  segment_id = shmget(IPC_PRIVATE, 1 * getpagesize(),
                      IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
  shared_memory = (char *)shmat(segment_id, 0, 0);
  sprintf(shared_memory, "%d", 100);
  shmdt(shared_memory);
  child_pid = fork();
  int i = 0;
  for (i = 0; i < 1000000; i++)
    print_hundred(segment_id);
  shmctl(segment_id, IPC_RMID, 0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1070 <getpagesize@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%esi
shl    $0x0,%esi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
mov    0x2e36(%rip),%rax
or     (%rax),%edx
mov    0x2dfd(%rip),%rax
or     (%rax),%edx
mov    0x2e0c(%rip),%rax
or     (%rax),%edx
call   1060 <shmget@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1050 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe0f(%rip),%rsi
mov    $0x64,%edx
call   1080 <sprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <shmdt@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0xf4240,-0x28(%rbp)
jge    123f <main+0xbf>
mov    -0xc(%rbp),%edi
call   1090 <print_hundred@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    121c <main+0x9c>
mov    -0xc(%rbp),%edi
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <shmctl@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e55(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1070 <getpagesize@plt>
mov    0x2e1d(%rip),%rcx
mov    0x2e4e(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e13(%rip),%rcx
or     (%rcx),%edx
mov    0x2e22(%rip),%rcx
or     (%rcx),%edx
mov    %ebx,%edi
mov    %eax,%esi
call   1060 <shmget@plt>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,%r14
lea    0xe28(%rip),%rsi
mov    %rax,%rdi
mov    $0x64,%edx
call   1080 <sprintf@plt>
mov    %r14,%rdi
call   1030 <shmdt@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    $0xf4240,%ebp
nopl   0x0(%rax)
mov    %ebx,%edi
call   1090 <print_hundred@plt>
add    $0xffffffff,%ebp
jne    1200 <main+0x80>
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <shmctl@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.shmid_ds = type { i32 }
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [3 x i8] c"%d\00"
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.shmid_ds
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = load i32, i32* @IPC_PRIVATE
%9 = call i32 (...) @getpagesize()
%10 = mul nsw i32 1, %9
%11 = load i32, i32* @IPC_CREAT
%12 = load i32, i32* @IPC_EXCL
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRUSR
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IWUSR
%17 = or i32 %15, %16
%18 = call i32 @shmget(i32 %8, i32 %10, i32 %17)
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = call i64 @shmat(i32 %19, i32 0, i32 0)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %4
%22 = load i8*, i8** %4
%23 = call i32 @sprintf(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 100)
%24 = load i8*, i8** %4
%25 = call i32 @shmdt(i8* %24)
%26 = call i32 (...) @fork()
store i32 %26, i32* %2
store i32 0, i32* %7
store i32 0, i32* %7
br label %27
27: ; preds = %33, %0
%28 = load i32, i32* %7
%29 = icmp slt i32 %28, 1000000
br i1 %29, label %30, label %36
30: ; preds = %27
%31 = load i32, i32* %3
%32 = call i32 @print_hundred(i32 %31)
br label %33
33: ; preds = %30
%34 = load i32, i32* %7
%35 = add nsw i32 %34, 1
store i32 %35, i32* %7
br label %27
36: ; preds = %27
%37 = load i32, i32* %3
%38 = load i32, i32* @IPC_RMID
%39 = call i32 @shmctl(i32 %37, i32 %38, i32 0)
ret i32 0
}

LLVM IR (-O2):
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [3 x i8] c"%d\00"
@IPC_RMID = global i32 0
define i32 @main() {
%1 = load i32, i32* @IPC_PRIVATE
%2 = tail call i32 (...) @getpagesize()
%3 = load i32, i32* @IPC_CREAT
%4 = load i32, i32* @IPC_EXCL
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRUSR
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %7, %8
%10 = tail call i32 @shmget(i32 %1, i32 %2, i32 %9)
%11 = tail call i64 @shmat(i32 %10, i32 0, i32 0)
%12 = inttoptr i64 %11 to i8*
%13 = tail call i32 @sprintf(i8* dereferenceable(1) %12, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 100)
%14 = tail call i32 @shmdt(i8* %12)
%15 = tail call i32 (...) @fork()
br label %16
16: ; preds = %0, %16
%17 = phi i32 [ 0, %0 ], [ %19, %16 ]
%18 = tail call i32 @print_hundred(i32 %10)
%19 = add nuw nsw i32 %17, 1
%20 = icmp eq i32 %19, 1000000
br i1 %20, label %21, label %16
21: ; preds = %16
%22 = load i32, i32* @IPC_RMID
%23 = tail call i32 @shmctl(i32 %10, i32 %22, i32 0)
ret i32 0
}