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

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ IPC_STAT;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ child(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ parent(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
void *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
int shmget(int /*<<< orphan*/, int, int);
int main(int argc, char **argv) {
  int shmid;
  if ((shmid = shmget((key_t)9979, 1024, IPC_CREAT | 0666)) < 0) {
    perror("shmget");
    exit(1);
  }
  struct shmid_ds shmid_ds;
  if (shmctl(shmid, IPC_STAT, &shmid_ds) == -1) {
    perror("shmctl: shmctl failed");
    exit(1);
  }
  printf("Shmid: %d\n", shmid);
  void *addr = shmat(shmid, NULL, 0);
  if (addr == (void *)-1) {
    perror("main: shmat");
    abort();
  }
  memset(addr, 0, 1024);
  if (fork() == 0) {
    child(shmid);
  } else {
    parent(shmid);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x26fb,%edi
mov    $0x400,%esi
call   10b0 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1206 <main+0x56>
lea    0xe09(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1080 <shmctl@plt>
cmp    $0xffffffff,%eax
jne    123a <main+0x8a>
lea    0xddc(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%esi
lea    0xdd9(%rip),%rdi
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10a0 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
jne    127f <main+0xcf>
lea    0xdb5(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1050 <abort@plt>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
cmp    $0x0,%rax
jne    12ad <main+0xfd>
mov    -0x14(%rbp),%edi
call   1030 <child@plt>
jmp    12b5 <main+0x105>
mov    -0x14(%rbp),%edi
call   1040 <parent@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e1c(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x26fb,%edi
mov    $0x400,%esi
call   10b0 <shmget@plt>
test   %eax,%eax
js     1242 <main+0x92>
mov    %eax,%ebx
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1080 <shmctl@plt>
cmp    $0xffffffff,%eax
je     124b <main+0x9b>
lea    0xe24(%rip),%rdi
mov    %ebx,%esi
call   1060 <printf@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     1261 <main+0xb1>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %ebx,%edi
test   %rax,%rax
je     1235 <main+0x85>
call   1040 <parent@plt>
jmp    123a <main+0x8a>
call   1030 <child@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdb7(%rip),%rdi
jmp    1252 <main+0xa2>
lea    0xdb5(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xdc0(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   1050 <abort@plt>

LLVM IR (-O0):
%struct.shmid_ds = type { i32 }
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@IPC_STAT = global i32 0
@.str.1 = [22 x i8] c"shmctl: shmctl failed\00"
@.str.2 = [11 x i8] c"Shmid: %d\0A\00"
@.str.3 = [12 x i8] c"main: shmat\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.shmid_ds
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @IPC_CREAT
%10 = or i32 %9, 438
%11 = call i32 @shmget(i32 9979, i32 1024, i32 %10)
store i32 %11, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%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 = %2
%17 = load i32, i32* %6
%18 = load i32, i32* @IPC_STAT
%19 = call i32 @shmctl(i32 %17, i32 %18, %struct.shmid_ds* %7)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %16
%25 = load i32, i32* %6
%26 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %6
%28 = call i8* @shmat(i32 %27, i32* null, i32 0)
store i8* %28, i8** %8
%29 = load i8*, i8** %8
%30 = icmp eq i8* %29, inttoptr (i64 -1 to i8*)
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 (...) @abort()
unreachable
34: ; preds = %24
%35 = load i8*, i8** %8
%36 = call i32 @memset(i8* %35, i32 0, i32 1024)
%37 = call i64 (...) @fork()
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = load i32, i32* %6
%41 = call i32 @child(i32 %40)
br label %45
42: ; preds = %34
%43 = load i32, i32* %6
%44 = call i32 @parent(i32 %43)
br label %45
45: ; preds = %42, %39
ret i32 0
}

LLVM IR (-O2):
%struct.shmid_ds = type { i32 }
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@IPC_STAT = global i32 0
@.str.1 = [22 x i8] c"shmctl: shmctl failed\00"
@.str.2 = [11 x i8] c"Shmid: %d\0A\00"
@.str.3 = [12 x i8] c"main: shmat\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.shmid_ds
%4 = load i32, i32* @IPC_CREAT
%5 = or i32 %4, 438
%6 = tail call i32 @shmget(i32 9979, i32 1024, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = bitcast %struct.shmid_ds* %3 to i8*
%13 = load i32, i32* @IPC_STAT
%14 = call i32 @shmctl(i32 %6, i32 %13, %struct.shmid_ds* %3)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %11
%20 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %6)
%21 = call i8* @shmat(i32 %6, i32* null, i32 0)
%22 = icmp eq i8* %21, inttoptr (i64 -1 to i8*)
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 (...) @abort()
unreachable
26: ; preds = %19
%27 = call i32 @memset(i8* %21, i32 0, i32 1024)
%28 = call i64 (...) @fork()
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @child(i32 %6)
br label %34
32: ; preds = %26
%33 = call i32 @parent(i32 %6)
br label %34
34: ; preds = %32, %30
ret i32 0
}