#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 binary_semaphore_allocation(int /*<<< orphan*/, int);
int /*<<< orphan*/ binary_semaphore_deallocate(int);
int /*<<< orphan*/ binary_semaphore_initialize(int);
int /*<<< orphan*/ fork();
int getpagesize();
int /*<<< orphan*/ print_hundred(int, 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;
  int semid =
      binary_semaphore_allocation(IPC_PRIVATE, IPC_CREAT | S_IRUSR | S_IWUSR);
  binary_semaphore_initialize(semid);
  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(semid, segment_id);
  shmctl(segment_id, IPC_RMID, 0);
  binary_semaphore_deallocate(semid);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    0x2de8(%rip),%rax
or     (%rax),%esi
mov    0x2df7(%rip),%rax
or     (%rax),%esi
call   1050 <binary_semaphore_allocation@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1030 <binary_semaphore_initialize@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1090 <getpagesize@plt>
mov    -0x30(%rbp),%edi
mov    %eax,%esi
shl    $0x0,%esi
mov    0x2da3(%rip),%rax
mov    (%rax),%edx
mov    0x2dd2(%rip),%rax
or     (%rax),%edx
mov    0x2d99(%rip),%rax
or     (%rax),%edx
mov    0x2da8(%rip),%rax
or     (%rax),%edx
call   1080 <shmget@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xdab(%rip),%rsi
mov    $0x64,%edx
call   10a0 <sprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <shmdt@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x8(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0xf4240,-0x2c(%rbp)
jge    12a6 <main+0xf6>
mov    -0x28(%rbp),%edi
mov    -0xc(%rbp),%esi
call   10b0 <print_hundred@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1280 <main+0xd0>
mov    -0xc(%rbp),%edi
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <shmctl@plt>
mov    -0x28(%rbp),%edi
call   10c0 <binary_semaphore_deallocate@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e1e(%rip),%rbx
mov    0x2def(%rip),%r12
mov    0x2df0(%rip),%r14
mov    (%r14),%esi
or     (%r12),%esi
mov    (%rbx),%edi
mov    0x2df8(%rip),%r13
or     0x0(%r13),%esi
call   1050 <binary_semaphore_allocation@plt>
mov    %eax,%r15d
mov    %eax,%edi
call   1030 <binary_semaphore_initialize@plt>
mov    (%rbx),%ebp
xor    %eax,%eax
call   1090 <getpagesize@plt>
mov    0x2ded(%rip),%rcx
mov    (%rcx),%edx
or     (%r12),%edx
or     (%r14),%edx
or     0x0(%r13),%edx
mov    %ebp,%edi
mov    %eax,%esi
call   1080 <shmget@plt>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,%r14
lea    0xdd0(%rip),%rsi
mov    %rax,%rdi
mov    $0x64,%edx
call   10a0 <sprintf@plt>
mov    %r14,%rdi
call   1040 <shmdt@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    $0xf4240,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %ebp,%esi
call   10b0 <print_hundred@plt>
add    $0xffffffff,%ebx
jne    1260 <main+0xb0>
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1060 <shmctl@plt>
mov    %r15d,%edi
call   10c0 <binary_semaphore_deallocate@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.shmid_ds = type { i32 }
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@IPC_EXCL = 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
%8 = alloca i32
store i32 0, i32* %1
%9 = load i32, i32* @IPC_PRIVATE
%10 = load i32, i32* @IPC_CREAT
%11 = load i32, i32* @S_IRUSR
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IWUSR
%14 = or i32 %12, %13
%15 = call i32 @binary_semaphore_allocation(i32 %9, i32 %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = call i32 @binary_semaphore_initialize(i32 %16)
%18 = load i32, i32* @IPC_PRIVATE
%19 = call i32 (...) @getpagesize()
%20 = mul nsw i32 1, %19
%21 = load i32, i32* @IPC_CREAT
%22 = load i32, i32* @IPC_EXCL
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRUSR
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IWUSR
%27 = or i32 %25, %26
%28 = call i32 @shmget(i32 %18, i32 %20, i32 %27)
store i32 %28, i32* %3
%29 = load i32, i32* %3
%30 = call i64 @shmat(i32 %29, i32 0, i32 0)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %4
%32 = load i8*, i8** %4
%33 = call i32 @sprintf(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 100)
%34 = load i8*, i8** %4
%35 = call i32 @shmdt(i8* %34)
%36 = call i32 (...) @fork()
store i32 %36, i32* %2
store i32 0, i32* %8
store i32 0, i32* %8
br label %37
37: ; preds = %44, %0
%38 = load i32, i32* %8
%39 = icmp slt i32 %38, 1000000
br i1 %39, label %40, label %47
40: ; preds = %37
%41 = load i32, i32* %7
%42 = load i32, i32* %3
%43 = call i32 @print_hundred(i32 %41, i32 %42)
br label %44
44: ; preds = %40
%45 = load i32, i32* %8
%46 = add nsw i32 %45, 1
store i32 %46, i32* %8
br label %37
47: ; preds = %37
%48 = load i32, i32* %3
%49 = load i32, i32* @IPC_RMID
%50 = call i32 @shmctl(i32 %48, i32 %49, i32 0)
%51 = load i32, i32* %7
%52 = call i32 @binary_semaphore_deallocate(i32 %51)
ret i32 0
}

LLVM IR (-O2):
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@IPC_EXCL = 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 = load i32, i32* @IPC_CREAT
%3 = load i32, i32* @S_IRUSR
%4 = or i32 %3, %2
%5 = load i32, i32* @S_IWUSR
%6 = or i32 %4, %5
%7 = tail call i32 @binary_semaphore_allocation(i32 %1, i32 %6)
%8 = tail call i32 @binary_semaphore_initialize(i32 %7)
%9 = load i32, i32* @IPC_PRIVATE
%10 = tail call i32 (...) @getpagesize()
%11 = load i32, i32* @IPC_CREAT
%12 = load i32, i32* @IPC_EXCL
%13 = or i32 %12, %11
%14 = load i32, i32* @S_IRUSR
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IWUSR
%17 = or i32 %15, %16
%18 = tail call i32 @shmget(i32 %9, i32 %10, i32 %17)
%19 = tail call i64 @shmat(i32 %18, i32 0, i32 0)
%20 = inttoptr i64 %19 to i8*
%21 = tail call i32 @sprintf(i8* dereferenceable(1) %20, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 100)
%22 = tail call i32 @shmdt(i8* %20)
%23 = tail call i32 (...) @fork()
br label %24
24: ; preds = %0, %24
%25 = phi i32 [ 0, %0 ], [ %27, %24 ]
%26 = tail call i32 @print_hundred(i32 %7, i32 %18)
%27 = add nuw nsw i32 %25, 1
%28 = icmp eq i32 %27, 1000000
br i1 %28, label %29, label %24
29: ; preds = %24
%30 = load i32, i32* @IPC_RMID
%31 = tail call i32 @shmctl(i32 %18, i32 %30, i32 0)
%32 = tail call i32 @binary_semaphore_deallocate(i32 %7)
ret i32 0
}