#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 /*<<< orphan*/ key_t;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ ftok(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int /*<<< orphan*/, int, int);
int main(int argc, char *argv[]) {
  key_t key = ftok("/tmp/mem.temp", 0);
  int shmid = shmget(key, 1048576, IPC_CREAT | 0666);
  char *shm = shmat(shmid, NULL, 0);
  if (shmid < 0) {
    perror("shmget");
    return 1;
  }
  if (shm == (char *)-1) {
    perror("shmat");
    return 2;
  }
  char *s = shm;
  int i;
  for (i = 0; i < 1048576; i++) {
    *s++ = 42;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea3(%rip),%rdi
xor    %esi,%esi
call   1060 <ftok@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x100000,%esi
call   1050 <shmget@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <shmat@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ba <main+0x7a>
lea    0xe65(%rip),%rdi
call   1030 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1226 <main+0xe6>
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
jne    11e3 <main+0xa3>
lea    0xe43(%rip),%rdi
call   1030 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1226 <main+0xe6>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x100000,-0x2c(%rbp)
jge    121f <main+0xdf>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x2a,(%rax)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11f2 <main+0xb2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xea8(%rip),%rdi
xor    %esi,%esi
call   1070 <ftok@plt>
mov    0x2e7a(%rip),%rcx
mov    $0x1b6,%edx
or     (%rcx),%edx
mov    %eax,%edi
mov    $0x100000,%esi
call   1060 <shmget@plt>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
test   %ebx,%ebx
js     11a6 <main+0x56>
cmp    $0xffffffffffffffff,%rax
je     11b9 <main+0x69>
mov    $0x100000,%edx
mov    %rax,%rdi
mov    $0x2a,%esi
call   1030 <memset@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe61(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%eax
pop    %rbx
ret
lea    0xe55(%rip),%rdi
call   1040 <perror@plt>
mov    $0x2,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [14 x i8] c"/tmp/mem.temp\00"
@IPC_CREAT = global i32 0
@.str.1 = [7 x i8] c"shmget\00"
@.str.2 = [6 x i8] c"shmat\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @ftok(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 0)
%4 = load i32, i32* @IPC_CREAT
%5 = or i32 %4, 438
%6 = tail call i32 @shmget(i32 %3, i32 1048576, i32 %5)
%7 = tail call i8* @shmat(i32 %6, i32* null, i32 0)
%8 = icmp slt i32 %6, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %16
11: ; preds = %2
%12 = icmp eq i8* %7, inttoptr (i64 -1 to i8*)
br i1 %12, label %14, label %13
13: ; preds = %11
br label %16
14: ; preds = %11
%15 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %13, %14, %9
%17 = phi i32 [ 1, %9 ], [ 2, %14 ], [ 0, %13 ]
ret i32 %17
}