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

/* Variables and functions */
scalar_t__ ECHILD;
int MAP_ANONYMOUS;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ PTHREAD_PROCESS_SHARED;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ *mmap(int /*<<< orphan*/ *, int, int, int, int,
                         int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ pthread_mutex_destroy(int /*<<< orphan*/ *);
scalar_t__ pthread_mutex_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_unlock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutexattr_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutexattr_setpshared(int /*<<< orphan*/ *,
                                                int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
scalar_t__ wait();
int main() {
  pid_t forkResult;
  static pthread_mutex_t *lock;
  lock = mmap(NULL, sizeof *lock, PROT_READ | PROT_WRITE,
              MAP_SHARED | MAP_ANONYMOUS, -1, 0);
  pthread_mutexattr_t attr;
  pthread_mutexattr_init(&attr);
  pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
  int i = 0;
  if (pthread_mutex_init(lock, &attr) != 0) {
    printf("cannot initialize lock\n");
    return -2;
  }
  for (i = 0; i < 200; i++) {
    forkResult = fork();
    if (forkResult == 0) {
      printf("A new fork with ID %d is spawned\n", getpid());
      break;
    } else if (forkResult < 0) {
      printf("Error number : %d\n", forkResult);
      return -1;
    } else {
    }
  }
  if (forkResult > 0) {
    while (wait()) {
      if (errno == ECHILD)
        break;
    }
    printf("Finished \n");
    pthread_mutex_destroy(lock);
    return 0;
  } else if (forkResult == 0) {
    pthread_mutex_lock(lock);
    usleep(200000);
    printf("Process with PID: %d has exited\n", getpid());
    pthread_mutex_unlock(lock);
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
mov    0x2dd1(%rip),%rax
or     (%rax),%edx
mov    0x2df0(%rip),%rax
mov    (%rax),%ecx
mov    0x2dd7(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,0x2e6f(%rip)
lea    -0x14(%rbp),%rdi
call   10a0 <pthread_mutexattr_init@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdi
call   1070 <pthread_mutexattr_setpshared@plt>
movl   $0x0,-0x18(%rbp)
mov    0x2e46(%rip),%rdi
lea    -0x14(%rbp),%rsi
call   10b0 <pthread_mutex_init@plt>
cmp    $0x0,%rax
je     1277 <main+0xa7>
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    13be <main+0x1ee>
movl   $0x0,-0x18(%rbp)
cmpl   $0xc8,-0x18(%rbp)
jge    1303 <main+0x133>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    12bd <main+0xed>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1303 <main+0x133>
cmpq   $0x0,-0x10(%rbp)
jge    12e6 <main+0x116>
mov    -0x10(%rbp),%rsi
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13be <main+0x1ee>
jmp    12eb <main+0x11b>
jmp    12f0 <main+0x120>
jmp    12f5 <main+0x125>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    127e <main+0xae>
cmpq   $0x0,-0x10(%rbp)
jle    136e <main+0x19e>
jmp    1313 <main+0x143>
mov    $0x0,%al
call   10c0 <wait@plt>
cmp    $0x0,%rax
je     1348 <main+0x178>
mov    0x2c9d(%rip),%rax
mov    (%rax),%rax
mov    0x2c7b(%rip),%rcx
cmp    (%rcx),%rax
jne    1343 <main+0x173>
jmp    1348 <main+0x178>
jmp    1313 <main+0x143>
lea    0xcfe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d33(%rip),%rdi
call   1060 <pthread_mutex_destroy@plt>
movl   $0x0,-0x4(%rbp)
jmp    13be <main+0x1ee>
cmpq   $0x0,-0x10(%rbp)
jne    13b9 <main+0x1e9>
mov    0x2d10(%rip),%rdi
call   10e0 <pthread_mutex_lock@plt>
mov    $0x30d40,%edi
call   10f0 <usleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xcb9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2ce3(%rip),%rdi
call   1080 <pthread_mutex_unlock@plt>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    13be <main+0x1ee>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e0d(%rip),%rax
mov    0x2dce(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2deb(%rip),%rax
mov    0x2dd4(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,0x2e74(%rip)
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   10b0 <pthread_mutexattr_init@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <pthread_mutexattr_setpshared@plt>
mov    0x2e4f(%rip),%rdi
mov    %rbx,%rsi
call   10c0 <pthread_mutex_init@plt>
test   %rax,%rax
je     1269 <main+0x89>
lea    0xe03(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffe,%eax
jmp    12e6 <main+0x106>
mov    $0xc8,%ebx
xchg   %ax,%ax
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
je     12ee <main+0x10e>
js     12d0 <main+0xf0>
add    $0xffffffff,%ebx
jne    1270 <main+0x90>
mov    0x2d3e(%rip),%r14
mov    0x2d1f(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10d0 <wait@plt>
test   %rax,%rax
je     12b4 <main+0xd4>
mov    (%r14),%rax
cmp    (%rbx),%rax
jne    12a0 <main+0xc0>
lea    0xd9b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dd1(%rip),%rdi
call   1070 <pthread_mutex_destroy@plt>
xor    %eax,%eax
jmp    12e6 <main+0x106>
lea    0xd4b(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd04(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d8c(%rip),%rdi
call   10f0 <pthread_mutex_lock@plt>
mov    $0x30d40,%edi
call   1100 <usleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd0c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d5f(%rip),%rdi
call   1090 <pthread_mutex_unlock@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@main.lock = internal global i32* null
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@PTHREAD_PROCESS_SHARED = global i32 0
@.str = [24 x i8] c"cannot initialize lock\0A\00"
@.str.1 = [34 x i8] c"A new fork with ID %d is spawned\0A\00"
@.str.2 = [19 x i8] c"Error number : %d\0A\00"
@errno = global i64 0
@ECHILD = global i64 0
@.str.3 = [11 x i8] c"Finished \0A\00"
@.str.4 = [33 x i8] c"Process with PID: %d has exited\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @PROT_WRITE
%7 = or i32 %5, %6
%8 = load i32, i32* @MAP_SHARED
%9 = load i32, i32* @MAP_ANONYMOUS
%10 = or i32 %8, %9
%11 = call i32* @mmap(i32* null, i32 4, i32 %7, i32 %10, i32 -1, i32 0)
store i32* %11, i32** @main.lock
%12 = call i32 @pthread_mutexattr_init(i32* %3)
%13 = load i32, i32* @PTHREAD_PROCESS_SHARED
%14 = call i32 @pthread_mutexattr_setpshared(i32* %3, i32 %13)
store i32 0, i32* %4
%15 = load i32*, i32** @main.lock
%16 = call i64 @pthread_mutex_init(i32* %15, i32* %3)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %0
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -2, i32* %1
br label %73
20: ; preds = %0
store i32 0, i32* %4
br label %21
21: ; preds = %40, %20
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 200
br i1 %23, label %24, label %43
24: ; preds = %21
%25 = call i64 (...) @fork()
store i64 %25, i64* %2
%26 = load i64, i64* %2
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = call i32 (...) @getpid()
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %29)
br label %43
31: ; preds = %24
%32 = load i64, i64* %2
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i64, i64* %2
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i64 %35)
store i32 -1, i32* %1
br label %73
37: ; preds = %31
br label %38
38: ; preds = %37
br label %39
39: ; preds = %38
br label %40
40: ; preds = %39
%41 = load i32, i32* %4
%42 = add nsw i32 %41, 1
store i32 %42, i32* %4
br label %21
43: ; preds = %28, %21
%44 = load i64, i64* %2
%45 = icmp sgt i64 %44, 0
br i1 %45, label %46, label %60
46: ; preds = %43
br label %47
47: ; preds = %55, %46
%48 = call i64 (...) @wait()
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %56
50: ; preds = %47
%51 = load i64, i64* @errno
%52 = load i64, i64* @ECHILD
%53 = icmp eq i64 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %50
br label %56
55: ; preds = %50
br label %47
56: ; preds = %54, %47
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%58 = load i32*, i32** @main.lock
%59 = call i32 @pthread_mutex_destroy(i32* %58)
store i32 0, i32* %1
br label %73
60: ; preds = %43
%61 = load i64, i64* %2
%62 = icmp eq i64 %61, 0
br i1 %62, label %63, label %72
63: ; preds = %60
%64 = load i32*, i32** @main.lock
%65 = call i32 @pthread_mutex_lock(i32* %64)
%66 = call i32 @usleep(i32 200000)
%67 = call i32 (...) @getpid()
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %67)
%69 = load i32*, i32** @main.lock
%70 = call i32 @pthread_mutex_unlock(i32* %69)
%71 = call i32 @exit(i32 0)
unreachable
72: ; preds = %60
br label %73
73: ; preds = %18, %34, %56, %72
%74 = load i32, i32* %1
ret i32 %74
}

LLVM IR (-O2):
@main.lock = internal unnamed_addr global i32* null
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@PTHREAD_PROCESS_SHARED = global i32 0
@.str.1 = [34 x i8] c"A new fork with ID %d is spawned\0A\00"
@.str.2 = [19 x i8] c"Error number : %d\0A\00"
@errno = global i64 0
@ECHILD = global i64 0
@.str.4 = [33 x i8] c"Process with PID: %d has exited\0A\00"
@str = [10 x i8] c"Finished \00"
@str.5 = [23 x i8] c"cannot initialize lock\00"
define i32 @main() {
%1 = alloca i32
%2 = load i32, i32* @PROT_READ
%3 = load i32, i32* @PROT_WRITE
%4 = or i32 %3, %2
%5 = load i32, i32* @MAP_SHARED
%6 = load i32, i32* @MAP_ANONYMOUS
%7 = or i32 %6, %5
%8 = tail call i32* @mmap(i32* null, i32 4, i32 %4, i32 %7, i32 -1, i32 0)
store i32* %8, i32** @main.lock
%9 = bitcast i32* %1 to i8*
%10 = call i32 @pthread_mutexattr_init(i32* %1)
%11 = load i32, i32* @PTHREAD_PROCESS_SHARED
%12 = call i32 @pthread_mutexattr_setpshared(i32* %1, i32 %11)
%13 = load i32*, i32** @main.lock
%14 = call i64 @pthread_mutex_init(i32* %13, i32* %1)
%15 = icmp eq i64 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %0
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.5, i64 0, i64 0))
br label %51
18: ; preds = %25
%19 = add nuw nsw i32 %22, 1
%20 = icmp eq i32 %19, 200
br i1 %20, label %29, label %21
21: ; preds = %0, %18
%22 = phi i32 [ %19, %18 ], [ 0, %0 ]
%23 = call i64 (...) @fork()
%24 = icmp eq i64 %23, 0
br i1 %24, label %40, label %25
25: ; preds = %21
%26 = icmp slt i64 %23, 0
br i1 %26, label %27, label %18
27: ; preds = %25
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i64 %23)
br label %51
29: ; preds = %18, %32
%30 = call i64 (...) @wait()
%31 = icmp eq i64 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %29
%33 = load i64, i64* @errno
%34 = load i64, i64* @ECHILD
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %29
36: ; preds = %32, %29
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%38 = load i32*, i32** @main.lock
%39 = call i32 @pthread_mutex_destroy(i32* %38)
br label %51
40: ; preds = %21
%41 = call i32 (...) @getpid()
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %41)
%43 = load i32*, i32** @main.lock
%44 = call i32 @pthread_mutex_lock(i32* %43)
%45 = call i32 @usleep(i32 200000)
%46 = call i32 (...) @getpid()
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %46)
%48 = load i32*, i32** @main.lock
%49 = call i32 @pthread_mutex_unlock(i32* %48)
%50 = call i32 @exit(i32 0)
unreachable
51: ; preds = %36, %27, %16
%52 = phi i32 [ -2, %16 ], [ 0, %36 ], [ -1, %27 ]
ret i32 %52
}