#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 */
int IPC_CREAT;
int /*<<< orphan*/ IPC_PRIVATE;
int atoi(char *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ logPID();
int /*<<< orphan*/ printf(char *, ...);
int *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ shmdt(int *);
int shmget(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int pid, *contagem;
  int seg_id;
  if (argc <= 1) {
    fprintf(stderr, "Numero invalido de argumentos. (%d)\n", argc);
    exit(-1);
  }
  seg_id = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666);
  printf("Argumentos: ");
  for (int i = 0; i < argc; i++) {
    printf("%s ", argv[i]);
    printf("%c", (i == argc - 1) ? '\n' : ',');
  }
  contagem = shmat(seg_id, NULL, 0);
  *contagem = atoi(argv[1]);
  printf("Numero de processos: %d\n", *contagem);
  while (*contagem > 1) {
    if (!pid) {
      logPID();
      *contagem -= 1;
      pid = fork();
    } else {
      wait(NULL);
    }
  }
  while (1) {
  }
  if (contagem != 0) {
    shmdt(contagem);
    printf("Liberando memoria compartilhda... \n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     11d2 <main+0x42>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe3d(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x4,%esi
call   1070 <shmget@plt>
mov    %eax,-0x24(%rbp)
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1266 <main+0xd6>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%ecx
mov    -0x8(%rbp),%edx
sub    $0x1,%edx
mov    $0x2c,%esi
mov    $0xa,%eax
cmp    %edx,%ecx
cmove  %eax,%esi
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    120c <main+0x7c>
mov    -0x24(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xd9f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
cmpl   $0x1,(%rax)
jle    12e7 <main+0x157>
cmpl   $0x0,-0x14(%rbp)
jne    12d9 <main+0x149>
mov    $0x0,%al
call   1040 <logPID@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
sub    $0x1,%ecx
mov    %ecx,(%rax)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x14(%rbp)
jmp    12e2 <main+0x152>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
jmp    12a1 <main+0x111>
jmp    12ec <main+0x15c>
jmp    12ec <main+0x15c>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r15d
cmp    $0x1,%edi
jle    12a2 <main+0x102>
mov    %rsi,%r12
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x4,%esi
call   1080 <shmget@plt>
mov    %eax,0x4(%rsp)
lea    0xe3f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    -0x1(%r15),%ebp
mov    %r15d,%ebx
lea    0xe37(%rip),%r15
mov    $0xa,%r13d
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r12,%r14,8),%rsi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    %r14,%rbp
mov    $0x2c,%edi
cmove  %r13d,%edi
call   1030 <putchar@plt>
add    $0x1,%r14
cmp    %r14,%rbx
jne    1210 <main+0x70>
mov    0x4(%rsp),%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,%r14
mov    0x8(%r12),%rdi
call   1090 <atoi@plt>
mov    %eax,(%r14)
lea    0xdda(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmpl   $0x2,(%r14)
jl     12a0 <main+0x100>
jmp    127d <main+0xdd>
nopl   (%rax)
xor    %edi,%edi
call   10b0 <wait@plt>
cmpl   $0x1,(%r14)
jle    12a0 <main+0x100>
test   %ebx,%ebx
jne    1270 <main+0xd0>
xor    %eax,%eax
call   1050 <logPID@plt>
addl   $0xffffffff,(%r14)
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ebx
cmpl   $0x1,(%r14)
jg     127d <main+0xdd>
nopl   0x0(%rax,%rax,1)
jmp    12a0 <main+0x100>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    0xd4e(%rip),%rsi
mov    %r15d,%edx
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"Numero invalido de argumentos. (%d)\0A\00"
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@.str.1 = [13 x i8] c"Argumentos: \00"
@.str.2 = [4 x i8] c"%s \00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [25 x i8] c"Numero de processos: %d\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp sle i32 %10, 1
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i32, i32* %4
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = load i32, i32* @IPC_PRIVATE
%19 = load i32, i32* @IPC_CREAT
%20 = or i32 %19, 438
%21 = call i32 @shmget(i32 %18, i32 4, i32 %20)
store i32 %21, i32* %8
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %9
br label %23
23: ; preds = %41, %17
%24 = load i32, i32* %9
%25 = load i32, i32* %4
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %44
27: ; preds = %23
%28 = load i8**, i8*** %5
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8*, i8** %28, i64 %30
%32 = load i8*, i8** %31
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = load i32, i32* %9
%35 = load i32, i32* %4
%36 = sub nsw i32 %35, 1
%37 = icmp eq i32 %34, %36
%38 = zext i1 %37 to i64
%39 = select i1 %37, i32 10, i32 44
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %27
%42 = load i32, i32* %9
%43 = add nsw i32 %42, 1
store i32 %43, i32* %9
br label %23
44: ; preds = %23
%45 = load i32, i32* %8
%46 = call i32* @shmat(i32 %45, i32* null, i32 0)
store i32* %46, i32** %7
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = load i8*, i8** %48
%50 = call i32 @atoi(i8* %49)
%51 = load i32*, i32** %7
store i32 %50, i32* %51
%52 = load i32*, i32** %7
%53 = load i32, i32* %52
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %53)
br label %55
55: ; preds = %70, %44
%56 = load i32*, i32** %7
%57 = load i32, i32* %56
%58 = icmp sgt i32 %57, 1
br i1 %58, label %59, label %71
59: ; preds = %55
%60 = load i32, i32* %6
%61 = icmp ne i32 %60, 0
br i1 %61, label %68, label %62
62: ; preds = %59
%63 = call i32 (...) @logPID()
%64 = load i32*, i32** %7
%65 = load i32, i32* %64
%66 = sub nsw i32 %65, 1
store i32 %66, i32* %64
%67 = call i32 (...) @fork()
store i32 %67, i32* %6
br label %70
68: ; preds = %59
%69 = call i32 @wait(i32* null)
br label %70
70: ; preds = %68, %62
br label %55
71: ; preds = %55
br label %72
72: ; preds = %71, %72
br label %72
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"Numero invalido de argumentos. (%d)\0A\00"
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@.str.1 = [13 x i8] c"Argumentos: \00"
@.str.2 = [4 x i8] c"%s \00"
@.str.4 = [25 x i8] c"Numero de processos: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = tail call i32 @exit(i32 -1)
unreachable
8: ; preds = %2
%9 = load i32, i32* @IPC_PRIVATE
%10 = load i32, i32* @IPC_CREAT
%11 = or i32 %10, 438
%12 = tail call i32 @shmget(i32 %9, i32 4, i32 %11)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%14 = add nsw i32 %0, -1
%15 = zext i32 %14 to i64
%16 = zext i32 %0 to i64
br label %25
17: ; preds = %25
%18 = tail call i32* @shmat(i32 %12, i32* null, i32 0)
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = tail call i32 @atoi(i8* %20)
store i32 %21, i32* %18
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %18
%24 = icmp sgt i32 %23, 1
br i1 %24, label %35, label %49
25: ; preds = %8, %25
%26 = phi i64 [ 0, %8 ], [ %33, %25 ]
%27 = getelementptr inbounds i8*, i8** %1, i64 %26
%28 = load i8*, i8** %27
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = icmp eq i64 %26, %15
%31 = select i1 %30, i32 10, i32 44
%32 = tail call i32 @putchar(i32 %31)
%33 = add nuw nsw i64 %26, 1
%34 = icmp eq i64 %33, %16
br i1 %34, label %17, label %25
35: ; preds = %17, %45
%36 = phi i32 [ %46, %45 ], [ undef, %17 ]
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %35
%39 = tail call i32 (...) @logPID()
%40 = load i32, i32* %18
%41 = add nsw i32 %40, -1
store i32 %41, i32* %18
%42 = tail call i32 (...) @fork()
br label %45
43: ; preds = %35
%44 = tail call i32 @wait(i32* null)
br label %45
45: ; preds = %43, %38
%46 = phi i32 [ %36, %43 ], [ %42, %38 ]
%47 = load i32, i32* %18
%48 = icmp sgt i32 %47, 1
br i1 %48, label %35, label %49
49: ; preds = %45, %17
br label %50
50: ; preds = %49, %50
br label %50
}