#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*/ memset(void *, int, int);
int /*<<< orphan*/ puts(char *);
void *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ shmdt(void *);
int shmget(int /*<<< orphan*/, int, int);
int main(void) {
  int key;
  void *p;
  key = shmget(IPC_PRIVATE, (1024 * 1024), IPC_CREAT | 0600);
  if (key < 0) {
    puts("shmget failed");
    return 1;
  }
  p = shmat(key, NULL, 0);
  memset(p, 0xfa, (1024 * 1024));
  shmdt(p);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%edx
or     $0x180,%edx
mov    $0x100000,%esi
call   1070 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11a6 <main+0x56>
lea    0xe6b(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    11db <main+0x8b>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0xfa,%esi
mov    $0x100000,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1040 <shmdt@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e8d(%rip),%rax
mov    (%rax),%edi
mov    0x2e74(%rip),%rax
mov    $0x180,%edx
or     (%rax),%edx
mov    $0x100000,%esi
call   1070 <shmget@plt>
test   %eax,%eax
js     11a6 <main+0x56>
xor    %r14d,%r14d
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0xfa,%esi
mov    $0x100000,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
jmp    11b8 <main+0x68>
lea    0xe53(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@.str = [14 x i8] c"shmget failed\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
store i32 0, i32* %1
%4 = load i32, i32* @IPC_PRIVATE
%5 = load i32, i32* @IPC_CREAT
%6 = or i32 %5, 384
%7 = call i32 @shmget(i32 %4, i32 1048576, i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %19
12: ; preds = %0
%13 = load i32, i32* %2
%14 = call i8* @shmat(i32 %13, i32* null, i32 0)
store i8* %14, i8** %3
%15 = load i8*, i8** %3
%16 = call i32 @memset(i8* %15, i32 250, i32 1048576)
%17 = load i8*, i8** %3
%18 = call i32 @shmdt(i8* %17)
store i32 0, i32* %1
br label %19
19: ; preds = %12, %10
%20 = load i32, i32* %1
ret i32 %20
}

LLVM IR (-O2):
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@.str = [14 x i8] c"shmget failed\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 1048576, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %12
8: ; preds = %0
%9 = tail call i8* @shmat(i32 %4, i32* null, i32 0)
%10 = tail call i32 @memset(i8* %9, i32 250, i32 1048576)
%11 = tail call i32 @shmdt(i8* %9)
br label %12
12: ; preds = %8, %6
%13 = phi i32 [ 1, %6 ], [ 0, %8 ]
ret i32 %13
}