#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 int key_t;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, int, int);
int /*<<< orphan*/ sleep(int);
main() {
  char c;
  int shmid;
  key_t key;
  char *shm, *s;
  int i;
  key = 5678;
  if ((shmid = shmget(key, 1024 * 1024 * 1024, IPC_CREAT | 0666)) < 0) {
    perror("shmget");
    exit(1);
  }
  if ((shm = shmat(shmid, NULL, 0)) == (char *)-1) {
    perror("shmat");
    exit(1);
  }
  s = shm;
  while (1) {
    for (i = 0; i < 1024 * 1024 * 1024; i++)
      s[i] = 'c';
  }
  sleep(1000000);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x162e,-0x10(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x40000000,%esi
call   1050 <shmget@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1194 <main+0x54>
lea    0xe7b(%rip),%rdi
call   1030 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0xc(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    11cc <main+0x8c>
lea    0xe4a(%rip),%rdi
call   1030 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x40000000,-0x24(%rbp)
jge    1202 <main+0xc2>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x63,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11db <main+0x9b>
jmp    11d4 <main+0x94>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e88(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x162e,%edi
mov    $0x40000000,%esi
call   1060 <shmget@plt>
test   %eax,%eax
js     11a4 <main+0x54>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     11ad <main+0x5d>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    $0x40000000,%edx
mov    %rbx,%rdi
mov    $0x63,%esi
call   1030 <memset@plt>
jmp    1190 <main+0x40>
lea    0xe55(%rip),%rdi
jmp    11b4 <main+0x64>
lea    0xe53(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i32 0, i32* %1
store i32 5678, i32* %4
%8 = load i32, i32* %4
%9 = load i32, i32* @IPC_CREAT
%10 = or i32 %9, 438
%11 = call i32 @shmget(i32 %8, i32 1073741824, i32 %10)
store i32 %11, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32, i32* %3
%18 = call i8* @shmat(i32 %17, i32* null, i32 0)
store i8* %18, i8** %5
%19 = icmp eq i8* %18, inttoptr (i64 -1 to i8*)
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = load i8*, i8** %5
store i8* %24, i8** %6
br label %25
25: ; preds = %23, %37
store i32 0, i32* %7
br label %26
26: ; preds = %34, %25
%27 = load i32, i32* %7
%28 = icmp slt i32 %27, 1073741824
br i1 %28, label %29, label %37
29: ; preds = %26
%30 = load i8*, i8** %6
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 99, i8* %33
br label %34
34: ; preds = %29
%35 = load i32, i32* %7
%36 = add nsw i32 %35, 1
store i32 %36, i32* %7
br label %26
37: ; preds = %26
br label %25
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
define i32 @main() {
%1 = load i32, i32* @IPC_CREAT
%2 = or i32 %1, 438
%3 = tail call i32 @shmget(i32 5678, i32 1073741824, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = tail call i8* @shmat(i32 %3, i32* null, i32 0)
%10 = icmp eq i8* %9, inttoptr (i64 -1 to i8*)
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8, %14
br label %14
}