#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*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, long *, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, long *, int);
int main() {
  printf("***Running parasum***\n");
  long int n = 12345678900;
  long int sum;
  long int i;
  int pipes[2];
  if (pipe(pipes) != 0) {
    perror("pipe()");
    exit(1);
  }
  int frk = fork();
  if (frk < 0) {
    perror("fork()");
    exit(1);
  }
  switch (frk) {
  case 0:
    close(pipes[0]);
    for (i = (n / 2) + 1; i < n; i++) {
      sum += 1;
    }
    write(pipes[1], &sum, 8);
    exit(3);
    break;
  default:
    close(pipes[1]);
    for (i = 0; i <= n / 2; i++) {
      sum += 1;
    }
    wait(NULL);
    long int tmp;
    read(pipes[0], &tmp, 8);
    sum += tmp;
    printf("Total sum = %ld\n", sum);
    break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movabs $0x2dfdc1c34,%rax
mov    %rax,-0x10(%rbp)
lea    -0x28(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
je     11e4 <main+0x54>
lea    0xe42(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    120e <main+0x7e>
lea    0xe1f(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
jne    1282 <main+0xf2>
jmp    121e <main+0x8e>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    1267 <main+0xd7>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    123c <main+0xac>
mov    -0x24(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
jg     12d5 <main+0x145>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1292 <main+0x102>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
mov    -0x28(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x8,%edx
call   1070 <read@plt>
mov    -0x38(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xd1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
jne    1204 <main+0x64>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     120d <main+0x6d>
je     1223 <main+0x83>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10b0 <wait@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x8,%edx
call   1080 <read@plt>
lea    0xe18(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret
lea    0xdf5(%rip),%rdi
jmp    1214 <main+0x74>
lea    0xdf3(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x8,%edx
call   1040 <write@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"***Running parasum***\0A\00"
@.str.1 = [7 x i8] c"pipe()\00"
@.str.2 = [7 x i8] c"fork()\00"
@.str.3 = [17 x i8] c"Total sum = %ld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca [2 x i32]
%6 = alloca i32
%7 = alloca i64
store i32 0, i32* %1
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i64 12345678900, i64* %2
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%10 = call i64 @pipe(i32* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = call i32 (...) @fork()
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %15
%23 = load i32, i32* %6
switch i32 %23, label %46 [
i32 0, label %24
]
24: ; preds = %22
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = load i64, i64* %2
%29 = sdiv i64 %28, 2
%30 = add nsw i64 %29, 1
store i64 %30, i64* %4
br label %31
31: ; preds = %38, %24
%32 = load i64, i64* %4
%33 = load i64, i64* %2
%34 = icmp slt i64 %32, %33
br i1 %34, label %35, label %41
35: ; preds = %31
%36 = load i64, i64* %3
%37 = add nsw i64 %36, 1
store i64 %37, i64* %3
br label %38
38: ; preds = %35
%39 = load i64, i64* %4
%40 = add nsw i64 %39, 1
store i64 %40, i64* %4
br label %31
41: ; preds = %31
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%43 = load i32, i32* %42
%44 = call i32 @write(i32 %43, i64* %3, i32 8)
%45 = call i32 @exit(i32 3)
unreachable
46: ; preds = %22
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%48 = load i32, i32* %47
%49 = call i32 @close(i32 %48)
store i64 0, i64* %4
br label %50
50: ; preds = %58, %46
%51 = load i64, i64* %4
%52 = load i64, i64* %2
%53 = sdiv i64 %52, 2
%54 = icmp sle i64 %51, %53
br i1 %54, label %55, label %61
55: ; preds = %50
%56 = load i64, i64* %3
%57 = add nsw i64 %56, 1
store i64 %57, i64* %3
br label %58
58: ; preds = %55
%59 = load i64, i64* %4
%60 = add nsw i64 %59, 1
store i64 %60, i64* %4
br label %50
61: ; preds = %50
%62 = call i32 @wait(i32* null)
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%64 = load i32, i32* %63
%65 = call i32 @read(i32 %64, i64* %7, i32 8)
%66 = load i64, i64* %7
%67 = load i64, i64* %3
%68 = add nsw i64 %67, %66
store i64 %68, i64* %3
%69 = load i64, i64* %3
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i64 %69)
br label %71
71: ; preds = %61
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [7 x i8] c"pipe()\00"
@.str.2 = [7 x i8] c"fork()\00"
@.str.3 = [17 x i8] c"Total sum = %ld\0A\00"
@str = [22 x i8] c"***Running parasum***\00"
define i32 @main() {
%1 = alloca i64
%2 = alloca [2 x i32]
%3 = alloca i64
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%5 = bitcast i64* %1 to i8*
%6 = bitcast [2 x i32]* %2 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = call i32 (...) @fork()
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = icmp eq i32 %14, 0
br i1 %20, label %21, label %28
21: ; preds = %19
%22 = load i32, i32* %7
%23 = call i32 @close(i32 %22)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @write(i32 %25, i64* %1, i32 8)
%27 = call i32 @exit(i32 3)
unreachable
28: ; preds = %19
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = call i32 @wait(i32* null)
%33 = bitcast i64* %3 to i8*
%34 = load i32, i32* %7
%35 = call i32 @read(i32 %34, i64* %3, i32 8)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i64 undef)
ret i32 0
}