#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int consume_cowsay(int *, scalar_t__, char **);
int /*<<< orphan*/ exec_cowsay(int *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
bool cowsay(char **argv) {
  int pipefds[2];
  if (pipe(pipefds) < 0) {
    perror("pipe");
    return false;
  }
  pid_t pid = fork();
  if (pid < 0) {
    perror("fork");
    return false;
  } else if (pid == 0) {
    exec_cowsay(pipefds);
    exit(EXIT_FAILURE);
  } else {
    return consume_cowsay(pipefds, pid, argv);
  }
}


x86 assembly (clang, -O0):
<cowsay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1030 <pipe@plt>
cmp    $0x0,%rax
jge    1197 <cowsay+0x37>
lea    0xe7a(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11fb <cowsay+0x9b>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11c5 <cowsay+0x65>
lea    0xe51(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11fb <cowsay+0x9b>
cmpq   $0x0,-0x20(%rbp)
jne    11e7 <cowsay+0x87>
lea    -0x18(%rbp),%rdi
call   1040 <exec_cowsay@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
lea    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1070 <consume_cowsay@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cowsay>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsp,%rdi
call   1030 <pipe@plt>
test   %rax,%rax
js     119c <cowsay+0x3c>
xor    %ebp,%ebp
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11ac <cowsay+0x4c>
mov    %rsp,%rdi
je     11ba <cowsay+0x5a>
mov    %rax,%rsi
mov    %rbx,%rdx
call   1070 <consume_cowsay@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe5d(%rip),%rdi
call   1050 <perror@plt>
xor    %ebp,%ebp
jmp    1193 <cowsay+0x33>
lea    0xe52(%rip),%rdi
call   1050 <perror@plt>
jmp    1193 <cowsay+0x33>
call   1040 <exec_cowsay@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
define i32 @cowsay(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca [2 x i32]
%5 = alloca i64
store i8** %0, i8*** %3
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%7 = call i64 @pipe(i32* %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %30
11: ; preds = %1
%12 = call i64 (...) @fork()
store i64 %12, i64* %5
%13 = load i64, i64* %5
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %30
17: ; preds = %11
%18 = load i64, i64* %5
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%22 = call i32 @exec_cowsay(i32* %21)
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %17
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%27 = load i64, i64* %5
%28 = load i8**, i8*** %3
%29 = call i32 @consume_cowsay(i32* %26, i64 %27, i8** %28)
store i32 %29, i32* %2
br label %30
30: ; preds = %25, %15, %9
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
define i32 @cowsay(i8** %0) {
%2 = alloca [2 x i32]
%3 = bitcast [2 x i32]* %2 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%5 = call i64 @pipe(i32* %4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %22
9: ; preds = %1
%10 = call i64 (...) @fork()
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %22
14: ; preds = %9
%15 = icmp eq i64 %10, 0
br i1 %15, label %16, label %20
16: ; preds = %14
%17 = call i32 @exec_cowsay(i32* %4)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %14
%21 = call i32 @consume_cowsay(i32* %4, i64 %10, i8** %0)
br label %22
22: ; preds = %12, %20, %7
%23 = phi i32 [ 0, %7 ], [ 0, %12 ], [ %21, %20 ]
ret i32 %23
}