#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int atoi(char *);
int collatz(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ ftruncate(int, int const);
void *mmap(int /*<<< orphan*/, int const, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int shm_open(char const *, int, int);
scalar_t__ sprintf(void *, char *, ...);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc != 2) {
    if (argc == 1) {
      fprintf(stderr, "No argument\n");
      exit(1);
    } else {
      fprintf(stderr, "One argument only\n");
      exit(1);
    }
  }
  if (atoi(argv[1]) <= 0) {
    fprintf(stderr, "Only positive integers\n");
    exit(1);
  }
  int number = atoi(argv[1]);
  const int SIZE = 4096;
  const char *Obj = "Shm";
  int shm_fd;
  void *ptr;
  shm_fd = shm_open(Obj, O_CREAT | O_RDWR, 0666);
  ftruncate(shm_fd, SIZE);
  ptr = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
  if (ptr == MAP_FAILED) {
    printf("Map failed\n");
    return -1;
  }
  int arr[SIZE];
  char str[SIZE];
  int j = 1;
  arr[0] = number;
  while (number != 1) {
    number = collatz(number);
    arr[j] = number;
    j++;
  }
  int i = 0;
  int k = 0;
  for (i = 0; arr[i] >= 1; i++) {
    k += sprintf(&str[k], "%d ", arr[i]);
  }
  sprintf(ptr, "%s", str);
  printf("Producer: Writing the sequence to the shared memory is done! \n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11f8 <main+0x68>
cmpl   $0x1,-0x8(%rbp)
jne    11d9 <main+0x49>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
cmp    $0x0,%eax
jg     122d <main+0x9d>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
movl   $0x1000,-0x18(%rbp)
lea    0xded(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
mov    0x2d6d(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   10b0 <shm_open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    $0x1000,%esi
call   1060 <ftruncate@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edx
mov    0x2d29(%rip),%rax
or     (%rax),%edx
mov    0x2d48(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x1000,%esi
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2cf3(%rip),%rcx
cmp    (%rcx),%rax
jne    12de <main+0x14e>
lea    0xd6f(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13db <main+0x24b>
mov    %rsp,%rax
mov    %rax,-0x38(%rbp)
mov    %rsp,%rax
add    $0xffffffffffffc000,%rax
mov    %rax,-0x58(%rbp)
mov    %rax,%rsp
mov    %rsp,%rcx
add    $0xfffffffffffff000,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
movl   $0x1,-0x3c(%rbp)
mov    -0x14(%rbp),%ecx
mov    %ecx,(%rax)
cmpl   $0x1,-0x14(%rbp)
je     1345 <main+0x1b5>
mov    -0x14(%rbp),%edi
call   1030 <collatz@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
mov    %ecx,-0x14(%rbp)
mov    -0x14(%rbp),%edx
movslq -0x3c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1312 <main+0x182>
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x58(%rbp),%rax
movslq -0x40(%rbp),%rcx
cmpl   $0x1,(%rax,%rcx,4)
jl     13ab <main+0x21b>
mov    -0x58(%rbp),%rax
mov    -0x50(%rbp),%rdi
movslq -0x44(%rbp),%rcx
add    %rcx,%rdi
movslq -0x40(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xcbf(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    %rax,%rcx
movslq -0x44(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    135a <main+0x1ca>
mov    -0x50(%rbp),%rdx
mov    -0x30(%rbp),%rdi
lea    0xc92(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
lea    0xc87(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    130f <main+0x17f>
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
test   %eax,%eax
jle    1326 <main+0x196>
mov    %eax,%r14d
mov    0x2e33(%rip),%rax
mov    0x2e04(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe61(%rip),%rdi
mov    $0x1b6,%edx
call   10b0 <shm_open@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x1000,%esi
call   1060 <ftruncate@plt>
mov    0x2dfa(%rip),%rax
mov    0x2dbb(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dd8(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    0x2d8d(%rip),%rcx
cmp    (%rcx),%rax
je     12ed <main+0x15d>
mov    %rax,-0x30(%rbp)
mov    %rsp,-0x38(%rbp)
mov    %rsp,%rax
lea    -0x4000(%rax),%r13
mov    %r13,%rsp
mov    %rsp,%r15
add    $0xfffffffffffff000,%r15
mov    %r15,%rsp
mov    %r14d,-0x4000(%rax)
cmp    $0x1,%r14d
je     1287 <main+0xf7>
lea    0x4(%r13),%rbx
mov    %r14d,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %eax,%edi
call   1030 <collatz@plt>
mov    %eax,(%rbx)
add    $0x4,%rbx
cmp    $0x1,%eax
jne    1270 <main+0xe0>
test   %r14d,%r14d
jle    12c4 <main+0x134>
add    $0x4,%r13
lea    0xdaa(%rip),%r12
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movslq %eax,%rbx
lea    (%r15,%rbx,1),%rdi
mov    %r12,%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1090 <sprintf@plt>
add    %rbx,%rax
mov    0x0(%r13),%r14d
add    $0x4,%r13
test   %r14d,%r14d
jg     12a0 <main+0x110>
lea    0xd75(%rip),%rsi
xor    %ebx,%ebx
mov    -0x30(%rbp),%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1090 <sprintf@plt>
lea    0xd61(%rip),%rdi
call   1040 <puts@plt>
mov    -0x38(%rbp),%rsp
jmp    12fe <main+0x16e>
lea    0xd8d(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2caa(%rip),%rax
cmp    $0x1,%edi
jne    1338 <main+0x1a8>
mov    (%rax),%edi
lea    0xcdc(%rip),%rsi
jmp    1341 <main+0x1b1>
mov    0x2c93(%rip),%rax
mov    (%rax),%edi
lea    0xcea(%rip),%rsi
jmp    1341 <main+0x1b1>
mov    (%rax),%edi
lea    0xccc(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [13 x i8] c"No argument\0A\00"
@.str.1 = [19 x i8] c"One argument only\0A\00"
@.str.2 = [24 x i8] c"Only positive integers\0A\00"
@.str.3 = [4 x i8] c"Shm\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [12 x i8] c"Map failed\0A\00"
@.str.5 = [4 x i8] c"%d \00"
@.str.6 = [3 x i8] c"%s\00"
@.str.7 = [63 x i8] c"Producer: Writing the sequence to the shared memory is done! \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 i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 2
br i1 %16, label %17, label %28
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %2
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
%33 = icmp sle i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %28
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %28
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = call i32 @atoi(i8* %41)
store i32 %42, i32* %6
store i32 4096, i32* %7
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8** %8
%43 = load i8*, i8** %8
%44 = load i32, i32* @O_CREAT
%45 = load i32, i32* @O_RDWR
%46 = or i32 %44, %45
%47 = call i32 @shm_open(i8* %43, i32 %46, i32 438)
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = call i32 @ftruncate(i32 %48, i32 4096)
%50 = load i32, i32* @PROT_READ
%51 = load i32, i32* @PROT_WRITE
%52 = or i32 %50, %51
%53 = load i32, i32* @MAP_SHARED
%54 = load i32, i32* %9
%55 = call i8* @mmap(i32 0, i32 4096, i32 %52, i32 %53, i32 %54, i32 0)
store i8* %55, i8** %10
%56 = load i8*, i8** %10
%57 = load i8*, i8** @MAP_FAILED
%58 = icmp eq i8* %56, %57
br i1 %58, label %59, label %61
59: ; preds = %38
%60 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %107
61: ; preds = %38
%62 = call i8* @llvm.stacksave()
store i8* %62, i8** %11
%63 = alloca i32, i64 4096
%64 = alloca i8, i64 4096
store i32 1, i32* %12
%65 = load i32, i32* %6
%66 = getelementptr inbounds i32, i32* %63, i64 0
store i32 %65, i32* %66
br label %67
67: ; preds = %70, %61
%68 = load i32, i32* %6
%69 = icmp ne i32 %68, 1
br i1 %69, label %70, label %79
70: ; preds = %67
%71 = load i32, i32* %6
%72 = call i32 @collatz(i32 %71)
store i32 %72, i32* %6
%73 = load i32, i32* %6
%74 = load i32, i32* %12
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i32, i32* %63, i64 %75
store i32 %73, i32* %76
%77 = load i32, i32* %12
%78 = add nsw i32 %77, 1
store i32 %78, i32* %12
br label %67
79: ; preds = %67
store i32 0, i32* %13
store i32 0, i32* %14
store i32 0, i32* %13
br label %80
80: ; preds = %99, %79
%81 = load i32, i32* %13
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i32, i32* %63, i64 %82
%84 = load i32, i32* %83
%85 = icmp sge i32 %84, 1
br i1 %85, label %86, label %102
86: ; preds = %80
%87 = load i32, i32* %14
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds i8, i8* %64, i64 %88
%90 = load i32, i32* %13
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds i32, i32* %63, i64 %91
%93 = load i32, i32* %92
%94 = call i64 (i8*, i8*, ...) @sprintf(i8* %89, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %93)
%95 = load i32, i32* %14
%96 = sext i32 %95 to i64
%97 = add nsw i64 %96, %94
%98 = trunc i64 %97 to i32
store i32 %98, i32* %14
br label %99
99: ; preds = %86
%100 = load i32, i32* %13
%101 = add nsw i32 %100, 1
store i32 %101, i32* %13
br label %80
102: ; preds = %80
%103 = load i8*, i8** %10
%104 = call i64 (i8*, i8*, ...) @sprintf(i8* %103, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %64)
%105 = call i32 @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %3
%106 = load i8*, i8** %11
br label %107
107: ; preds = %102, %59
%108 = load i32, i32* %3
ret i32 %108
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [13 x i8] c"No argument\0A\00"
@.str.1 = [19 x i8] c"One argument only\0A\00"
@.str.2 = [24 x i8] c"Only positive integers\0A\00"
@.str.3 = [4 x i8] c"Shm\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [4 x i8] c"%d \00"
@.str.6 = [3 x i8] c"%s\00"
@str = [62 x i8] c"Producer: Writing the sequence to the shared memory is done! \00"
@str.8 = [11 x i8] c"Map failed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
switch i32 %0, label %7 [
i32 2, label %11
i32 1, label %3
]
3: ; preds = %2
%4 = load i32, i32* @stderr
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = icmp slt i32 %14, 1
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %11
%21 = load i32, i32* @O_CREAT
%22 = load i32, i32* @O_RDWR
%23 = or i32 %22, %21
%24 = tail call i32 @shm_open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %23, i32 438)
%25 = tail call i32 @ftruncate(i32 %24, i32 4096)
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @PROT_WRITE
%28 = or i32 %27, %26
%29 = load i32, i32* @MAP_SHARED
%30 = tail call i8* @mmap(i32 0, i32 4096, i32 %28, i32 %29, i32 %24, i32 0)
%31 = load i8*, i8** @MAP_FAILED
%32 = icmp eq i8* %30, %31
br i1 %32, label %33, label %35
33: ; preds = %20
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.8, i64 0, i64 0))
br label %68
35: ; preds = %20
%36 = tail call i8* @llvm.stacksave()
%37 = alloca [4096 x i32]
%38 = alloca [4096 x i8]
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %38, i64 0, i64 0
%40 = getelementptr inbounds [4096 x i32], [4096 x i32]* %37, i64 0, i64 0
store i32 %14, i32* %40
%41 = icmp eq i32 %14, 1
br i1 %41, label %44, label %45
42: ; preds = %45
%43 = icmp sgt i32 %14, 0
br i1 %43, label %44, label %65
44: ; preds = %35, %42
br label %52
45: ; preds = %35, %45
%46 = phi i64 [ %50, %45 ], [ 1, %35 ]
%47 = phi i32 [ %48, %45 ], [ %14, %35 ]
%48 = tail call i32 @collatz(i32 %47)
%49 = getelementptr inbounds [4096 x i32], [4096 x i32]* %37, i64 0, i64 %46
store i32 %48, i32* %49
%50 = add nuw i64 %46, 1
%51 = icmp eq i32 %48, 1
br i1 %51, label %42, label %45
52: ; preds = %44, %52
%53 = phi i64 [ %61, %52 ], [ 0, %44 ]
%54 = phi i32 [ %63, %52 ], [ %14, %44 ]
%55 = phi i64 [ %60, %52 ], [ 0, %44 ]
%56 = shl i64 %55, 32
%57 = ashr exact i64 %56, 32
%58 = getelementptr inbounds [4096 x i8], [4096 x i8]* %38, i64 0, i64 %57
%59 = call i64 (i8*, i8*, ...) @sprintf(i8* %58, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %54)
%60 = add nsw i64 %59, %57
%61 = add nuw i64 %53, 1
%62 = getelementptr inbounds [4096 x i32], [4096 x i32]* %37, i64 0, i64 %61
%63 = load i32, i32* %62
%64 = icmp sgt i32 %63, 0
br i1 %64, label %52, label %65
65: ; preds = %52, %42
%66 = call i64 (i8*, i8*, ...) @sprintf(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %39)
%67 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @str, i64 0, i64 0))
br label %68
68: ; preds = %65, %33
%69 = phi i32 [ -1, %33 ], [ 0, %65 ]
ret i32 %69
}