#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 */
struct iovec {
  size_t iov_len;
  void *iov_base;
};
typedef size_t ssize_t;

/* Variables and functions */
size_t readfd(int *, char *, int);
int /*<<< orphan*/ write_iov(int, struct iovec *, unsigned int);
bool write_ansi(int *fdp, bool *is_on, bool want, int fda, const char *ansi,
                size_t size, int fdb, bool *try_splice) {
  bool ret = false;
  ssize_t len = 0;
  char buf[4096];
  struct iovec iov[2];
  unsigned niov = 0;
  if (*is_on != want) {
    *is_on = want;
    iov[niov].iov_base = (void *)ansi;
    iov[niov].iov_len = size;
    niov++;
  }
  if (fdp != NULL) {
    *try_splice = false;
    if (!*try_splice) {
      if (niov != 0 && fda != fdb) {
        if (!write_iov(fda, iov, niov))
          goto fail;
        niov = 0;
      }
      len = readfd(fdp, buf, sizeof buf);
      if (len < 0)
        goto fail;
      if (len > 0) {
        iov[niov].iov_base = (void *)buf;
        iov[niov].iov_len = len;
        niov++;
      }
    }
  }
  if (fdb >= 0 && niov > 0 && !write_iov(fdb, iov, niov))
    goto fail;
  ret = true;
fail:
  return ret;
}


