#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 ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FIONREAD;
int MAP_ANONYMOUS;
char *MAP_FAILED;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ _SC_PAGE_SIZE;
scalar_t__ do_write(int /*<<< orphan*/, char const *, int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int ioctl(int, int /*<<< orphan*/, int *);
char *mmap(int /*<<< orphan*/, int, int, int, int /*<<< orphan*/,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int strlen(char const *);
size_t sysconf(int /*<<< orphan*/);
bool dye_pipe(int in_fd, const char *color_string) {
  static char *buffer;
  static ssize_t buf_size;
  static size_t PSIZE;
  int nbyte;
  if (!buffer)
    PSIZE = sysconf(_SC_PAGE_SIZE);
  for (;;) {
    if (ioctl(in_fd, FIONREAD, &nbyte) == -1) {
      perror("ioctl");
      return false;
    }
    if (nbyte > buf_size) {
      if (nbyte % PSIZE)
        nbyte = nbyte + (PSIZE - nbyte % PSIZE);
      if (buffer)
        munmap(buffer, buf_size);
      buf_size = nbyte;
      buffer = mmap(0, buf_size, PROT_READ | PROT_WRITE,
                    MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
      if (buffer == MAP_FAILED) {
        perror("mmap");
        exit(1);
      }
    }
    while ((nbyte = read(in_fd, buffer, buf_size)) == -1 && errno == EINTR)
      ;
    if (nbyte <= 0)
      break;
    if (do_write(STDOUT_FILENO, color_string, strlen(color_string)) < 0)
      return false;
    if (do_write(STDOUT_FILENO, buffer, nbyte) < 0)
      return false;
  }
  return true;
}


x86 assembly (clang, -O0):
<dye_pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,0x2ec9(%rip)
jne    11c2 <dye_pipe+0x32>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %rax,0x2ebe(%rip)
jmp    11c7 <dye_pipe+0x37>
mov    -0x8(%rbp),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11fd <dye_pipe+0x6d>
lea    0xe14(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    13ce <dye_pipe+0x23e>
mov    -0x14(%rbp),%eax
cmp    0x2e72(%rip),%eax
jle    12e8 <dye_pipe+0x158>
movslq -0x14(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   0x2e65(%rip)
cmp    $0x0,%rdx
je     124e <dye_pipe+0xbe>
movslq -0x14(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e4c(%rip),%rcx
movslq -0x14(%rbp),%rax
xor    %edx,%edx
divq   0x2e3f(%rip)
mov    -0x20(%rbp),%rax
sub    %rdx,%rcx
add    %rcx,%rax
mov    %eax,-0x14(%rbp)
cmpq   $0x0,0x2e1a(%rip)
je     126e <dye_pipe+0xde>
mov    0x2e0d(%rip),%rdi
mov    0x2e0f(%rip),%esi
call   1080 <munmap@plt>
mov    -0x14(%rbp),%eax
mov    %eax,0x2e01(%rip)
mov    0x2dfb(%rip),%esi
mov    0x2d74(%rip),%rax
mov    (%rax),%edx
mov    0x2d23(%rip),%rax
or     (%rax),%edx
mov    0x2d3a(%rip),%rax
mov    (%rax),%ecx
mov    0x2d01(%rip),%rax
or     (%rax),%ecx
xor    %r9d,%r9d
mov    %r9d,%edi
mov    %r9d,%r8d
call   1050 <mmap@plt>
mov    %rax,0x2dba(%rip)
mov    0x2db3(%rip),%rax
mov    0x2cd4(%rip),%rcx
cmp    (%rcx),%rax
jne    12e3 <dye_pipe+0x153>
lea    0xd32(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    12e8 <dye_pipe+0x158>
jmp    12ed <dye_pipe+0x15d>
mov    -0x8(%rbp),%edi
mov    0x2d79(%rip),%rsi
mov    0x2d7b(%rip),%edx
call   1070 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x21(%rbp)
jne    132f <dye_pipe+0x19f>
mov    0x2ca4(%rip),%rax
mov    (%rax),%rax
mov    0x2c92(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    133f <dye_pipe+0x1af>
jmp    1344 <dye_pipe+0x1b4>
jmp    12ed <dye_pipe+0x15d>
cmpl   $0x0,-0x14(%rbp)
jg     1353 <dye_pipe+0x1c3>
jmp    13c7 <dye_pipe+0x237>
mov    0x2c8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <do_write@plt>
cmp    $0x0,%rax
jge    1394 <dye_pipe+0x204>
movl   $0x0,-0x4(%rbp)
jmp    13ce <dye_pipe+0x23e>
mov    0x2c4d(%rip),%rax
mov    (%rax),%edi
mov    0x2ccc(%rip),%rsi
mov    -0x14(%rbp),%edx
call   1030 <do_write@plt>
cmp    $0x0,%rax
jge    13c2 <dye_pipe+0x232>
movl   $0x0,-0x4(%rbp)
jmp    13ce <dye_pipe+0x23e>
jmp    11c7 <dye_pipe+0x37>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dye_pipe>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebp
cmpq   $0x0,0x2ec8(%rip)
jne    11bf <dye_pipe+0x2f>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %rax,0x2ec1(%rip)
mov    0x2dfa(%rip),%r13
mov    0x2deb(%rip),%r12
mov    0x2e14(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
lea    0x4(%rsp),%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1341 <dye_pipe+0x1b1>
mov    0x4(%rsp),%esi
mov    0x2e70(%rip),%ecx
cmp    %ecx,%esi
jle    12b0 <dye_pipe+0x120>
movslq %esi,%rax
mov    0x2e66(%rip),%rdi
mov    %rax,%rdx
or     %rdi,%rdx
shr    $0x20,%rdx
je     1317 <dye_pipe+0x187>
xor    %edx,%edx
div    %rdi
test   %rdx,%rdx
je     123c <dye_pipe+0xac>
sub    %edx,%edi
add    %edi,%esi
mov    %esi,0x4(%rsp)
mov    0x2e2d(%rip),%rdi
test   %rdi,%rdi
je     1253 <dye_pipe+0xc3>
mov    %ecx,%esi
call   1080 <munmap@plt>
mov    0x4(%rsp),%esi
mov    0x2d56(%rip),%rax
mov    (%rax),%edx
mov    0x2d95(%rip),%rax
or     (%rax),%edx
mov    %esi,0x2e0d(%rip)
mov    0x2d2e(%rip),%rax
mov    (%rax),%ecx
mov    0x2d55(%rip),%rax
or     (%rax),%ecx
xor    %edi,%edi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,0x2ddf(%rip)
mov    0x2d00(%rip),%rcx
cmp    (%rcx),%rax
je     1351 <dye_pipe+0x1c1>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x2db9(%rip),%rsi
mov    0x2dbb(%rip),%edx
mov    %ebp,%edi
call   1070 <read@plt>
mov    %eax,0x4(%rsp)
cmp    $0xffffffff,%eax
jne    12d7 <dye_pipe+0x147>
mov    (%r12),%rcx
cmp    %rcx,0x0(%r13)
je     12b0 <dye_pipe+0x120>
test   %eax,%eax
jle    132b <dye_pipe+0x19b>
mov    (%r14),%ebx
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <do_write@plt>
xor    %ebx,%ebx
test   %rax,%rax
js     1330 <dye_pipe+0x1a0>
mov    (%r14),%edi
mov    0x2d6d(%rip),%rsi
mov    0x4(%rsp),%edx
call   1030 <do_write@plt>
test   %rax,%rax
jns    11e0 <dye_pipe+0x50>
jmp    1330 <dye_pipe+0x1a0>
mov    %esi,%eax
xor    %edx,%edx
div    %edi
test   %rdx,%rdx
jne    1234 <dye_pipe+0xa4>
jmp    123c <dye_pipe+0xac>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcb8(%rip),%rdi
call   1090 <perror@plt>
xor    %ebx,%ebx
jmp    1330 <dye_pipe+0x1a0>
lea    0xcae(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@dye_pipe.buffer = internal global i8* null
@dye_pipe.buf_size = internal global i32 0
@dye_pipe.PSIZE = internal global i64 0
@_SC_PAGE_SIZE = global i32 0
@FIONREAD = global i32 0
@.str = [6 x i8] c"ioctl\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
@errno = global i64 0
@EINTR = global i64 0
@STDOUT_FILENO = global i32 0
define i32 @dye_pipe(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%7 = load i8*, i8** @dye_pipe.buffer
%8 = icmp ne i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i32, i32* @_SC_PAGE_SIZE
%11 = call i64 @sysconf(i32 %10)
store i64 %11, i64* @dye_pipe.PSIZE
br label %12
12: ; preds = %9, %2
br label %13
13: ; preds = %98, %12
%14 = load i32, i32* %4
%15 = load i32, i32* @FIONREAD
%16 = call i32 @ioctl(i32 %14, i32 %15, i32* %6)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %100
20: ; preds = %13
%21 = load i32, i32* %6
%22 = load i32, i32* @dye_pipe.buf_size
%23 = icmp sgt i32 %21, %22
br i1 %23, label %24, label %65
24: ; preds = %20
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = load i64, i64* @dye_pipe.PSIZE
%28 = urem i64 %26, %27
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %41
30: ; preds = %24
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = load i64, i64* @dye_pipe.PSIZE
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = load i64, i64* @dye_pipe.PSIZE
%37 = urem i64 %35, %36
%38 = sub i64 %33, %37
%39 = add i64 %32, %38
%40 = trunc i64 %39 to i32
store i32 %40, i32* %6
br label %41
41: ; preds = %30, %24
%42 = load i8*, i8** @dye_pipe.buffer
%43 = icmp ne i8* %42, null
br i1 %43, label %44, label %48
44: ; preds = %41
%45 = load i8*, i8** @dye_pipe.buffer
%46 = load i32, i32* @dye_pipe.buf_size
%47 = call i32 @munmap(i8* %45, i32 %46)
br label %48
48: ; preds = %44, %41
%49 = load i32, i32* %6
store i32 %49, i32* @dye_pipe.buf_size
%50 = load i32, i32* @dye_pipe.buf_size
%51 = load i32, i32* @PROT_READ
%52 = load i32, i32* @PROT_WRITE
%53 = or i32 %51, %52
%54 = load i32, i32* @MAP_ANONYMOUS
%55 = load i32, i32* @MAP_PRIVATE
%56 = or i32 %54, %55
%57 = call i8* @mmap(i32 0, i32 %50, i32 %53, i32 %56, i32 0, i32 0)
store i8* %57, i8** @dye_pipe.buffer
%58 = load i8*, i8** @dye_pipe.buffer
%59 = load i8*, i8** @MAP_FAILED
%60 = icmp eq i8* %58, %59
br i1 %60, label %61, label %64
61: ; preds = %48
%62 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %48
br label %65
65: ; preds = %64, %20
br label %66
66: ; preds = %78, %65
%67 = load i32, i32* %4
%68 = load i8*, i8** @dye_pipe.buffer
%69 = load i32, i32* @dye_pipe.buf_size
%70 = call i32 @read(i32 %67, i8* %68, i32 %69)
store i32 %70, i32* %6
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %76
72: ; preds = %66
%73 = load i64, i64* @errno
%74 = load i64, i64* @EINTR
%75 = icmp eq i64 %73, %74
br label %76
76: ; preds = %72, %66
%77 = phi i1 [ false, %66 ], [ %75, %72 ]
br i1 %77, label %78, label %79
78: ; preds = %76
br label %66
79: ; preds = %76
%80 = load i32, i32* %6
%81 = icmp sle i32 %80, 0
br i1 %81, label %82, label %83
82: ; preds = %79
br label %99
83: ; preds = %79
%84 = load i32, i32* @STDOUT_FILENO
%85 = load i8*, i8** %5
%86 = load i8*, i8** %5
%87 = call i32 @strlen(i8* %86)
%88 = call i64 @do_write(i32 %84, i8* %85, i32 %87)
%89 = icmp slt i64 %88, 0
br i1 %89, label %90, label %91
90: ; preds = %83
store i32 0, i32* %3
br label %100
91: ; preds = %83
%92 = load i32, i32* @STDOUT_FILENO
%93 = load i8*, i8** @dye_pipe.buffer
%94 = load i32, i32* %6
%95 = call i64 @do_write(i32 %92, i8* %93, i32 %94)
%96 = icmp slt i64 %95, 0
br i1 %96, label %97, label %98
97: ; preds = %91
store i32 0, i32* %3
br label %100
98: ; preds = %91
br label %13
99: ; preds = %82
store i32 1, i32* %3
br label %100
100: ; preds = %99, %97, %90, %18
%101 = load i32, i32* %3
ret i32 %101
}

LLVM IR (-O2):
@dye_pipe.buffer = internal unnamed_addr global i8* null
@dye_pipe.buf_size = internal unnamed_addr global i32 0
@dye_pipe.PSIZE = internal unnamed_addr global i64 0
@_SC_PAGE_SIZE = global i32 0
@FIONREAD = global i32 0
@.str = [6 x i8] c"ioctl\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
@errno = global i64 0
@EINTR = global i64 0
@STDOUT_FILENO = global i32 0
define i32 @dye_pipe(i32 %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i8*, i8** @dye_pipe.buffer
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @_SC_PAGE_SIZE
%9 = tail call i64 @sysconf(i32 %8)
store i64 %9, i64* @dye_pipe.PSIZE
br label %10
10: ; preds = %7, %2
br label %11
11: ; preds = %10, %68
%12 = load i32, i32* @FIONREAD
%13 = call i32 @ioctl(i32 %0, i32 %12, i32* %3)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %74
17: ; preds = %11
%18 = load i32, i32* %3
%19 = load i32, i32* @dye_pipe.buf_size
%20 = icmp sgt i32 %18, %19
br i1 %20, label %22, label %21
21: ; preds = %38, %17
br label %52
22: ; preds = %17
%23 = sext i32 %18 to i64
%24 = load i64, i64* @dye_pipe.PSIZE
%25 = urem i64 %23, %24
%26 = icmp eq i64 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %22
%28 = sub i64 %24, %25
%29 = trunc i64 %28 to i32
%30 = add i32 %18, %29
store i32 %30, i32* %3
br label %31
31: ; preds = %27, %22
%32 = phi i32 [ %30, %27 ], [ %18, %22 ]
%33 = load i8*, i8** @dye_pipe.buffer
%34 = icmp eq i8* %33, null
br i1 %34, label %38, label %35
35: ; preds = %31
%36 = call i32 @munmap(i8* %33, i32 %19)
%37 = load i32, i32* %3
br label %38
38: ; preds = %35, %31
%39 = phi i32 [ %37, %35 ], [ %32, %31 ]
store i32 %39, i32* @dye_pipe.buf_size
%40 = load i32, i32* @PROT_READ
%41 = load i32, i32* @PROT_WRITE
%42 = or i32 %41, %40
%43 = load i32, i32* @MAP_ANONYMOUS
%44 = load i32, i32* @MAP_PRIVATE
%45 = or i32 %44, %43
%46 = call i8* @mmap(i32 0, i32 %39, i32 %42, i32 %45, i32 0, i32 0)
store i8* %46, i8** @dye_pipe.buffer
%47 = load i8*, i8** @MAP_FAILED
%48 = icmp eq i8* %46, %47
br i1 %48, label %49, label %21
49: ; preds = %38
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %21, %52
%53 = load i8*, i8** @dye_pipe.buffer
%54 = load i32, i32* @dye_pipe.buf_size
%55 = call i32 @read(i32 %0, i8* %53, i32 %54)
store i32 %55, i32* %3
%56 = icmp eq i32 %55, -1
%57 = load i64, i64* @errno
%58 = load i64, i64* @EINTR
%59 = icmp eq i64 %57, %58
%60 = select i1 %56, i1 %59, i1 false
br i1 %60, label %52, label %61
61: ; preds = %52
%62 = icmp slt i32 %55, 1
br i1 %62, label %74, label %63
63: ; preds = %61
%64 = load i32, i32* @STDOUT_FILENO
%65 = call i32 @strlen(i8* %1)
%66 = call i64 @do_write(i32 %64, i8* %1, i32 %65)
%67 = icmp slt i64 %66, 0
br i1 %67, label %74, label %68
68: ; preds = %63
%69 = load i32, i32* @STDOUT_FILENO
%70 = load i8*, i8** @dye_pipe.buffer
%71 = load i32, i32* %3
%72 = call i64 @do_write(i32 %69, i8* %70, i32 %71)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %11
74: ; preds = %61, %68, %63, %15
%75 = phi i32 [ 0, %15 ], [ 1, %61 ], [ 0, %68 ], [ 0, %63 ]
ret i32 %75
}