#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*/ fflush(int /*<<< orphan*/);
int fork();
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ readAndPrint();
int /*<<< orphan*/ replace(int, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stdout;
int substitute(int, int);
int /*<<< orphan*/ syserr(char *);
int wait(int /*<<< orphan*/);
void printOrBranch(int d, int readFrom) {
  char message[100];
  sprintf(message, "Happy holidays from %d", d);
  if (d > 0) {
    int pipe_dsc[2];
    if (pipe(pipe_dsc) == -1) {
      syserr("Error in pipe");
    }
    int pid;
    switch (pid = fork()) {
    case -1:
      syserr("Error in fork\n");
      break;
    case 0:
      printOrBranch(d - 1, pipe_dsc[0]);
      break;
    default: {
      int out = substitute(pipe_dsc[1], 1);
      if (readFrom != -1) {
        int in = substitute(readFrom, 0);
        readAndPrint();
        replace(in, 0);
      }
      printf("%s\n", message);
      printf("\n");
      fflush(stdout);
      replace(out, 1);
      if (wait(0) == -1) {
        syserr("Error in wait");
      }
    }
    }
  } else {
    if (readFrom != -1) {
      int in = substitute(readFrom, 0);
      readAndPrint();
      replace(in, 0);
    }
    puts(message);
  }
}


x86 assembly (clang, -O0):
<printOrBranch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe21(%rip),%rsi
call   10c0 <sprintf@plt>
cmpl   $0x0,-0x4(%rbp)
jle    1302 <printOrBranch+0x142>
lea    -0x78(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    120c <printOrBranch+0x4c>
lea    0xe10(%rip),%rdi
call   1060 <syserr@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %eax,-0x7c(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x8c(%rbp)
sub    $0xffffffff,%eax
je     123f <printOrBranch+0x7f>
jmp    122c <printOrBranch+0x6c>
mov    -0x8c(%rbp),%eax
test   %eax,%eax
je     1250 <printOrBranch+0x90>
jmp    1263 <printOrBranch+0xa3>
lea    0xddf(%rip),%rdi
call   1060 <syserr@plt>
jmp    12fd <printOrBranch+0x13d>
mov    -0x4(%rbp),%edi
sub    $0x1,%edi
mov    -0x78(%rbp),%esi
call   1080 <printOrBranch@plt>
jmp    12fd <printOrBranch+0x13d>
mov    -0x74(%rbp),%edi
mov    $0x1,%esi
call   10b0 <substitute@plt>
mov    %eax,-0x80(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     12a1 <printOrBranch+0xe1>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   10b0 <substitute@plt>
mov    %eax,-0x84(%rbp)
mov    $0x0,%al
call   1040 <readAndPrint@plt>
mov    -0x84(%rbp),%edi
xor    %esi,%esi
call   10a0 <replace@plt>
lea    -0x70(%rbp),%rsi
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    -0x80(%rbp),%edi
mov    $0x1,%esi
call   10a0 <replace@plt>
xor    %edi,%edi
call   10d0 <wait@plt>
cmp    $0xffffffff,%eax
jne    12f8 <printOrBranch+0x138>
lea    0xd45(%rip),%rdi
call   1060 <syserr@plt>
jmp    12fd <printOrBranch+0x13d>
jmp    1339 <printOrBranch+0x179>
cmpl   $0xffffffff,-0x8(%rbp)
je     1330 <printOrBranch+0x170>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   10b0 <substitute@plt>
mov    %eax,-0x88(%rbp)
mov    $0x0,%al
call   1040 <readAndPrint@plt>
mov    -0x88(%rbp),%edi
xor    %esi,%esi
call   10a0 <replace@plt>
lea    -0x70(%rbp),%rdi
call   1030 <puts@plt>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printOrBranch>:
push   %rbp
push   %rbx
sub    $0x78,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
lea    0xe2f(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %ebp,%edx
call   10c0 <sprintf@plt>
test   %ebp,%ebp
jle    1218 <printOrBranch+0x58>
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11fc <printOrBranch+0x3c>
lea    0xe20(%rip),%rdi
call   1060 <syserr@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     1247 <printOrBranch+0x87>
cmp    $0xffffffff,%eax
jne    1257 <printOrBranch+0x97>
lea    0xe12(%rip),%rdi
jmp    12c8 <printOrBranch+0x108>
cmp    $0xffffffff,%ebx
je     1238 <printOrBranch+0x78>
mov    %ebx,%edi
xor    %esi,%esi
call   10b0 <substitute@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <readAndPrint@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   10a0 <replace@plt>
lea    0x10(%rsp),%rdi
call   1040 <puts@plt>
jmp    12cd <printOrBranch+0x10d>
add    $0xffffffff,%ebp
mov    0x8(%rsp),%esi
mov    %ebp,%edi
call   1080 <printOrBranch@plt>
jmp    12cd <printOrBranch+0x10d>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   10b0 <substitute@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%ebx
je     1287 <printOrBranch+0xc7>
mov    %ebx,%edi
xor    %esi,%esi
call   10b0 <substitute@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <readAndPrint@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   10a0 <replace@plt>
lea    0x10(%rsp),%rdi
call   1040 <puts@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   10a0 <replace@plt>
xor    %edi,%edi
call   10d0 <wait@plt>
cmp    $0xffffffff,%eax
jne    12cd <printOrBranch+0x10d>
lea    0xd6c(%rip),%rdi
call   1060 <syserr@plt>
add    $0x78,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Happy holidays from %d\00"
@.str.1 = [14 x i8] c"Error in pipe\00"
@.str.2 = [15 x i8] c"Error in fork\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
@stdout = global i32 0
@.str.5 = [14 x i8] c"Error in wait\00"
define void @printOrBranch(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [100 x i8]
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%12 = load i32, i32* %3
%13 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %3
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %57
16: ; preds = %2
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%18 = call i32 @pipe(i32* %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @syserr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = call i32 (...) @fork()
store i32 %23, i32* %7
switch i32 %23, label %31 [
i32 -1, label %24
i32 0, label %26
]
24: ; preds = %22
%25 = call i32 @syserr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %56
26: ; preds = %22
%27 = load i32, i32* %3
%28 = sub nsw i32 %27, 1
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%30 = load i32, i32* %29
br label %56
31: ; preds = %22
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @substitute(i32 %33, i32 1)
store i32 %34, i32* %8
%35 = load i32, i32* %4
%36 = icmp ne i32 %35, -1
br i1 %36, label %37, label %43
37: ; preds = %31
%38 = load i32, i32* %4
%39 = call i32 @substitute(i32 %38, i32 0)
store i32 %39, i32* %9
%40 = call i32 (...) @readAndPrint()
%41 = load i32, i32* %9
%42 = call i32 @replace(i32 %41, i32 0)
br label %43
43: ; preds = %37, %31
%44 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%47 = load i32, i32* @stdout
%48 = call i32 @fflush(i32 %47)
%49 = load i32, i32* %8
%50 = call i32 @replace(i32 %49, i32 1)
%51 = call i32 @wait(i32 0)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %43
%54 = call i32 @syserr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %55
55: ; preds = %53, %43
br label %56
56: ; preds = %55, %26, %24
br label %69
57: ; preds = %2
%58 = load i32, i32* %4
%59 = icmp ne i32 %58, -1
br i1 %59, label %60, label %66
60: ; preds = %57
%61 = load i32, i32* %4
%62 = call i32 @substitute(i32 %61, i32 0)
store i32 %62, i32* %10
%63 = call i32 (...) @readAndPrint()
%64 = load i32, i32* %10
%65 = call i32 @replace(i32 %64, i32 0)
br label %66
66: ; preds = %60, %57
%67 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%68 = call i32 @puts(i8* %67)
br label %69
69: ; preds = %66, %56
ret void
}

LLVM IR (-O2):
@.str = [23 x i8] c"Happy holidays from %d\00"
@.str.1 = [14 x i8] c"Error in pipe\00"
@.str.2 = [15 x i8] c"Error in fork\0A\00"
@stdout = global i32 0
@.str.5 = [14 x i8] c"Error in wait\00"
define void @printOrBranch(i32 %0, i32 %1) {
%3 = alloca [100 x i8]
%4 = alloca [2 x i32]
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%6 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = icmp sgt i32 %0, 0
br i1 %7, label %8, label %42
8: ; preds = %2
%9 = bitcast [2 x i32]* %4 to i8*
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = call i32 @syserr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %8
%16 = call i32 (...) @fork()
switch i32 %16, label %22 [
i32 -1, label %17
i32 0, label %19
]
17: ; preds = %15
%18 = call i32 @syserr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %41
19: ; preds = %15
%20 = add nsw i32 %0, -1
%21 = load i32, i32* %10
br label %41
22: ; preds = %15
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @substitute(i32 %24, i32 1)
%26 = icmp eq i32 %1, -1
br i1 %26, label %31, label %27
27: ; preds = %22
%28 = call i32 @substitute(i32 %1, i32 0)
%29 = call i32 (...) @readAndPrint()
%30 = call i32 @replace(i32 %28, i32 0)
br label %31
31: ; preds = %27, %22
%32 = call i32 @puts(i8* %5)
%33 = call i32 @putchar(i32 10)
%34 = load i32, i32* @stdout
%35 = call i32 @fflush(i32 %34)
%36 = call i32 @replace(i32 %25, i32 1)
%37 = call i32 @wait(i32 0)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %31
%40 = call i32 @syserr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %41
41: ; preds = %31, %39, %19, %17
br label %50
42: ; preds = %2
%43 = icmp eq i32 %1, -1
br i1 %43, label %48, label %44
44: ; preds = %42
%45 = tail call i32 @substitute(i32 %1, i32 0)
%46 = tail call i32 (...) @readAndPrint()
%47 = tail call i32 @replace(i32 %45, i32 0)
br label %48
48: ; preds = %44, %42
%49 = call i32 @puts(i8* %5)
br label %50
50: ; preds = %48, %41
ret void
}