#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 scalar_t__ pid_t;
typedef int key_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ shmdt(int *);
int shmget(int, int, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int *sharedInt;
  int shmid;
  key_t key = 5678;
  FILE *fp_fifo;
  pid_t pids[100];
  int i;
  int n = 100;
  if ((shmid = shmget(key, sizeof(int *), 0666)) < 0) {
    perror("shmget");
    exit(EXIT_FAILURE);
  }
  if ((sharedInt = (int *)shmat(shmid, NULL, 0)) == (int *)-1) {
    perror("shmat");
    exit(EXIT_FAILURE);
  }
  for (i = 0; i < n; i++) {
    pids[i] = fork();
    if (pids[i] < 0) {
      perror("fork");
      abort();
    } else if (pids[i] == 0) {
      printf("I am a child: %d with PID: %d\n", i, getpid());
      int *sharedIntStart = sharedInt;
      for (int i = 0; i < 100; i++) {
        *sharedIntStart += 1;
      }
      printf("Child will exit now...\n");
      exit(0);
    }
    wait(NULL);
  }
  int *sharedIntStart = sharedInt;
  printf("%d\n", *sharedIntStart);
  if ((fp_fifo = fopen("RESULT_FIFO", "w")) == NULL) {
    perror("fopen");
    exit(EXIT_FAILURE);
  }
  fprintf(fp_fifo, "%d", *sharedIntStart);
  if (fclose(fp_fifo) == EOF) {
    perror("fclose");
    exit(EXIT_FAILURE);
  }
  shmdt(sharedInt);
  shmctl(shmid, IPC_RMID, NULL);
  wait(NULL);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x380,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x162e,-0x20(%rbp)
movl   $0x64,-0x358(%rbp)
mov    -0x20(%rbp),%edi
mov    $0x8,%esi
mov    $0x1b6,%edx
call   10d0 <shmget@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1242 <main+0x62>
lea    0xdd1(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x1c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10c0 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    127e <main+0x9e>
lea    0xd9c(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
movl   $0x0,-0x354(%rbp)
mov    -0x354(%rbp),%eax
cmp    -0x358(%rbp),%eax
jge    1396 <main+0x1b6>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %rax,%rcx
movslq -0x354(%rbp),%rax
mov    %rcx,-0x350(%rbp,%rax,8)
ff
movslq -0x354(%rbp),%rax
cmpq   $0x0,-0x350(%rbp,%rax,8)
jge    12dc <main+0xfc>
lea    0xd3d(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
movslq -0x354(%rbp),%rax
cmpq   $0x0,-0x350(%rbp,%rax,8)
jne    1374 <main+0x194>
mov    -0x354(%rbp),%eax
mov    %eax,-0x374(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x374(%rbp),%esi
mov    %eax,%edx
lea    0xcfe(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x360(%rbp)
movl   $0x0,-0x364(%rbp)
cmpl   $0x64,-0x364(%rbp)
jge    135f <main+0x17f>
mov    -0x360(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x364(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x364(%rbp)
jmp    1330 <main+0x150>
lea    0xccb(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
jmp    1379 <main+0x199>
xor    %eax,%eax
mov    %eax,%edi
call   10f0 <wait@plt>
mov    -0x354(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x354(%rbp)
jmp    1288 <main+0xa8>
mov    -0x18(%rbp),%rax
mov    %rax,-0x370(%rbp)
mov    -0x370(%rbp),%rax
mov    (%rax),%esi
lea    0xc7c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xc8a(%rip),%rdi
lea    0xc8f(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    13f3 <main+0x213>
lea    0xc77(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2bec(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x370(%rbp),%rax
mov    (%rax),%edx
lea    0xc56(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
mov    0x2bcc(%rip),%rcx
cmp    (%rcx),%rax
jne    143f <main+0x25f>
lea    0xc34(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2ba0(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1040 <shmdt@plt>
mov    -0x1c(%rbp),%edi
mov    0x2b7e(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <shmctl@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10f0 <wait@plt>
mov    0x2b5b(%rip),%rax
mov    (%rax),%eax
add    $0x380,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    $0x162e,%edi
mov    $0x8,%esi
mov    $0x1b6,%edx
call   10e0 <shmget@plt>
test   %eax,%eax
js     131f <main+0x12f>
mov    %eax,%r14d
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     1328 <main+0x138>
mov    %rax,%rbx
xor    %ebp,%ebp
nopl   (%rax)
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
js     12dd <main+0xed>
je     12f0 <main+0x100>
xor    %edi,%edi
call   1100 <wait@plt>
add    $0x1,%ebp
cmp    $0x64,%ebp
jne    1230 <main+0x40>
mov    (%rbx),%esi
lea    0xdcf(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xdc5(%rip),%rdi
lea    0xdca(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1331 <main+0x141>
mov    %rax,%rbp
mov    (%rbx),%edx
lea    0xdb8(%rip),%rsi
mov    %rax,%rdi
call   1090 <fprintf@plt>
mov    %rbp,%rdi
call   1070 <fclose@plt>
mov    0x2d44(%rip),%rcx
cmp    (%rcx),%rax
je     133a <main+0x14a>
mov    %rbx,%rdi
call   1050 <shmdt@plt>
mov    0x2d14(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
xor    %edx,%edx
call   10a0 <shmctl@plt>
xor    %edi,%edi
call   1100 <wait@plt>
mov    0x2cf2(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd29(%rip),%rdi
call   10c0 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xd14(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <printf@plt>
addl   $0x64,(%rbx)
lea    0xd3c(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
lea    0xcda(%rip),%rdi
jmp    1341 <main+0x151>
lea    0xcd8(%rip),%rdi
jmp    1341 <main+0x151>
lea    0xd07(%rip),%rdi
jmp    1341 <main+0x151>
lea    0xd07(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2c8b(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"shmget\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [6 x i8] c"shmat\00"
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [31 x i8] c"I am a child: %d with PID: %d\0A\00"
@.str.4 = [24 x i8] c"Child will exit now...\0A\00"
@.str.5 = [4 x i8] c"%d\0A\00"
@.str.6 = [12 x i8] c"RESULT_FIFO\00"
@.str.7 = [2 x i8] c"w\00"
@.str.8 = [6 x i8] c"fopen\00"
@.str.9 = [3 x i8] c"%d\00"
@EOF = global i64 0
@.str.10 = [7 x i8] c"fclose\00"
@IPC_RMID = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca [100 x i64]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 5678, i32* %8
store i32 100, i32* %12
%16 = load i32, i32* %8
%17 = call i32 @shmget(i32 %16, i32 8, i32 438)
store i32 %17, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %2
%24 = load i32, i32* %7
%25 = call i64 @shmat(i32 %24, i32* null, i32 0)
%26 = inttoptr i64 %25 to i32*
store i32* %26, i32** %6
%27 = icmp eq i32* %26, inttoptr (i64 -1 to i32*)
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %23
store i32 0, i32* %11
br label %33
33: ; preds = %77, %32
%34 = load i32, i32* %11
%35 = load i32, i32* %12
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %80
37: ; preds = %33
%38 = call i64 (...) @fork()
%39 = load i32, i32* %11
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [100 x i64], [100 x i64]* %10, i64 0, i64 %40
store i64 %38, i64* %41
%42 = load i32, i32* %11
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [100 x i64], [100 x i64]* %10, i64 0, i64 %43
%45 = load i64, i64* %44
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %37
%48 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%49 = call i32 (...) @abort()
unreachable
50: ; preds = %37
%51 = load i32, i32* %11
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [100 x i64], [100 x i64]* %10, i64 0, i64 %52
%54 = load i64, i64* %53
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %74
56: ; preds = %50
%57 = load i32, i32* %11
%58 = call i32 (...) @getpid()
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %57, i32 %58)
%60 = load i32*, i32** %6
store i32* %60, i32** %13
store i32 0, i32* %14
br label %61
61: ; preds = %68, %56
%62 = load i32, i32* %14
%63 = icmp slt i32 %62, 100
br i1 %63, label %64, label %71
64: ; preds = %61
%65 = load i32*, i32** %13
%66 = load i32, i32* %65
%67 = add nsw i32 %66, 1
store i32 %67, i32* %65
br label %68
68: ; preds = %64
%69 = load i32, i32* %14
%70 = add nsw i32 %69, 1
store i32 %70, i32* %14
br label %61
71: ; preds = %61
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%73 = call i32 @exit(i32 0)
unreachable
74: ; preds = %50
br label %75
75: ; preds = %74
%76 = call i32 @wait(i32* null)
br label %77
77: ; preds = %75
%78 = load i32, i32* %11
%79 = add nsw i32 %78, 1
store i32 %79, i32* %11
br label %33
80: ; preds = %33
%81 = load i32*, i32** %6
store i32* %81, i32** %15
%82 = load i32*, i32** %15
%83 = load i32, i32* %82
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %83)
%85 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
store i32* %85, i32** %9
%86 = icmp eq i32* %85, null
br i1 %86, label %87, label %91
87: ; preds = %80
%88 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%89 = load i32, i32* @EXIT_FAILURE
%90 = call i32 @exit(i32 %89)
unreachable
91: ; preds = %80
%92 = load i32*, i32** %9
%93 = load i32*, i32** %15
%94 = load i32, i32* %93
%95 = call i32 @fprintf(i32* %92, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i32 %94)
%96 = load i32*, i32** %9
%97 = call i64 @fclose(i32* %96)
%98 = load i64, i64* @EOF
%99 = icmp eq i64 %97, %98
br i1 %99, label %100, label %104
100: ; preds = %91
%101 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0))
%102 = load i32, i32* @EXIT_FAILURE
%103 = call i32 @exit(i32 %102)
unreachable
104: ; preds = %91
%105 = load i32*, i32** %6
%106 = call i32 @shmdt(i32* %105)
%107 = load i32, i32* %7
%108 = load i32, i32* @IPC_RMID
%109 = call i32 @shmctl(i32 %107, i32 %108, i32* null)
%110 = call i32 @wait(i32* null)
%111 = load i32, i32* @EXIT_SUCCESS
ret i32 %111
}

LLVM IR (-O2):
@.str = [7 x i8] c"shmget\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [6 x i8] c"shmat\00"
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [31 x i8] c"I am a child: %d with PID: %d\0A\00"
@.str.5 = [4 x i8] c"%d\0A\00"
@.str.6 = [12 x i8] c"RESULT_FIFO\00"
@.str.7 = [2 x i8] c"w\00"
@.str.8 = [6 x i8] c"fopen\00"
@.str.9 = [3 x i8] c"%d\00"
@EOF = global i64 0
@.str.10 = [7 x i8] c"fclose\00"
@IPC_RMID = global i32 0
@EXIT_SUCCESS = global i32 0
@str = [23 x i8] c"Child will exit now...\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @shmget(i32 5678, i32 8, i32 438)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = tail call i64 @shmat(i32 %3, i32* null, i32 0)
%11 = inttoptr i64 %10 to i32*
%12 = icmp eq i32* %11, inttoptr (i64 -1 to i32*)
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %9, %33
%18 = phi i32 [ %35, %33 ], [ 0, %9 ]
%19 = tail call i64 (...) @fork()
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 (...) @abort()
unreachable
24: ; preds = %17
%25 = icmp eq i64 %19, 0
br i1 %25, label %26, label %33
26: ; preds = %24
%27 = tail call i32 (...) @getpid()
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %18, i32 %27)
%29 = load i32, i32* %11
%30 = add i32 %29, 100
store i32 %30, i32* %11
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%32 = tail call i32 @exit(i32 0)
unreachable
33: ; preds = %24
%34 = tail call i32 @wait(i32* null)
%35 = add nuw nsw i32 %18, 1
%36 = icmp eq i32 %35, 100
br i1 %36, label %37, label %17
37: ; preds = %33
%38 = load i32, i32* %11
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %38)
%40 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %46
42: ; preds = %37
%43 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = tail call i32 @exit(i32 %44)
unreachable
46: ; preds = %37
%47 = load i32, i32* %11
%48 = tail call i32 @fprintf(i32* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i32 %47)
%49 = tail call i64 @fclose(i32* %40)
%50 = load i64, i64* @EOF
%51 = icmp eq i64 %49, %50
br i1 %51, label %52, label %56
52: ; preds = %46
%53 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
%55 = tail call i32 @exit(i32 %54)
unreachable
56: ; preds = %46
%57 = tail call i32 @shmdt(i32* %11)
%58 = load i32, i32* @IPC_RMID
%59 = tail call i32 @shmctl(i32 %3, i32 %58, i32* null)
%60 = tail call i32 @wait(i32* null)
%61 = load i32, i32* @EXIT_SUCCESS
ret i32 %61
}