#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 /*<<< orphan*/ IPC_PRIVATE;
int fork();
int /*<<< orphan*/ getchar();
int msgget(int /*<<< orphan*/, int);
int msgrcv(int, struct msgbuf *, int, int, int /*<<< orphan*/);
int msgsnd(int, struct msgbuf *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  int msgid = msgget(IPC_PRIVATE, 0660);
  struct msgbuf {
    long mtype;
    char mtext[1];
  };
  switch (fork()) {
  case 0: {
    int sum1 = 0;
    int number1_from_pipe = 1;
    int finished = 0;
    while (!finished) {
      struct msgbuf msg_son1;
      msg_son1.mtype = 1;
      msg_son1.mtext[1] = number1_from_pipe;
      if (msgrcv(msgid, &msg_son1, (sizeof(struct msgbuf) - sizeof(long)), 1,
                 0) == -1) {
        perror("Failed to receive message son 1");
      }
      printf("%d\n", msg_son1.mtext[1]);
      sum1 = sum1 + msg_son1.mtext[1];
      if (msg_son1.mtext[1] == 0) {
        struct msgbuf msg_sum1;
        msg_sum1.mtype = 1;
        msg_sum1.mtext[1] = sum1;
        if (msgsnd(msgid, &msg_sum1, (sizeof(struct msgbuf) - sizeof(long)),
                   0) == -1) {
          perror("Failed to send sum 1");
        }
        finished = 1;
      }
    }
    break;
  }
  case -1: {
    printf("ERROR WITH FORK");
    return -1;
    break;
  }
  default: {
    switch (fork()) {
    case 0: {
      int sum2 = 0;
      int number2_from_pipe = 1;
      int finished = 0;
      while (!finished) {
        struct msgbuf msg_son2;
        msg_son2.mtype = 2;
        msg_son2.mtext[1] = number2_from_pipe;
        if (msgrcv(msgid, &msg_son2, (sizeof(struct msgbuf) - sizeof(long)), 2,
                   0) == -1) {
          perror("Failed to receive son 2");
        }
        sum2 = sum2 + msg_son2.mtext[1];
        if (msg_son2.mtext[1] == 0) {
          struct msgbuf msg_sum2;
          msg_sum2.mtype = 2;
          msg_sum2.mtext[1] = sum2;
          msgsnd(msgid, &msg_sum2, (sizeof(struct msgbuf) - sizeof(long)), 0);
          finished = 1;
        }
      }
      break;
    }
    case -1: {
      printf("ERROR WITH FORK");
      return -1;
      break;
    }
    default: {
      int temp = 1;
      int negative_entered = 0;
      int positive_entered = 0;
      do {
        printf("\nInput an integer (0 triggers the computation)\n");
        scanf("%d", &temp);
        getchar();
        if (temp > 0) {
          positive_entered = 1;
          struct msgbuf msg_pos;
          msg_pos.mtype = 1;
          msg_pos.mtext[1] = temp;
          msgsnd(msgid, &msg_pos, (sizeof(struct msgbuf) - sizeof(long)), 0);
        }
        if (temp < 0) {
          negative_entered = 1;
          struct msgbuf msg_neg;
          msg_neg.mtype = 2;
          msg_neg.mtext[1] = temp;
          msgsnd(msgid, &msg_neg, (sizeof(struct msgbuf) - sizeof(long)), 0);
        }
        if (temp == 0) {
          printf("FINI\n");
          struct msgbuf msg_zero1;
          msg_zero1.mtype = 1;
          msg_zero1.mtext[1] = temp;
          msgsnd(msgid, &msg_zero1, (sizeof(struct msgbuf) - sizeof(long)), 0);
          struct msgbuf msg_zero2;
          msg_zero2.mtype = 2;
          msg_zero2.mtext[1] = temp;
          msgsnd(msgid, &msg_zero2, (sizeof(struct msgbuf) - sizeof(long)), 0);
        }
      } while (temp != 0);
      if (!positive_entered) {
      }
      if (!negative_entered) {
      }
      int res1;
      int res2;
      struct msgbuf msg_res1;
      msg_res1.mtype = 0;
      msg_res1.mtext[1] = res1;
      msgrcv(msgid, &msg_res1, (sizeof(struct msgbuf) - sizeof(long)), 1, 0);
      struct msgbuf msg_res2;
      msg_res2.mtype = 0;
      msg_res2.mtext[1] = res2;
      msgrcv(msgid, &msg_res2, (sizeof(struct msgbuf) - sizeof(long)), 2, 0);
      printf("\nSUM 1 = %d\n", msg_res1.mtext[1]);
      printf("SUM 2 = %d\n", msg_res2.mtext[1]);
      wait(NULL);
      wait(NULL);
    }
    }
  }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    $0x1b0,%esi
call   1030 <msgget@plt>
mov    %eax,-0x8(%rbp)
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0xe4(%rbp)
sub    $0xffffffff,%eax
je     12c1 <main+0x131>
jmp    11d5 <main+0x45>
mov    -0xe4(%rbp),%eax
test   %eax,%eax
jne    12db <main+0x14b>
jmp    11e8 <main+0x58>
movl   $0x0,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1213 <main+0x83>
jmp    12bc <main+0x12c>
movq   $0x1,-0x28(%rbp)
mov    -0x10(%rbp),%eax
mov    %al,-0x1f(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
mov    $0x1,%ecx
xor    %r8d,%r8d
call   10a0 <msgrcv@plt>
cmp    $0xffffffff,%eax
jne    124f <main+0xbf>
lea    0xdb6(%rip),%rdi
call   1080 <perror@plt>
movsbl -0x1f(%rbp),%esi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
movsbl -0x1f(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0xc(%rbp)
movsbl -0x1f(%rbp),%eax
cmp    $0x0,%eax
jne    12b7 <main+0x127>
movq   $0x1,-0x38(%rbp)
mov    -0xc(%rbp),%eax
mov    %al,-0x2f(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1060 <msgsnd@plt>
cmp    $0xffffffff,%eax
jne    12b0 <main+0x120>
lea    0xd75(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x14(%rbp)
jmp    11fd <main+0x6d>
jmp    15c4 <main+0x434>
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15c4 <main+0x434>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0xe8(%rbp)
sub    $0xffffffff,%eax
je     13bd <main+0x22d>
jmp    12f8 <main+0x168>
mov    -0xe8(%rbp),%eax
test   %eax,%eax
jne    13d7 <main+0x247>
jmp    130b <main+0x17b>
movl   $0x0,-0x3c(%rbp)
movl   $0x1,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1336 <main+0x1a6>
jmp    13b8 <main+0x228>
movq   $0x2,-0x58(%rbp)
mov    -0x40(%rbp),%eax
mov    %al,-0x4f(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x58(%rbp),%rsi
mov    $0x8,%edx
mov    $0x2,%ecx
xor    %r8d,%r8d
call   10a0 <msgrcv@plt>
cmp    $0xffffffff,%eax
jne    1372 <main+0x1e2>
lea    0xcd8(%rip),%rdi
call   1080 <perror@plt>
mov    -0x3c(%rbp),%eax
movsbl -0x4f(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x3c(%rbp)
movsbl -0x4f(%rbp),%eax
cmp    $0x0,%eax
jne    13b3 <main+0x223>
movq   $0x2,-0x68(%rbp)
mov    -0x3c(%rbp),%eax
mov    %al,-0x5f(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x68(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1060 <msgsnd@plt>
movl   $0x1,-0x44(%rbp)
jmp    1320 <main+0x190>
jmp    15bf <main+0x42f>
lea    0xc71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15c4 <main+0x434>
movl   $0x1,-0x6c(%rbp)
movl   $0x0,-0x70(%rbp)
movl   $0x0,-0x74(%rbp)
lea    0xc6a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xc8c(%rip),%rdi
lea    -0x6c(%rbp),%rsi
call   1070 <scanf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
cmpl   $0x0,-0x6c(%rbp)
jle    1449 <main+0x2b9>
movl   $0x1,-0x74(%rbp)
movq   $0x1,-0x88(%rbp)
mov    -0x6c(%rbp),%eax
mov    %al,-0x7f(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x88(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1060 <msgsnd@plt>
cmpl   $0x0,-0x6c(%rbp)
jge    1484 <main+0x2f4>
movl   $0x1,-0x70(%rbp)
movq   $0x2,-0x98(%rbp)
mov    -0x6c(%rbp),%eax
mov    %al,-0x8f(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x98(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1060 <msgsnd@plt>
cmpl   $0x0,-0x6c(%rbp)
jne    14f0 <main+0x360>
lea    0xbfb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x1,-0xa8(%rbp)
mov    -0x6c(%rbp),%eax
mov    %al,-0x9f(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xa8(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1060 <msgsnd@plt>
movq   $0x2,-0xb8(%rbp)
mov    -0x6c(%rbp),%eax
mov    %al,-0xaf(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xb8(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1060 <msgsnd@plt>
jmp    14f5 <main+0x365>
cmpl   $0x0,-0x6c(%rbp)
jne    13ec <main+0x25c>
cmpl   $0x0,-0x74(%rbp)
jne    150e <main+0x37e>
jmp    150e <main+0x37e>
cmpl   $0x0,-0x70(%rbp)
jne    151d <main+0x38d>
jmp    151d <main+0x38d>
movq   $0x0,-0xd0(%rbp)
mov    -0xbc(%rbp),%eax
mov    %al,-0xc7(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xd0(%rbp),%rsi
mov    $0x8,%edx
mov    $0x1,%ecx
xor    %r8d,%r8d
call   10a0 <msgrcv@plt>
movq   $0x0,-0xe0(%rbp)
mov    -0xc0(%rbp),%eax
mov    %al,-0xd7(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xe0(%rbp),%rsi
mov    $0x8,%edx
mov    $0x2,%ecx
xor    %r8d,%r8d
call   10a0 <msgrcv@plt>
movsbl -0xc7(%rbp),%esi
lea    0xb05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movsbl -0xd7(%rbp),%esi
lea    0xafd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
jmp    15c4 <main+0x434>
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    $0x1b0,%esi
call   1030 <msgget@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
je     12b4 <main+0x114>
test   %eax,%eax
jne    1243 <main+0xa3>
lea    0x8(%rsp),%r15
lea    0xe1c(%rip),%r12
lea    0xe7e(%rip),%rbp
xor    %ebx,%ebx
jmp    120e <main+0x6e>
nop
movsbl 0x11(%rsp),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x11(%rsp),%eax
add    %eax,%ebx
test   %eax,%eax
je     12cc <main+0x12c>
movq   $0x1,0x8(%rsp)
movb   $0x1,0x11(%rsp)
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x8,%edx
mov    $0x1,%ecx
xor    %r8d,%r8d
call   10b0 <msgrcv@plt>
cmp    $0xffffffff,%eax
jne    11f0 <main+0x50>
mov    %r12,%rdi
call   1090 <perror@plt>
jmp    11f0 <main+0x50>
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
je     12b4 <main+0x114>
test   %eax,%eax
jne    1309 <main+0x169>
xor    %ebx,%ebx
lea    0x8(%rsp),%rbp
lea    0xde0(%rip),%r15
jmp    127f <main+0xdf>
nopw   0x0(%rax,%rax,1)
movzbl 0x11(%rsp),%eax
add    %eax,%ebx
test   %eax,%eax
je     1418 <main+0x278>
movq   $0x2,0x8(%rsp)
movb   $0x1,0x11(%rsp)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
mov    $0x2,%ecx
xor    %r8d,%r8d
call   10b0 <msgrcv@plt>
cmp    $0xffffffff,%eax
jne    1270 <main+0xd0>
mov    %r15,%rdi
call   1090 <perror@plt>
jmp    1270 <main+0xd0>
lea    0xd7a(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebp
jmp    14b9 <main+0x319>
movq   $0x1,0x18(%rsp)
mov    %bl,0x21(%rsp)
lea    0x18(%rsp),%rsi
xor    %ebp,%ebp
mov    %r14d,%edi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1070 <msgsnd@plt>
cmp    $0xffffffff,%eax
jne    14b9 <main+0x319>
lea    0xd21(%rip),%rdi
call   1090 <perror@plt>
jmp    14b9 <main+0x319>
movl   $0x1,0x4(%rsp)
lea    0xd61(%rip),%rbx
lea    0xd3e(%rip),%r15
lea    0x4(%rsp),%r12
lea    0x8(%rsp),%r13
lea    0x18(%rsp),%rbp
jmp    1338 <main+0x198>
test   %eax,%eax
je     143d <main+0x29d>
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1080 <scanf@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
mov    0x4(%rsp),%eax
test   %eax,%eax
jle    137d <main+0x1dd>
movq   $0x1,0x8(%rsp)
mov    %al,0x11(%rsp)
mov    %r14d,%edi
mov    %r13,%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1070 <msgsnd@plt>
mov    0x4(%rsp),%eax
test   %eax,%eax
js     1390 <main+0x1f0>
test   %eax,%eax
jne    1330 <main+0x190>
jmp    13bb <main+0x21b>
nopw   0x0(%rax,%rax,1)
movq   $0x2,0x8(%rsp)
mov    %al,0x11(%rsp)
mov    %r14d,%edi
mov    %r13,%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1070 <msgsnd@plt>
mov    0x4(%rsp),%eax
test   %eax,%eax
jne    1330 <main+0x190>
lea    0xce6(%rip),%rdi
call   1040 <puts@plt>
movq   $0x1,0x8(%rsp)
movzbl 0x4(%rsp),%eax
mov    %al,0x11(%rsp)
mov    %r14d,%edi
mov    %r13,%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1070 <msgsnd@plt>
movq   $0x2,0x18(%rsp)
movzbl 0x4(%rsp),%eax
mov    %al,0x21(%rsp)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1070 <msgsnd@plt>
mov    0x4(%rsp),%eax
jmp    1330 <main+0x190>
movq   $0x2,0x18(%rsp)
mov    %bl,0x21(%rsp)
lea    0x18(%rsp),%rsi
xor    %ebp,%ebp
mov    %r14d,%edi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1070 <msgsnd@plt>
jmp    14b9 <main+0x319>
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
xor    %ebp,%ebp
mov    %r14d,%edi
mov    $0x8,%edx
mov    $0x1,%ecx
xor    %r8d,%r8d
call   10b0 <msgrcv@plt>
movq   $0x0,0x18(%rsp)
lea    0x18(%rsp),%rsi
mov    %r14d,%edi
mov    $0x8,%edx
mov    $0x2,%ecx
xor    %r8d,%r8d
call   10b0 <msgrcv@plt>
movsbl 0x11(%rsp),%esi
lea    0xbcf(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x21(%rsp),%esi
lea    0xbc9(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.msgbuf = type { i64, [1 x i8] }
%struct.msgbuf.0 = type opaque
%struct.msgbuf.1 = type opaque
@IPC_PRIVATE = global i32 0
@.str = [32 x i8] c"Failed to receive message son 1\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [21 x i8] c"Failed to send sum 1\00"
@.str.3 = [16 x i8] c"ERROR WITH FORK\00"
@.str.4 = [24 x i8] c"Failed to receive son 2\00"
@.str.5 = [48 x i8] c"\0AInput an integer (0 triggers the computation)\0A\00"
@.str.6 = [3 x i8] c"%d\00"
@.str.7 = [6 x i8] c"FINI\0A\00"
@.str.8 = [13 x i8] c"\0ASUM 1 = %d\0A\00"
@.str.9 = [12 x i8] c"SUM 2 = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.msgbuf
%7 = alloca %struct.msgbuf
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.msgbuf
%12 = alloca %struct.msgbuf
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.msgbuf
%17 = alloca %struct.msgbuf
%18 = alloca %struct.msgbuf
%19 = alloca %struct.msgbuf
%20 = alloca i32
%21 = alloca i32
%22 = alloca %struct.msgbuf
%23 = alloca %struct.msgbuf
store i32 0, i32* %1
%24 = load i32, i32* @IPC_PRIVATE
%25 = call i32 @msgget(i32 %24, i32 432)
store i32 %25, i32* %2
%26 = call i32 (...) @fork()
switch i32 %26, label %78 [
i32 0, label %27
i32 -1, label %76
]
27: ; preds = %0
store i32 0, i32* %3
store i32 1, i32* %4
store i32 0, i32* %5
br label %28
28: ; preds = %74, %27
%29 = load i32, i32* %5
%30 = icmp ne i32 %29, 0
%31 = xor i1 %30, true
br i1 %31, label %32, label %75
32: ; preds = %28
%33 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %6, i32 0, i32 0
store i64 1, i64* %33
%34 = load i32, i32* %4
%35 = trunc i32 %34 to i8
%36 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %6, i32 0, i32 1
%37 = getelementptr inbounds [1 x i8], [1 x i8]* %36, i64 0, i64 1
store i8 %35, i8* %37
%38 = load i32, i32* %2
%39 = bitcast %struct.msgbuf* %6 to %struct.msgbuf.0*
%40 = call i32 @msgrcv(i32 %38, %struct.msgbuf.0* %39, i32 8, i32 1, i32 0)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %32
%43 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %44
44: ; preds = %42, %32
%45 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %6, i32 0, i32 1
%46 = getelementptr inbounds [1 x i8], [1 x i8]* %45, i64 0, i64 1
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %48)
%50 = load i32, i32* %3
%51 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %6, i32 0, i32 1
%52 = getelementptr inbounds [1 x i8], [1 x i8]* %51, i64 0, i64 1
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = add nsw i32 %50, %54
store i32 %55, i32* %3
%56 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %6, i32 0, i32 1
%57 = getelementptr inbounds [1 x i8], [1 x i8]* %56, i64 0, i64 1
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %74
61: ; preds = %44
%62 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %7, i32 0, i32 0
store i64 1, i64* %62
%63 = load i32, i32* %3
%64 = trunc i32 %63 to i8
%65 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %7, i32 0, i32 1
%66 = getelementptr inbounds [1 x i8], [1 x i8]* %65, i64 0, i64 1
store i8 %64, i8* %66
%67 = load i32, i32* %2
%68 = bitcast %struct.msgbuf* %7 to %struct.msgbuf.1*
%69 = call i32 @msgsnd(i32 %67, %struct.msgbuf.1* %68, i32 8, i32 0)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %73
71: ; preds = %61
%72 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %73
73: ; preds = %71, %61
store i32 1, i32* %5
br label %74
74: ; preds = %73, %44
br label %28
75: ; preds = %28
br label %213
76: ; preds = %0
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %213
78: ; preds = %0
%79 = call i32 (...) @fork()
switch i32 %79, label %122 [
i32 0, label %80
i32 -1, label %120
]
80: ; preds = %78
store i32 0, i32* %8
store i32 1, i32* %9
store i32 0, i32* %10
br label %81
81: ; preds = %118, %80
%82 = load i32, i32* %10
%83 = icmp ne i32 %82, 0
%84 = xor i1 %83, true
br i1 %84, label %85, label %119
85: ; preds = %81
%86 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %11, i32 0, i32 0
store i64 2, i64* %86
%87 = load i32, i32* %9
%88 = trunc i32 %87 to i8
%89 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %11, i32 0, i32 1
%90 = getelementptr inbounds [1 x i8], [1 x i8]* %89, i64 0, i64 1
store i8 %88, i8* %90
%91 = load i32, i32* %2
%92 = bitcast %struct.msgbuf* %11 to %struct.msgbuf.0*
%93 = call i32 @msgrcv(i32 %91, %struct.msgbuf.0* %92, i32 8, i32 2, i32 0)
%94 = icmp eq i32 %93, -1
br i1 %94, label %95, label %97
95: ; preds = %85
%96 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %97
97: ; preds = %95, %85
%98 = load i32, i32* %8
%99 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %11, i32 0, i32 1
%100 = getelementptr inbounds [1 x i8], [1 x i8]* %99, i64 0, i64 1
%101 = load i8, i8* %100
%102 = sext i8 %101 to i32
%103 = add nsw i32 %98, %102
store i32 %103, i32* %8
%104 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %11, i32 0, i32 1
%105 = getelementptr inbounds [1 x i8], [1 x i8]* %104, i64 0, i64 1
%106 = load i8, i8* %105
%107 = sext i8 %106 to i32
%108 = icmp eq i32 %107, 0
br i1 %108, label %109, label %118
109: ; preds = %97
%110 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %12, i32 0, i32 0
store i64 2, i64* %110
%111 = load i32, i32* %8
%112 = trunc i32 %111 to i8
%113 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %12, i32 0, i32 1
%114 = getelementptr inbounds [1 x i8], [1 x i8]* %113, i64 0, i64 1
store i8 %112, i8* %114
%115 = load i32, i32* %2
%116 = bitcast %struct.msgbuf* %12 to %struct.msgbuf.1*
%117 = call i32 @msgsnd(i32 %115, %struct.msgbuf.1* %116, i32 8, i32 0)
store i32 1, i32* %10
br label %118
118: ; preds = %109, %97
br label %81
119: ; preds = %81
br label %212
120: ; preds = %78
%121 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %213
122: ; preds = %78
store i32 1, i32* %13
store i32 0, i32* %14
store i32 0, i32* %15
br label %123
123: ; preds = %172, %122
%124 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0))
%125 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32* %13)
%126 = call i32 (...) @getchar()
%127 = load i32, i32* %13
%128 = icmp sgt i32 %127, 0
br i1 %128, label %129, label %138
129: ; preds = %123
store i32 1, i32* %15
%130 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %16, i32 0, i32 0
store i64 1, i64* %130
%131 = load i32, i32* %13
%132 = trunc i32 %131 to i8
%133 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %16, i32 0, i32 1
%134 = getelementptr inbounds [1 x i8], [1 x i8]* %133, i64 0, i64 1
store i8 %132, i8* %134
%135 = load i32, i32* %2
%136 = bitcast %struct.msgbuf* %16 to %struct.msgbuf.1*
%137 = call i32 @msgsnd(i32 %135, %struct.msgbuf.1* %136, i32 8, i32 0)
br label %138
138: ; preds = %129, %123
%139 = load i32, i32* %13
%140 = icmp slt i32 %139, 0
br i1 %140, label %141, label %150
141: ; preds = %138
store i32 1, i32* %14
%142 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %17, i32 0, i32 0
store i64 2, i64* %142
%143 = load i32, i32* %13
%144 = trunc i32 %143 to i8
%145 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %17, i32 0, i32 1
%146 = getelementptr inbounds [1 x i8], [1 x i8]* %145, i64 0, i64 1
store i8 %144, i8* %146
%147 = load i32, i32* %2
%148 = bitcast %struct.msgbuf* %17 to %struct.msgbuf.1*
%149 = call i32 @msgsnd(i32 %147, %struct.msgbuf.1* %148, i32 8, i32 0)
br label %150
150: ; preds = %141, %138
%151 = load i32, i32* %13
%152 = icmp eq i32 %151, 0
br i1 %152, label %153, label %171
153: ; preds = %150
%154 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%155 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %18, i32 0, i32 0
store i64 1, i64* %155
%156 = load i32, i32* %13
%157 = trunc i32 %156 to i8
%158 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %18, i32 0, i32 1
%159 = getelementptr inbounds [1 x i8], [1 x i8]* %158, i64 0, i64 1
store i8 %157, i8* %159
%160 = load i32, i32* %2
%161 = bitcast %struct.msgbuf* %18 to %struct.msgbuf.1*
%162 = call i32 @msgsnd(i32 %160, %struct.msgbuf.1* %161, i32 8, i32 0)
%163 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %19, i32 0, i32 0
store i64 2, i64* %163
%164 = load i32, i32* %13
%165 = trunc i32 %164 to i8
%166 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %19, i32 0, i32 1
%167 = getelementptr inbounds [1 x i8], [1 x i8]* %166, i64 0, i64 1
store i8 %165, i8* %167
%168 = load i32, i32* %2
%169 = bitcast %struct.msgbuf* %19 to %struct.msgbuf.1*
%170 = call i32 @msgsnd(i32 %168, %struct.msgbuf.1* %169, i32 8, i32 0)
br label %171
171: ; preds = %153, %150
br label %172
172: ; preds = %171
%173 = load i32, i32* %13
%174 = icmp ne i32 %173, 0
br i1 %174, label %123, label %175
175: ; preds = %172
%176 = load i32, i32* %15
%177 = icmp ne i32 %176, 0
br i1 %177, label %179, label %178
178: ; preds = %175
br label %179
179: ; preds = %178, %175
%180 = load i32, i32* %14
%181 = icmp ne i32 %180, 0
br i1 %181, label %183, label %182
182: ; preds = %179
br label %183
183: ; preds = %182, %179
%184 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %22, i32 0, i32 0
store i64 0, i64* %184
%185 = load i32, i32* %20
%186 = trunc i32 %185 to i8
%187 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %22, i32 0, i32 1
%188 = getelementptr inbounds [1 x i8], [1 x i8]* %187, i64 0, i64 1
store i8 %186, i8* %188
%189 = load i32, i32* %2
%190 = bitcast %struct.msgbuf* %22 to %struct.msgbuf.0*
%191 = call i32 @msgrcv(i32 %189, %struct.msgbuf.0* %190, i32 8, i32 1, i32 0)
%192 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %23, i32 0, i32 0
store i64 0, i64* %192
%193 = load i32, i32* %21
%194 = trunc i32 %193 to i8
%195 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %23, i32 0, i32 1
%196 = getelementptr inbounds [1 x i8], [1 x i8]* %195, i64 0, i64 1
store i8 %194, i8* %196
%197 = load i32, i32* %2
%198 = bitcast %struct.msgbuf* %23 to %struct.msgbuf.0*
%199 = call i32 @msgrcv(i32 %197, %struct.msgbuf.0* %198, i32 8, i32 2, i32 0)
%200 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %22, i32 0, i32 1
%201 = getelementptr inbounds [1 x i8], [1 x i8]* %200, i64 0, i64 1
%202 = load i8, i8* %201
%203 = sext i8 %202 to i32
%204 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i32 %203)
%205 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %23, i32 0, i32 1
%206 = getelementptr inbounds [1 x i8], [1 x i8]* %205, i64 0, i64 1
%207 = load i8, i8* %206
%208 = sext i8 %207 to i32
%209 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0), i32 %208)
%210 = call i32 @wait(i32* null)
%211 = call i32 @wait(i32* null)
br label %212
212: ; preds = %183, %119
br label %213
213: ; preds = %76, %120, %212, %75
%214 = load i32, i32* %1
ret i32 %214
}

LLVM IR (-O2):
%struct.msgbuf = type { i64, [1 x i8] }
%struct.msgbuf.0 = type opaque
%struct.msgbuf.1 = type opaque
@IPC_PRIVATE = global i32 0
@.str = [32 x i8] c"Failed to receive message son 1\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [21 x i8] c"Failed to send sum 1\00"
@.str.3 = [16 x i8] c"ERROR WITH FORK\00"
@.str.4 = [24 x i8] c"Failed to receive son 2\00"
@.str.6 = [3 x i8] c"%d\00"
@.str.8 = [13 x i8] c"\0ASUM 1 = %d\0A\00"
@.str.9 = [12 x i8] c"SUM 2 = %d\0A\00"
@str = [47 x i8] c"\0AInput an integer (0 triggers the computation)\00"
@str.10 = [5 x i8] c"FINI\00"
define i32 @main() {
%1 = alloca %struct.msgbuf
%2 = alloca %struct.msgbuf
%3 = alloca %struct.msgbuf
%4 = alloca %struct.msgbuf
%5 = alloca i32
%6 = alloca %struct.msgbuf
%7 = alloca %struct.msgbuf
%8 = alloca %struct.msgbuf
%9 = alloca %struct.msgbuf
%10 = alloca %struct.msgbuf
%11 = alloca %struct.msgbuf
%12 = load i32, i32* @IPC_PRIVATE
%13 = tail call i32 @msgget(i32 %12, i32 432)
%14 = tail call i32 (...) @fork()
switch i32 %14, label %47 [
i32 0, label %15
i32 -1, label %45
]
15: ; preds = %0
%16 = bitcast %struct.msgbuf* %1 to i8*
%17 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %1, i64 0, i32 0
%18 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %1, i64 0, i32 1, i64 1
%19 = bitcast %struct.msgbuf* %1 to %struct.msgbuf.0*
%20 = bitcast %struct.msgbuf* %2 to i8*
%21 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %2, i64 0, i32 0
%22 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %2, i64 0, i32 1, i64 1
%23 = bitcast %struct.msgbuf* %2 to %struct.msgbuf.1*
br label %24
24: ; preds = %44, %15
%25 = phi i32 [ 0, %15 ], [ %36, %44 ]
store i64 1, i64* %17
store i8 1, i8* %18
%26 = call i32 @msgrcv(i32 %13, %struct.msgbuf.0* %19, i32 8, i32 1, i32 0)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %28, %24
%31 = load i8, i8* %18
%32 = sext i8 %31 to i32
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = load i8, i8* %18
%35 = zext i8 %34 to i32
%36 = add i32 %25, %35
%37 = icmp eq i8 %34, 0
br i1 %37, label %38, label %44
38: ; preds = %30
store i64 1, i64* %21
%39 = trunc i32 %36 to i8
store i8 %39, i8* %22
%40 = call i32 @msgsnd(i32 %13, %struct.msgbuf.1* %23, i32 8, i32 0)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %144
42: ; preds = %38
%43 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %144
44: ; preds = %30
br label %24
45: ; preds = %0
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %145
47: ; preds = %0
%48 = tail call i32 (...) @fork()
switch i32 %48, label %72 [
i32 0, label %49
i32 -1, label %70
]
49: ; preds = %47
%50 = bitcast %struct.msgbuf* %3 to i8*
%51 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %3, i64 0, i32 0
%52 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %3, i64 0, i32 1, i64 1
%53 = bitcast %struct.msgbuf* %3 to %struct.msgbuf.0*
%54 = bitcast %struct.msgbuf* %4 to i8*
%55 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %4, i64 0, i32 0
%56 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %4, i64 0, i32 1, i64 1
%57 = bitcast %struct.msgbuf* %4 to %struct.msgbuf.1*
br label %58
58: ; preds = %69, %49
%59 = phi i32 [ 0, %49 ], [ %67, %69 ]
store i64 2, i64* %51
store i8 1, i8* %52
%60 = call i32 @msgrcv(i32 %13, %struct.msgbuf.0* %53, i32 8, i32 2, i32 0)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %64
64: ; preds = %62, %58
%65 = load i8, i8* %52
%66 = zext i8 %65 to i32
%67 = add i32 %59, %66
%68 = icmp eq i8 %65, 0
br i1 %68, label %141, label %69
69: ; preds = %64
br label %58
70: ; preds = %47
%71 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %145
72: ; preds = %47
%73 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%74 = bitcast %struct.msgbuf* %6 to i8*
%75 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %6, i64 0, i32 0
%76 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %6, i64 0, i32 1, i64 1
%77 = bitcast %struct.msgbuf* %6 to %struct.msgbuf.1*
%78 = bitcast %struct.msgbuf* %7 to i8*
%79 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %7, i64 0, i32 0
%80 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %7, i64 0, i32 1, i64 1
%81 = bitcast %struct.msgbuf* %7 to %struct.msgbuf.1*
%82 = bitcast %struct.msgbuf* %8 to i8*
%83 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %8, i64 0, i32 0
%84 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %8, i64 0, i32 1, i64 1
%85 = bitcast %struct.msgbuf* %8 to %struct.msgbuf.1*
%86 = bitcast %struct.msgbuf* %9 to i8*
%87 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %9, i64 0, i32 0
%88 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %9, i64 0, i32 1, i64 1
%89 = bitcast %struct.msgbuf* %9 to %struct.msgbuf.1*
br label %90
90: ; preds = %119, %72
%91 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str, i64 0, i64 0))
%92 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32* %5)
%93 = call i32 (...) @getchar()
%94 = load i32, i32* %5
%95 = icmp sgt i32 %94, 0
br i1 %95, label %96, label %100
96: ; preds = %90
store i64 1, i64* %75
%97 = trunc i32 %94 to i8
store i8 %97, i8* %76
%98 = call i32 @msgsnd(i32 %13, %struct.msgbuf.1* %77, i32 8, i32 0)
%99 = load i32, i32* %5
br label %100
100: ; preds = %96, %90
%101 = phi i32 [ %99, %96 ], [ %94, %90 ]
%102 = icmp slt i32 %101, 0
br i1 %102, label %103, label %107
103: ; preds = %100
store i64 2, i64* %79
%104 = trunc i32 %101 to i8
store i8 %104, i8* %80
%105 = call i32 @msgsnd(i32 %13, %struct.msgbuf.1* %81, i32 8, i32 0)
%106 = load i32, i32* %5
br label %107
107: ; preds = %103, %100
%108 = phi i32 [ %106, %103 ], [ %101, %100 ]
%109 = icmp eq i32 %108, 0
br i1 %109, label %110, label %119
110: ; preds = %107
%111 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.10, i64 0, i64 0))
store i64 1, i64* %83
%112 = load i32, i32* %5
%113 = trunc i32 %112 to i8
store i8 %113, i8* %84
%114 = call i32 @msgsnd(i32 %13, %struct.msgbuf.1* %85, i32 8, i32 0)
store i64 2, i64* %87
%115 = load i32, i32* %5
%116 = trunc i32 %115 to i8
store i8 %116, i8* %88
%117 = call i32 @msgsnd(i32 %13, %struct.msgbuf.1* %89, i32 8, i32 0)
%118 = load i32, i32* %5
br label %119
119: ; preds = %107, %110
%120 = phi i32 [ %108, %107 ], [ %118, %110 ]
%121 = icmp eq i32 %120, 0
br i1 %121, label %122, label %90
122: ; preds = %119
%123 = bitcast %struct.msgbuf* %10 to i8*
%124 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %10, i64 0, i32 0
store i64 0, i64* %124
%125 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %10, i64 0, i32 1, i64 1
%126 = bitcast %struct.msgbuf* %10 to %struct.msgbuf.0*
%127 = call i32 @msgrcv(i32 %13, %struct.msgbuf.0* %126, i32 8, i32 1, i32 0)
%128 = bitcast %struct.msgbuf* %11 to i8*
%129 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %11, i64 0, i32 0
store i64 0, i64* %129
%130 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %11, i64 0, i32 1, i64 1
%131 = bitcast %struct.msgbuf* %11 to %struct.msgbuf.0*
%132 = call i32 @msgrcv(i32 %13, %struct.msgbuf.0* %131, i32 8, i32 2, i32 0)
%133 = load i8, i8* %125
%134 = sext i8 %133 to i32
%135 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i32 %134)
%136 = load i8, i8* %130
%137 = sext i8 %136 to i32
%138 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0), i32 %137)
%139 = call i32 @wait(i32* null)
%140 = call i32 @wait(i32* null)
br label %145
141: ; preds = %64
store i64 2, i64* %55
%142 = trunc i32 %67 to i8
store i8 %142, i8* %56
%143 = call i32 @msgsnd(i32 %13, %struct.msgbuf.1* %57, i32 8, i32 0)
br label %145
144: ; preds = %38, %42
br label %145
145: ; preds = %144, %141, %122, %70, %45
%146 = phi i32 [ -1, %70 ], [ -1, %45 ], [ 0, %122 ], [ 0, %141 ], [ 0, %144 ]
ret i32 %146
}