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

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ IPC_PRIVATE;
int /*<<< orphan*/ printf(char *, ...);
void *shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
int shmget(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ sleep(int);
int main() {
  int i;
  int shmid;
  void *shared;
  shmid = shmget(IPC_PRIVATE, 4000, (IPC_CREAT | 0600));
  printf("shmid=%d\n", shmid);
  shared = shmat(shmid, 0, 0);
  printf("shared=%d\n", (unsigned)shared);
  *(unsigned *)shared = 0;
  for (i = 0; i < 1000; i++)
    *(int *)(shared + i) = 0;
  printf("sleeping... (shared value=0)\n");
  sleep(10);
  *(unsigned *)shared = 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%edi
mov    0x2e79(%rip),%rax
mov    (%rax),%edx
or     $0x180,%edx
mov    $0xfa0,%esi
call   1050 <shmget@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1040 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%esi
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x8(%rbp)
cmpl   $0x3e8,-0x8(%rbp)
jge    11e4 <main+0xa4>
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
movl   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11ba <main+0x7a>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xa,%edi
call   1060 <sleep@plt>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e90(%rip),%rax
mov    (%rax),%edi
mov    0x2e77(%rip),%rax
mov    $0x180,%edx
or     (%rax),%edx
mov    $0xfa0,%esi
call   1060 <shmget@plt>
mov    %eax,%ebx
lea    0xe85(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,%rbx
lea    0xe71(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x0,(%rbx)
mov    $0x9,%eax
nopl   (%rax)
movl   $0x0,-0x9(%rbx,%rax,1)
movl   $0x0,-0x8(%rbx,%rax,1)
movl   $0x0,-0x7(%rbx,%rax,1)
movl   $0x0,-0x6(%rbx,%rax,1)
movl   $0x0,-0x5(%rbx,%rax,1)
movl   $0x0,-0x4(%rbx,%rax,1)
movl   $0x0,-0x3(%rbx,%rax,1)
movl   $0x0,-0x2(%rbx,%rax,1)
movl   $0x0,-0x1(%rbx,%rax,1)
movl   $0x0,(%rbx,%rax,1)
add    $0xa,%rax
cmp    $0x3f1,%rax
jne    11b0 <main+0x60>
lea    0xe03(%rip),%rdi
call   1030 <puts@plt>
mov    $0xa,%edi
call   1070 <sleep@plt>
movl   $0x1,(%rbx)
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@.str = [10 x i8] c"shmid=%d\0A\00"
@.str.1 = [11 x i8] c"shared=%d\0A\00"
@.str.2 = [30 x i8] c"sleeping... (shared value=0)\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %1
%5 = load i32, i32* @IPC_PRIVATE
%6 = load i32, i32* @IPC_CREAT
%7 = or i32 %6, 384
%8 = call i32 @shmget(i32 %5, i32 4000, i32 %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* %3
%12 = call i8* @shmat(i32 %11, i32 0, i32 0)
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = ptrtoint i8* %13 to i32
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = load i8*, i8** %4
%17 = bitcast i8* %16 to i32*
store i32 0, i32* %17
store i32 0, i32* %2
br label %18
18: ; preds = %27, %0
%19 = load i32, i32* %2
%20 = icmp slt i32 %19, 1000
br i1 %20, label %21, label %30
21: ; preds = %18
%22 = load i8*, i8** %4
%23 = load i32, i32* %2
%24 = sext i32 %23 to i64
%25 = getelementptr i8, i8* %22, i64 %24
%26 = bitcast i8* %25 to i32*
store i32 0, i32* %26
br label %27
27: ; preds = %21
%28 = load i32, i32* %2
%29 = add nsw i32 %28, 1
store i32 %29, i32* %2
br label %18
30: ; preds = %18
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @sleep(i32 10)
%33 = load i8*, i8** %4
%34 = bitcast i8* %33 to i32*
store i32 1, i32* %34
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@.str = [10 x i8] c"shmid=%d\0A\00"
@.str.1 = [11 x i8] c"shared=%d\0A\00"
@str = [29 x i8] c"sleeping... (shared value=0)\00"
define i32 @main() {
%1 = load i32, i32* @IPC_PRIVATE
%2 = load i32, i32* @IPC_CREAT
%3 = or i32 %2, 384
%4 = tail call i32 @shmget(i32 %1, i32 4000, i32 %3)
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i8* @shmat(i32 %4, i32 0, i32 0)
%7 = ptrtoint i8* %6 to i64
%8 = trunc i64 %7 to i32
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = bitcast i8* %6 to i32*
store i32 0, i32* %10
br label %11
11: ; preds = %11, %0
%12 = phi i64 [ 0, %0 ], [ %42, %11 ]
%13 = getelementptr i8, i8* %6, i64 %12
%14 = bitcast i8* %13 to i32*
store i32 0, i32* %14
%15 = or i64 %12, 1
%16 = getelementptr i8, i8* %6, i64 %15
%17 = bitcast i8* %16 to i32*
store i32 0, i32* %17
%18 = add nuw nsw i64 %12, 2
%19 = getelementptr i8, i8* %6, i64 %18
%20 = bitcast i8* %19 to i32*
store i32 0, i32* %20
%21 = add nuw nsw i64 %12, 3
%22 = getelementptr i8, i8* %6, i64 %21
%23 = bitcast i8* %22 to i32*
store i32 0, i32* %23
%24 = add nuw nsw i64 %12, 4
%25 = getelementptr i8, i8* %6, i64 %24
%26 = bitcast i8* %25 to i32*
store i32 0, i32* %26
%27 = add nuw nsw i64 %12, 5
%28 = getelementptr i8, i8* %6, i64 %27
%29 = bitcast i8* %28 to i32*
store i32 0, i32* %29
%30 = add nuw nsw i64 %12, 6
%31 = getelementptr i8, i8* %6, i64 %30
%32 = bitcast i8* %31 to i32*
store i32 0, i32* %32
%33 = add nuw nsw i64 %12, 7
%34 = getelementptr i8, i8* %6, i64 %33
%35 = bitcast i8* %34 to i32*
store i32 0, i32* %35
%36 = add nuw nsw i64 %12, 8
%37 = getelementptr i8, i8* %6, i64 %36
%38 = bitcast i8* %37 to i32*
store i32 0, i32* %38
%39 = add nuw nsw i64 %12, 9
%40 = getelementptr i8, i8* %6, i64 %39
%41 = bitcast i8* %40 to i32*
store i32 0, i32* %41
%42 = add nuw nsw i64 %12, 10
%43 = icmp eq i64 %42, 1000
br i1 %43, label %44, label %11
44: ; preds = %11
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%46 = tail call i32 @sleep(i32 10)
store i32 1, i32* %10
ret i32 0
}