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

/* Variables and functions */
int /*<<< orphan*/ write(int, char *, int);
ssize_t write_without_ansi(int fd, const void *buf, size_t count) {
  int state = 0, i;
  char ch, esc = 0x1B;
  for (i = 0; i < count; i++) {
    ch = ((char *)buf)[i];
    switch (state) {
    case 0:
      if (ch == 0x1B) {
        state = 1;
      } else {
        write(fd, &ch, 1);
      }
      break;
    case 1:
      if (ch == '[') {
        state = 2;
      } else {
        state = 0;
        write(fd, &esc, 1);
      }
      break;
    case 2:
      if (((ch) >= 0x40 && (ch) <= 0x7E)) {
        state = 0;
      }
    }
  }
}


x86 assembly (clang, -O0):
<write_without_ansi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movb   $0x1b,-0x22(%rbp)
movl   $0x0,-0x20(%rbp)
movslq -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1224 <write_without_ansi+0x114>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x21(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
test   %eax,%eax
je     1186 <write_without_ansi+0x76>
jmp    1164 <write_without_ansi+0x54>
mov    -0x28(%rbp),%eax
sub    $0x1,%eax
je     11b5 <write_without_ansi+0xa5>
jmp    1175 <write_without_ansi+0x65>
mov    -0x28(%rbp),%eax
sub    $0x2,%eax
je     11eb <write_without_ansi+0xdb>
jmp    1211 <write_without_ansi+0x101>
movsbl -0x21(%rbp),%eax
cmp    $0x1b,%eax
jne    119f <write_without_ansi+0x8f>
movl   $0x1,-0x1c(%rbp)
jmp    11b0 <write_without_ansi+0xa0>
mov    -0x8(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1211 <write_without_ansi+0x101>
movsbl -0x21(%rbp),%eax
cmp    $0x5b,%eax
jne    11ce <write_without_ansi+0xbe>
movl   $0x2,-0x1c(%rbp)
jmp    11e6 <write_without_ansi+0xd6>
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1211 <write_without_ansi+0x101>
movsbl -0x21(%rbp),%eax
cmp    $0x40,%eax
jl     120c <write_without_ansi+0xfc>
movsbl -0x21(%rbp),%eax
cmp    $0x7e,%eax
jg     120c <write_without_ansi+0xfc>
movl   $0x0,-0x1c(%rbp)
jmp    1211 <write_without_ansi+0x101>
jmp    1216 <write_without_ansi+0x106>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1135 <write_without_ansi+0x25>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_without_ansi>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movb   $0x1b,0x6(%rsp)
test   %rdx,%rdx
je     119e <write_without_ansi+0x8e>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r13d
xor    %ebp,%ebp
lea    0x6(%rsp),%r15
lea    0x7(%rsp),%r12
xor    %eax,%eax
jmp    1156 <write_without_ansi+0x46>
xchg   %ax,%ax
add    $0xc0,%cl
xor    %eax,%eax
cmp    $0x3f,%cl
setae  %al
add    %eax,%eax
add    $0x1,%rbp
cmp    %rbp,%rbx
je     119e <write_without_ansi+0x8e>
movzbl (%r14,%rbp,1),%ecx
mov    %cl,0x7(%rsp)
cmp    $0x2,%eax
je     1140 <write_without_ansi+0x30>
cmp    $0x1,%eax
je     1180 <write_without_ansi+0x70>
test   %eax,%eax
jne    114d <write_without_ansi+0x3d>
mov    $0x1,%eax
cmp    $0x1b,%cl
je     114d <write_without_ansi+0x3d>
mov    %r13d,%edi
mov    %r12,%rsi
jmp    1190 <write_without_ansi+0x80>
nop
mov    $0x2,%eax
cmp    $0x5b,%cl
je     114d <write_without_ansi+0x3d>
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
xor    %eax,%eax
jmp    114d <write_without_ansi+0x3d>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @write_without_ansi(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
%11 = alloca i8
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %8
store i8 27, i8* %11
store i32 0, i32* %9
br label %12
12: ; preds = %53, %3
%13 = load i32, i32* %9
%14 = sext i32 %13 to i64
%15 = load i64, i64* %7
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %56
17: ; preds = %12
%18 = load i8*, i8** %6
%19 = load i32, i32* %9
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
store i8 %22, i8* %10
%23 = load i32, i32* %8
switch i32 %23, label %52 [
i32 0, label %24
i32 1, label %33
i32 2, label %42
]
24: ; preds = %17
%25 = load i8, i8* %10
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 27
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 1, i32* %8
br label %32
29: ; preds = %24
%30 = load i32, i32* %5
%31 = call i32 @write(i32 %30, i8* %10, i32 1)
br label %32
32: ; preds = %29, %28
br label %52
33: ; preds = %17
%34 = load i8, i8* %10
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 91
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 2, i32* %8
br label %41
38: ; preds = %33
store i32 0, i32* %8
%39 = load i32, i32* %5
%40 = call i32 @write(i32 %39, i8* %11, i32 1)
br label %41
41: ; preds = %38, %37
br label %52
42: ; preds = %17
%43 = load i8, i8* %10
%44 = sext i8 %43 to i32
%45 = icmp sge i32 %44, 64
br i1 %45, label %46, label %51
46: ; preds = %42
%47 = load i8, i8* %10
%48 = sext i8 %47 to i32
%49 = icmp sle i32 %48, 126
br i1 %49, label %50, label %51
50: ; preds = %46
store i32 0, i32* %8
br label %51
51: ; preds = %50, %46, %42
br label %52
52: ; preds = %51, %17, %41, %32
br label %53
53: ; preds = %52
%54 = load i32, i32* %9
%55 = add nsw i32 %54, 1
store i32 %55, i32* %9
br label %12
56: ; preds = %12
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
define i32 @write_without_ansi(i32 %0, i8* nocapture %1, i64 %2) {
%4 = alloca i8
%5 = alloca i8
store i8 27, i8* %5
%6 = icmp eq i64 %2, 0
br i1 %6, label %28, label %7
7: ; preds = %3, %24
%8 = phi i64 [ %26, %24 ], [ 0, %3 ]
%9 = phi i32 [ %25, %24 ], [ 0, %3 ]
%10 = getelementptr inbounds i8, i8* %1, i64 %8
%11 = load i8, i8* %10
store i8 %11, i8* %4
switch i32 %9, label %24 [
i32 0, label %12
i32 1, label %16
i32 2, label %20
]
12: ; preds = %7
%13 = icmp eq i8 %11, 27
br i1 %13, label %24, label %14
14: ; preds = %12
%15 = call i32 @write(i32 %0, i8* %4, i32 1)
br label %24
16: ; preds = %7
%17 = icmp eq i8 %11, 91
br i1 %17, label %24, label %18
18: ; preds = %16
%19 = call i32 @write(i32 %0, i8* %5, i32 1)
br label %24
20: ; preds = %7
%21 = add i8 %11, -64
%22 = icmp ult i8 %21, 63
%23 = select i1 %22, i32 0, i32 2
br label %24
24: ; preds = %20, %16, %12, %7, %14, %18
%25 = phi i32 [ %9, %7 ], [ 0, %18 ], [ 0, %14 ], [ 1, %12 ], [ 2, %16 ], [ %23, %20 ]
%26 = add nuw i64 %8, 1
%27 = icmp eq i64 %26, %2
br i1 %27, label %28, label %7
28: ; preds = %24, %3
ret i32 undef
}