#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*/ EXIT_FAILURE;
int /*<<< orphan*/ close(int);
scalar_t__ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, char *, ...);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *getenv(char *);
void exec_cowsay(int *pipefds) {
  close(pipefds[1]);
  if (dup2(pipefds[0], 0) < 0) {
    exit(EXIT_FAILURE);
  }
  char *cowfile = getenv("GI_COWFILE");
  if (cowfile && cowfile[0] != '\0') {
    execlp("cowsay", "cowsay", "-f", cowfile, NULL);
  } else {
    execlp("cowsay", "cowsay", NULL);
  }
}


x86 assembly (clang, -O0):
<exec_cowsay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
cmp    $0x0,%rax
jge    118d <exec_cowsay+0x3d>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
lea    0xe6c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11de <exec_cowsay+0x8e>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11de <exec_cowsay+0x8e>
mov    -0x10(%rbp),%rcx
lea    0xe48(%rip),%rsi
lea    0xe48(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
mov    $0x0,%al
call   1070 <execlp@plt>
jmp    11f3 <exec_cowsay+0xa3>
lea    0xe26(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
mov    $0x0,%al
call   1070 <execlp@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_cowsay>:
push   %rbx
mov    %rdi,%rbx
mov    0x4(%rdi),%edi
call   1050 <close@plt>
mov    (%rbx),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
test   %rax,%rax
js     11b3 <exec_cowsay+0x63>
lea    0xe8f(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     119f <exec_cowsay+0x4f>
cmpb   $0x0,(%rax)
je     119f <exec_cowsay+0x4f>
lea    0xe84(%rip),%rdi
lea    0xe84(%rip),%rdx
mov    %rdi,%rsi
mov    %rax,%rcx
xor    %r8d,%r8d
xor    %eax,%eax
pop    %rbx
jmp    1070 <execlp@plt>
lea    0xe65(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
xor    %eax,%eax
pop    %rbx
jmp    1070 <execlp@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [11 x i8] c"GI_COWFILE\00"
@.str.1 = [7 x i8] c"cowsay\00"
@.str.2 = [3 x i8] c"-f\00"
define void @exec_cowsay(i32* nocapture %0) {
%2 = getelementptr inbounds i32, i32* %0, i64 1
%3 = load i32, i32* %2
%4 = tail call i32 @close(i32 %3)
%5 = load i32, i32* %0
%6 = tail call i64 @dup2(i32 %5, i32 0)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %1
%12 = tail call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = load i8, i8* %12
%16 = icmp eq i8 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = tail call i32 (i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %12, i8* null)
br label %21
19: ; preds = %14, %11
%20 = tail call i32 (i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* null)
br label %21
21: ; preds = %19, %17
ret void
}