#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_EXCL;
int O_RDONLY;
int O_RDWR;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ PROT_WRITE;
int S_IRUSR;
int S_IWUSR;
int atoi(char *);
int /*<<< orphan*/ close(int);
unsigned int collatz(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ ftruncate(int, int);
int /*<<< orphan*/ memcpy(void *, unsigned int *, int);
void *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int);
int shm_open(char *, int, int);
int /*<<< orphan*/ shm_unlink(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  if (argc < 2) {
    fprintf(stderr, "Less number of arguments than required.\n");
    exit(-1);
  }
  int num = atoi(argv[1]);
  if (num <= 0) {
    fprintf(stderr, "Positive number required.\n");
    exit(-1);
  }
  pid_t child = fork();
  if (child == -1) {
    fprintf(stderr, "Child creation failed\n");
    exit(-1);
  } else if (child == 0) {
    unsigned int result = collatz(num);
    int shm_fd =
        shm_open("result", O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
    if (shm_fd == -1) {
      fprintf(stderr, "Shared memory creation failed\n");
      exit(-2);
    }
    ftruncate(shm_fd, sizeof(unsigned int));
    void *addr =
        mmap(NULL, sizeof(unsigned int), PROT_WRITE, MAP_SHARED, shm_fd, 0);
    memcpy(addr, &result, sizeof(unsigned int));
    close(shm_fd);
    munmap(addr, sizeof(unsigned int));
  } else {
    wait(NULL);
    int shm_fd = shm_open("result", O_RDONLY, S_IRUSR);
    if (shm_fd == -1) {
      fprintf(stderr, "Shared memory linking failed\n");
      exit(-2);
    }
    void *addr =
        mmap(NULL, sizeof(unsigned int), PROT_READ, MAP_SHARED, shm_fd, 0);
    fprintf(stdout, "%u\n", *(unsigned int *)addr);
    close(shm_fd);
    munmap(addr, sizeof(unsigned int));
    shm_unlink("result");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1211 <main+0x41>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     124c <main+0x7c>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1281 <main+0xb1>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
jne    1362 <main+0x192>
mov    -0x14(%rbp),%edi
call   1030 <collatz@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
mov    0x2d02(%rip),%rax
or     (%rax),%esi
mov    0x2d21(%rip),%rax
or     (%rax),%esi
mov    0x2ce0(%rip),%rax
mov    (%rax),%edx
mov    0x2cff(%rip),%rax
or     (%rax),%edx
lea    0xd91(%rip),%rdi
call   10c0 <shm_open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    12fd <main+0x12d>
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
lea    0xd76(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0xfffffffe,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%edi
mov    $0x4,%esi
call   1050 <ftruncate@plt>
mov    0x2c8f(%rip),%rax
mov    (%rax),%edx
mov    0x2cc6(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%rdi
mov    $0x4,%esi
call   1090 <munmap@plt>
jmp    1421 <main+0x251>
xor    %eax,%eax
mov    %eax,%edi
call   10e0 <wait@plt>
mov    0x2c56(%rip),%rax
mov    (%rax),%esi
mov    0x2c1d(%rip),%rax
mov    (%rax),%edx
lea    0xcd7(%rip),%rdi
call   10c0 <shm_open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    13b7 <main+0x1e7>
mov    0x2c13(%rip),%rax
mov    (%rax),%edi
lea    0xcdb(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0xfffffffe,%edi
call   10b0 <exit@plt>
mov    0x2c32(%rip),%rax
mov    (%rax),%edx
mov    0x2c19(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    0x2ba7(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
lea    0xca7(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%rdi
mov    $0x4,%esi
call   1090 <munmap@plt>
lea    0xc3f(%rip),%rdi
call   10d0 <shm_unlink@plt>
jmp    1426 <main+0x256>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jle    1354 <main+0x184>
mov    0x8(%rsi),%rdi
call   10a0 <atoi@plt>
test   %eax,%eax
jle    1366 <main+0x196>
mov    %eax,%ebx
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
je     12a1 <main+0xd1>
cmp    $0xffffffff,%eax
je     1378 <main+0x1a8>
xor    %edi,%edi
call   10e0 <wait@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
mov    0x2d75(%rip),%rax
mov    (%rax),%edx
lea    0xe2f(%rip),%rdi
call   10c0 <shm_open@plt>
cmp    $0xffffffff,%eax
je     1399 <main+0x1c9>
mov    %eax,%ebp
mov    0x2dad(%rip),%rax
mov    (%rax),%edx
mov    0x2d94(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%edx
lea    0xe2a(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
call   1090 <munmap@plt>
lea    0xdc4(%rip),%rdi
call   10d0 <shm_unlink@plt>
jmp    1349 <main+0x179>
mov    %ebx,%edi
call   1030 <collatz@plt>
mov    %eax,0xc(%rsp)
mov    0x2d45(%rip),%rax
mov    0x2cee(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d0b(%rip),%rax
or     (%rax),%esi
mov    0x2cca(%rip),%rax
mov    0x2ceb(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xd7b(%rip),%rdi
call   10c0 <shm_open@plt>
cmp    $0xffffffff,%eax
je     13ab <main+0x1db>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x4,%esi
call   1050 <ftruncate@plt>
mov    0x2c9d(%rip),%rax
mov    (%rax),%edx
mov    0x2cd4(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r14
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r14,%rdi
mov    $0x4,%esi
call   1090 <munmap@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2c55(%rip),%rax
mov    (%rax),%edi
lea    0xc9c(%rip),%rsi
jmp    1388 <main+0x1b8>
mov    0x2c43(%rip),%rax
mov    (%rax),%edi
lea    0xcb3(%rip),%rsi
jmp    1388 <main+0x1b8>
mov    0x2c31(%rip),%rax
mov    (%rax),%edi
lea    0xcbc(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    0x2c10(%rip),%rax
mov    (%rax),%edi
lea    0xcd8(%rip),%rsi
jmp    13bb <main+0x1eb>
mov    0x2bfe(%rip),%rax
mov    (%rax),%edi
lea    0xca7(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xfffffffe,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [41 x i8] c"Less number of arguments than required.\0A\00"
@.str.1 = [27 x i8] c"Positive number required.\0A\00"
@.str.2 = [23 x i8] c"Child creation failed\0A\00"
@.str.3 = [7 x i8] c"result\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.4 = [31 x i8] c"Shared memory creation failed\0A\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@O_RDONLY = global i32 0
@.str.5 = [30 x i8] c"Shared memory linking failed\0A\00"
@PROT_READ = global i32 0
@stdout = global i32 0
@.str.6 = [4 x i8] c"%u\0A\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 i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp sle i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %19
%31 = call i32 (...) @fork()
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 -1)
unreachable
38: ; preds = %30
%39 = load i32, i32* %7
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %72
41: ; preds = %38
%42 = load i32, i32* %6
%43 = call i32 @collatz(i32 %42)
store i32 %43, i32* %8
%44 = load i32, i32* @O_CREAT
%45 = load i32, i32* @O_EXCL
%46 = or i32 %44, %45
%47 = load i32, i32* @O_RDWR
%48 = or i32 %46, %47
%49 = load i32, i32* @S_IRUSR
%50 = load i32, i32* @S_IWUSR
%51 = or i32 %49, %50
%52 = call i32 @shm_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %48, i32 %51)
store i32 %52, i32* %9
%53 = load i32, i32* %9
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %59
55: ; preds = %41
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%58 = call i32 @exit(i32 -2)
unreachable
59: ; preds = %41
%60 = load i32, i32* %9
%61 = call i32 @ftruncate(i32 %60, i32 4)
%62 = load i32, i32* @PROT_WRITE
%63 = load i32, i32* @MAP_SHARED
%64 = load i32, i32* %9
%65 = call i8* @mmap(i32* null, i32 4, i32 %62, i32 %63, i32 %64, i32 0)
store i8* %65, i8** %10
%66 = load i8*, i8** %10
%67 = call i32 @memcpy(i8* %66, i32* %8, i32 4)
%68 = load i32, i32* %9
%69 = call i32 @close(i32 %68)
%70 = load i8*, i8** %10
%71 = call i32 @munmap(i8* %70, i32 4)
br label %98
72: ; preds = %38
%73 = call i32 @wait(i32* null)
%74 = load i32, i32* @O_RDONLY
%75 = load i32, i32* @S_IRUSR
%76 = call i32 @shm_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %74, i32 %75)
store i32 %76, i32* %11
%77 = load i32, i32* %11
%78 = icmp eq i32 %77, -1
br i1 %78, label %79, label %83
79: ; preds = %72
%80 = load i32, i32* @stderr
%81 = call i32 (i32, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
%82 = call i32 @exit(i32 -2)
unreachable
83: ; preds = %72
%84 = load i32, i32* @PROT_READ
%85 = load i32, i32* @MAP_SHARED
%86 = load i32, i32* %11
%87 = call i8* @mmap(i32* null, i32 4, i32 %84, i32 %85, i32 %86, i32 0)
store i8* %87, i8** %12
%88 = load i32, i32* @stdout
%89 = load i8*, i8** %12
%90 = bitcast i8* %89 to i32*
%91 = load i32, i32* %90
%92 = call i32 (i32, i8*, ...) @fprintf(i32 %88, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %91)
%93 = load i32, i32* %11
%94 = call i32 @close(i32 %93)
%95 = load i8*, i8** %12
%96 = call i32 @munmap(i8* %95, i32 4)
%97 = call i32 @shm_unlink(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %98
98: ; preds = %83, %59
br label %99
99: ; preds = %98
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [41 x i8] c"Less number of arguments than required.\0A\00"
@.str.1 = [27 x i8] c"Positive number required.\0A\00"
@.str.2 = [23 x i8] c"Child creation failed\0A\00"
@.str.3 = [7 x i8] c"result\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.4 = [31 x i8] c"Shared memory creation failed\0A\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@O_RDONLY = global i32 0
@.str.5 = [30 x i8] c"Shared memory linking failed\0A\00"
@PROT_READ = global i32 0
@stdout = global i32 0
@.str.6 = [4 x i8] c"%u\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = icmp slt i32 %12, 1
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %9
%19 = tail call i32 (...) @fork()
switch i32 %19, label %49 [
i32 -1, label %20
i32 0, label %24
]
20: ; preds = %18
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 @exit(i32 -1)
unreachable
24: ; preds = %18
%25 = bitcast i32* %3 to i8*
%26 = tail call i32 @collatz(i32 %12)
store i32 %26, i32* %3
%27 = load i32, i32* @O_CREAT
%28 = load i32, i32* @O_EXCL
%29 = or i32 %28, %27
%30 = load i32, i32* @O_RDWR
%31 = or i32 %29, %30
%32 = load i32, i32* @S_IRUSR
%33 = load i32, i32* @S_IWUSR
%34 = or i32 %33, %32
%35 = tail call i32 @shm_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %31, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %41
37: ; preds = %24
%38 = load i32, i32* @stderr
%39 = tail call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%40 = tail call i32 @exit(i32 -2)
unreachable
41: ; preds = %24
%42 = tail call i32 @ftruncate(i32 %35, i32 4)
%43 = load i32, i32* @PROT_WRITE
%44 = load i32, i32* @MAP_SHARED
%45 = tail call i8* @mmap(i32* null, i32 4, i32 %43, i32 %44, i32 %35, i32 0)
%46 = call i32 @memcpy(i8* %45, i32* %3, i32 4)
%47 = call i32 @close(i32 %35)
%48 = call i32 @munmap(i8* %45, i32 4)
br label %70
49: ; preds = %18
%50 = tail call i32 @wait(i32* null)
%51 = load i32, i32* @O_RDONLY
%52 = load i32, i32* @S_IRUSR
%53 = tail call i32 @shm_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %51, i32 %52)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %59
55: ; preds = %49
%56 = load i32, i32* @stderr
%57 = tail call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
%58 = tail call i32 @exit(i32 -2)
unreachable
59: ; preds = %49
%60 = load i32, i32* @PROT_READ
%61 = load i32, i32* @MAP_SHARED
%62 = tail call i8* @mmap(i32* null, i32 4, i32 %60, i32 %61, i32 %53, i32 0)
%63 = load i32, i32* @stdout
%64 = bitcast i8* %62 to i32*
%65 = load i32, i32* %64
%66 = tail call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %65)
%67 = tail call i32 @close(i32 %53)
%68 = tail call i32 @munmap(i8* %62, i32 4)
%69 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %70
70: ; preds = %41, %59
ret i32 0
}