x86 assembly (clang, -O0):
<write_ansi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1070,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%eax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x1064(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
cmp    -0x14(%rbp),%eax
je     11c3 <write_ansi+0xa3>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rcx
mov    -0x1064(%rbp),%eax
mov    %eax,%edx
lea    -0x1060(%rbp),%rax
shl    $0x4,%rdx
add    %rdx,%rax
mov    %rcx,0x8(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x1064(%rbp),%eax
mov    %eax,%edx
lea    -0x1060(%rbp),%rax
shl    $0x4,%rdx
add    %rdx,%rax
mov    %rcx,(%rax)
mov    -0x1064(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1064(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     12b6 <write_ansi+0x196>
mov    0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jne    12b1 <write_ansi+0x191>
cmpl   $0x0,-0x1064(%rbp)
je     122b <write_ansi+0x10b>
mov    -0x18(%rbp),%eax
cmp    0x10(%rbp),%eax
je     122b <write_ansi+0x10b>
mov    -0x18(%rbp),%edi
lea    -0x1060(%rbp),%rsi
mov    -0x1064(%rbp),%edx
call   1040 <write_iov@plt>
cmp    $0x0,%eax
jne    1221 <write_ansi+0x101>
jmp    12f7 <write_ansi+0x1d7>
movl   $0x0,-0x1064(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x1040(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <readfd@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jae    1254 <write_ansi+0x134>
jmp    12f7 <write_ansi+0x1d7>
cmpq   $0x0,-0x38(%rbp)
jbe    12ac <write_ansi+0x18c>
lea    -0x1040(%rbp),%rcx
mov    -0x1064(%rbp),%eax
mov    %eax,%edx
lea    -0x1060(%rbp),%rax
shl    $0x4,%rdx
add    %rdx,%rax
mov    %rcx,0x8(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x1064(%rbp),%eax
mov    %eax,%edx
lea    -0x1060(%rbp),%rax
shl    $0x4,%rdx
add    %rdx,%rax
mov    %rcx,(%rax)
mov    -0x1064(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1064(%rbp)
jmp    12b1 <write_ansi+0x191>
jmp    12b6 <write_ansi+0x196>
cmpl   $0x0,0x10(%rbp)
jl     12f0 <write_ansi+0x1d0>
cmpl   $0x0,-0x1064(%rbp)
jbe    12f0 <write_ansi+0x1d0>
mov    0x10(%rbp),%edi
lea    -0x1060(%rbp),%rsi
mov    -0x1064(%rbp),%edx
call   1040 <write_iov@plt>
cmp    $0x0,%eax
jne    12f0 <write_ansi+0x1d0>
jmp    12f7 <write_ansi+0x1d7>
movl   $0x1,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1070,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_ansi>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1028,%rsp
mov    %rdi,%rbx
mov    0x1058(%rsp),%rax
mov    0x1050(%rsp),%r14d
cmp    %edx,(%rsi)
jne    1157 <write_ansi+0x37>
test   %rbx,%rbx
je     11d6 <write_ansi+0xb6>
movl   $0x0,(%rax)
xor    %ebp,%ebp
jmp    1190 <write_ansi+0x70>
mov    %edx,(%rsi)
mov    %r8,0x8(%rsp)
mov    %r9,(%rsp)
test   %rbx,%rbx
je     11eb <write_ansi+0xcb>
movl   $0x0,(%rax)
mov    $0x1,%ebp
cmp    %r14d,%ecx
je     1190 <write_ansi+0x70>
mov    %rsp,%rsi
mov    %ecx,%edi
mov    $0x1,%edx
call   1040 <write_iov@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     11db <write_ansi+0xbb>
lea    0x20(%rsp),%r15
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1030 <readfd@plt>
test   %rax,%rax
je     11bc <write_ansi+0x9c>
mov    %ebp,%ecx
shl    $0x4,%rcx
mov    %r15,0x8(%rsp,%rcx,1)
mov    %rax,(%rsp,%rcx,1)
add    $0x1,%ebp
test   %r14d,%r14d
js     11d6 <write_ansi+0xb6>
test   %ebp,%ebp
je     11d6 <write_ansi+0xb6>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    %ebp,%edx
call   1040 <write_iov@plt>
test   %eax,%eax
je     11f7 <write_ansi+0xd7>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x1028,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%ebp
test   %r14d,%r14d
jns    11c1 <write_ansi+0xa1>
jmp    11d6 <write_ansi+0xb6>
xor    %ebp,%ebp
jmp    11db <write_ansi+0xbb>

LLVM IR (-O0):
%struct.iovec = type { i64, i8* }
define i32 @write_ansi(i32* %0, i32* %1, i32 %2, i32 %3, i8* %4, i64 %5, i32 %6, i32* %7) {
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i32
%16 = alloca i32*
%17 = alloca i32
%18 = alloca i64
%19 = alloca [4096 x i8]
%20 = alloca [2 x %struct.iovec]
%21 = alloca i32
store i32* %0, i32** %9
store i32* %1, i32** %10
store i32 %2, i32* %11
store i32 %3, i32* %12
store i8* %4, i8** %13
store i64 %5, i64* %14
store i32 %6, i32* %15
store i32* %7, i32** %16
store i32 0, i32* %17
store i64 0, i64* %18
store i32 0, i32* %21
%22 = load i32*, i32** %10
%23 = load i32, i32* %22
%24 = load i32, i32* %11
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %41
26: ; preds = %8
%27 = load i32, i32* %11
%28 = load i32*, i32** %10
store i32 %27, i32* %28
%29 = load i8*, i8** %13
%30 = load i32, i32* %21
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %20, i64 0, i64 %31
%33 = getelementptr inbounds %struct.iovec, %struct.iovec* %32, i32 0, i32 1
store i8* %29, i8** %33
%34 = load i64, i64* %14
%35 = load i32, i32* %21
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %20, i64 0, i64 %36
%38 = getelementptr inbounds %struct.iovec, %struct.iovec* %37, i32 0, i32 0
store i64 %34, i64* %38
%39 = load i32, i32* %21
%40 = add i32 %39, 1
store i32 %40, i32* %21
br label %41
41: ; preds = %26, %8
%42 = load i32*, i32** %9
%43 = icmp ne i32* %42, null
br i1 %43, label %44, label %89
44: ; preds = %41
%45 = load i32*, i32** %16
store i32 0, i32* %45
%46 = load i32*, i32** %16
%47 = load i32, i32* %46
%48 = icmp ne i32 %47, 0
br i1 %48, label %88, label %49
49: ; preds = %44
%50 = load i32, i32* %21
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %64
52: ; preds = %49
%53 = load i32, i32* %12
%54 = load i32, i32* %15
%55 = icmp ne i32 %53, %54
br i1 %55, label %56, label %64
56: ; preds = %52
%57 = load i32, i32* %12
%58 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %20, i64 0, i64 0
%59 = load i32, i32* %21
%60 = call i32 @write_iov(i32 %57, %struct.iovec* %58, i32 %59)
%61 = icmp ne i32 %60, 0
br i1 %61, label %63, label %62
62: ; preds = %56
br label %103
63: ; preds = %56
store i32 0, i32* %21
br label %64
64: ; preds = %63, %52, %49
%65 = load i32*, i32** %9
%66 = getelementptr inbounds [4096 x i8], [4096 x i8]* %19, i64 0, i64 0
%67 = call i64 @readfd(i32* %65, i8* %66, i32 4096)
store i64 %67, i64* %18
%68 = load i64, i64* %18
%69 = icmp ult i64 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %64
br label %103
71: ; preds = %64
%72 = load i64, i64* %18
%73 = icmp ugt i64 %72, 0
br i1 %73, label %74, label %87
74: ; preds = %71
%75 = getelementptr inbounds [4096 x i8], [4096 x i8]* %19, i64 0, i64 0
%76 = load i32, i32* %21
%77 = zext i32 %76 to i64
%78 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %20, i64 0, i64 %77
%79 = getelementptr inbounds %struct.iovec, %struct.iovec* %78, i32 0, i32 1
store i8* %75, i8** %79
%80 = load i64, i64* %18
%81 = load i32, i32* %21
%82 = zext i32 %81 to i64
%83 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %20, i64 0, i64 %82
%84 = getelementptr inbounds %struct.iovec, %struct.iovec* %83, i32 0, i32 0
store i64 %80, i64* %84
%85 = load i32, i32* %21
%86 = add i32 %85, 1
store i32 %86, i32* %21
br label %87
87: ; preds = %74, %71
br label %88
88: ; preds = %87, %44
br label %89
89: ; preds = %88, %41
%90 = load i32, i32* %15
%91 = icmp sge i32 %90, 0
br i1 %91, label %92, label %102
92: ; preds = %89
%93 = load i32, i32* %21
%94 = icmp ugt i32 %93, 0
br i1 %94, label %95, label %102
95: ; preds = %92
%96 = load i32, i32* %15
%97 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %20, i64 0, i64 0
%98 = load i32, i32* %21
%99 = call i32 @write_iov(i32 %96, %struct.iovec* %97, i32 %98)
%100 = icmp ne i32 %99, 0
br i1 %100, label %102, label %101
101: ; preds = %95
br label %103
102: ; preds = %95, %92, %89
store i32 1, i32* %17
br label %103
103: ; preds = %102, %101, %70, %62
%104 = load i32, i32* %17
ret i32 %104
}

LLVM IR (-O2):
%struct.iovec = type { i64, i8* }
define i32 @write_ansi(i32* %0, i32* nocapture %1, i32 %2, i32 %3, i8* %4, i64 %5, i32 %6, i32* nocapture writeonly %7) {
%9 = alloca [4096 x i8]
%10 = alloca [2 x %struct.iovec]
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%12 = bitcast [2 x %struct.iovec]* %10 to i8*
%13 = load i32, i32* %1
%14 = icmp eq i32 %13, %2
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = icmp eq i32* %0, null
br i1 %16, label %46, label %21
17: ; preds = %8
store i32 %2, i32* %1
%18 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %10, i64 0, i64 0, i32 1
store i8* %4, i8** %18
%19 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %10, i64 0, i64 0, i32 0
store i64 %5, i64* %19
%20 = icmp eq i32* %0, null
br i1 %20, label %37, label %22
21: ; preds = %15
store i32 0, i32* %7
br label %28
22: ; preds = %17
store i32 0, i32* %7
%23 = icmp eq i32 %3, %6
br i1 %23, label %28, label %24
24: ; preds = %22
%25 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %10, i64 0, i64 0
%26 = call i32 @write_iov(i32 %3, %struct.iovec* %25, i32 1)
%27 = icmp eq i32 %26, 0
br i1 %27, label %47, label %28
28: ; preds = %21, %24, %22
%29 = phi i32 [ 1, %22 ], [ 0, %21 ], [ 0, %24 ]
%30 = call i64 @readfd(i32* %0, i8* %11, i32 4096)
%31 = icmp eq i64 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %28
%33 = zext i32 %29 to i64
%34 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %10, i64 0, i64 %33, i32 1
store i8* %11, i8** %34
%35 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %10, i64 0, i64 %33, i32 0
store i64 %30, i64* %35
%36 = add nuw nsw i32 %29, 1
br label %37
37: ; preds = %17, %32, %28
%38 = phi i32 [ %36, %32 ], [ %29, %28 ], [ 1, %17 ]
%39 = icmp sgt i32 %6, -1
%40 = icmp ne i32 %38, 0
%41 = and i1 %39, %40
br i1 %41, label %42, label %46
42: ; preds = %37
%43 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %10, i64 0, i64 0
%44 = call i32 @write_iov(i32 %6, %struct.iovec* %43, i32 %38)
%45 = icmp eq i32 %44, 0
br i1 %45, label %47, label %46
46: ; preds = %15, %42, %37
br label %47
47: ; preds = %42, %24, %46
%48 = phi i32 [ 1, %46 ], [ 0, %42 ], [ 0, %24 ]
ret i32 %48
}