#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 int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ close(int);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
bool consume_cowsay(int *pipefds, pid_t pid, char **argv) {
  close(pipefds[0]);
  FILE *pp = fdopen(pipefds[1], "w");
  if (!pp) {
    perror("fdopen");
    return false;
  }
  fputs("I think you meant git ", pp);
  fputs(argv[1] + 1, pp);
  for (char **words = argv + 2; *words; words++) {
    fprintf(pp, " %s", *words);
  }
  if (fclose(pp) != 0) {
    perror("Error writing to cowsay");
  }
  int status;
  waitpid(pid, &status, 0);
  if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
    return false;
  }
  sleep(1);
  return true;
}


x86 assembly (clang, -O0):
<consume_cowsay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
lea    0xe34(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f8 <consume_cowsay+0x58>
lea    0xe1b(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12cc <consume_cowsay+0x12c>
mov    -0x28(%rbp),%rsi
lea    0xe06(%rip),%rdi
call   1040 <fputs@plt>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdi
add    $0x1,%rdi
mov    -0x28(%rbp),%rsi
call   1040 <fputs@plt>
mov    -0x20(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
je     125f <consume_cowsay+0xbf>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rdx
lea    0xdd7(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x30(%rbp)
jmp    1229 <consume_cowsay+0x89>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     127e <consume_cowsay+0xde>
lea    0xdab(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x14(%rbp),%edi
lea    -0x34(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    -0x34(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%eax
je     12af <consume_cowsay+0x10f>
mov    -0x34(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
cmp    $0x0,%rax
je     12bb <consume_cowsay+0x11b>
movl   $0x0,-0x4(%rbp)
jmp    12cc <consume_cowsay+0x12c>
mov    $0x1,%edi
call   10c0 <sleep@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<consume_cowsay>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %rdi,%rbp
mov    (%rdi),%edi
call   1050 <close@plt>
mov    0x4(%rbp),%edi
lea    0xe2f(%rip),%rsi
call   1070 <fdopen@plt>
test   %rax,%rax
je     1281 <consume_cowsay+0xd1>
mov    %rax,%rbp
lea    0xe20(%rip),%rdi
mov    $0x16,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10c0 <fwrite@plt>
mov    0x8(%rbx),%rdi
add    $0x1,%rdi
mov    %rbp,%rsi
call   1040 <fputs@plt>
mov    0x10(%rbx),%rdx
test   %rdx,%rdx
je     1237 <consume_cowsay+0x87>
add    $0x18,%rbx
lea    0xe01(%rip),%r15
nop
mov    %rbp,%rdi
mov    %r15,%rsi
call   1060 <fprintf@plt>
mov    (%rbx),%rdx
add    $0x8,%rbx
test   %rdx,%rdx
jne    1220 <consume_cowsay+0x70>
mov    %rbp,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
jne    129c <consume_cowsay+0xec>
lea    0x4(%rsp),%rsi
xor    %ebx,%ebx
mov    %r14d,%edi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    0x4(%rsp),%edi
call   10b0 <WIFEXITED@plt>
test   %eax,%eax
je     128f <consume_cowsay+0xdf>
mov    0x4(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
test   %rax,%rax
jne    128f <consume_cowsay+0xdf>
mov    $0x1,%ebx
mov    $0x1,%edi
call   10d0 <sleep@plt>
jmp    128f <consume_cowsay+0xdf>
lea    0xd7a(%rip),%rdi
call   10a0 <perror@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd81(%rip),%rdi
call   10a0 <perror@plt>
jmp    1244 <consume_cowsay+0x94>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [7 x i8] c"fdopen\00"
@.str.2 = [23 x i8] c"I think you meant git \00"
@.str.3 = [4 x i8] c" %s\00"
@.str.4 = [24 x i8] c"Error writing to cowsay\00"
define i32 @consume_cowsay(i32* %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i8**
%10 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%11 = load i32*, i32** %5
%12 = getelementptr inbounds i32, i32* %11, i64 0
%13 = load i32, i32* %12
%14 = call i32 @close(i32 %13)
%15 = load i32*, i32** %5
%16 = getelementptr inbounds i32, i32* %15, i64 1
%17 = load i32, i32* %16
%18 = call i32* @fdopen(i32 %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %8
%19 = load i32*, i32** %8
%20 = icmp ne i32* %19, null
br i1 %20, label %23, label %21
21: ; preds = %3
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %65
23: ; preds = %3
%24 = load i32*, i32** %8
%25 = call i32 @fputs(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32* %24)
%26 = load i8**, i8*** %7
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = getelementptr inbounds i8, i8* %28, i64 1
%30 = load i32*, i32** %8
%31 = call i32 @fputs(i8* %29, i32* %30)
%32 = load i8**, i8*** %7
%33 = getelementptr inbounds i8*, i8** %32, i64 2
store i8** %33, i8*** %9
br label %34
34: ; preds = %43, %23
%35 = load i8**, i8*** %9
%36 = load i8*, i8** %35
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %46
38: ; preds = %34
%39 = load i32*, i32** %8
%40 = load i8**, i8*** %9
%41 = load i8*, i8** %40
%42 = call i32 @fprintf(i32* %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %38
%44 = load i8**, i8*** %9
%45 = getelementptr inbounds i8*, i8** %44, i32 1
store i8** %45, i8*** %9
br label %34
46: ; preds = %34
%47 = load i32*, i32** %8
%48 = call i64 @fclose(i32* %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %50, %46
%53 = load i32, i32* %6
%54 = call i32 @waitpid(i32 %53, i32* %10, i32 0)
%55 = load i32, i32* %10
%56 = call i32 @WIFEXITED(i32 %55)
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %52
%59 = load i32, i32* %10
%60 = call i64 @WEXITSTATUS(i32 %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %58, %52
store i32 0, i32* %4
br label %65
63: ; preds = %58
%64 = call i32 @sleep(i32 1)
store i32 1, i32* %4
br label %65
65: ; preds = %63, %62, %21
%66 = load i32, i32* %4
ret i32 %66
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [7 x i8] c"fdopen\00"
@.str.2 = [23 x i8] c"I think you meant git \00"
@.str.3 = [4 x i8] c" %s\00"
@.str.4 = [24 x i8] c"Error writing to cowsay\00"
define i32 @consume_cowsay(i32* nocapture %0, i32 %1, i8** nocapture %2) {
%4 = alloca i32
%5 = load i32, i32* %0
%6 = tail call i32 @close(i32 %5)
%7 = getelementptr inbounds i32, i32* %0, i64 1
%8 = load i32, i32* %7
%9 = tail call i32* @fdopen(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %48
13: ; preds = %3
%14 = tail call i64 @fwrite(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i64 22, i64 1, i32* %9)
%15 = getelementptr inbounds i8*, i8** %2, i64 1
%16 = load i8*, i8** %15
%17 = getelementptr inbounds i8, i8* %16, i64 1
%18 = tail call i32 @fputs(i8* %17, i32* %9)
%19 = getelementptr inbounds i8*, i8** %2, i64 2
%20 = load i8*, i8** %19
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %25, %13
%23 = tail call i64 @fclose(i32* %9)
%24 = icmp eq i64 %23, 0
br i1 %24, label %34, label %32
25: ; preds = %13, %25
%26 = phi i8* [ %30, %25 ], [ %20, %13 ]
%27 = phi i8** [ %29, %25 ], [ %19, %13 ]
%28 = tail call i32 @fprintf(i32* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%29 = getelementptr inbounds i8*, i8** %27, i64 1
%30 = load i8*, i8** %29
%31 = icmp eq i8* %30, null
br i1 %31, label %22, label %25
32: ; preds = %22
%33 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %32, %22
%35 = bitcast i32* %4 to i8*
%36 = call i32 @waitpid(i32 %1, i32* %4, i32 0)
%37 = load i32, i32* %4
%38 = call i32 @WIFEXITED(i32 %37)
%39 = icmp eq i32 %38, 0
br i1 %39, label %46, label %40
40: ; preds = %34
%41 = load i32, i32* %4
%42 = call i64 @WEXITSTATUS(i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @sleep(i32 1)
br label %46
46: ; preds = %34, %40, %44
%47 = phi i32 [ 1, %44 ], [ 0, %40 ], [ 0, %34 ]
br label %48
48: ; preds = %46, %11
%49 = phi i32 [ %47, %46 ], [ 0, %11 ]
ret i32 %49
}