#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*/ err(int, char *);
int sprintf(char *, char *, ...);
const char *fmtesc(int fg, int bg, int bold) {
  static char buf[sizeof "\e[1;30;40m"];
  char *writep;
  int addcolon;
  int n;
  writep = buf;
  addcolon = 0;
  do {
    n = sprintf(writep, "\e[");
    if (n == -1)
      err(1, "sprintf");
    writep += n;
  } while (0);
  if (bold) {
    do {
      if (addcolon++)
        *writep++ = ';';
    } while (0);
    do {
      n = sprintf(writep, "1");
      if (n == -1)
        err(1, "sprintf");
      writep += n;
    } while (0);
  }
  if (fg > -1) {
    do {
      if (addcolon++)
        *writep++ = ';';
    } while (0);
    do {
      n = sprintf(writep, "%d", 30 + fg);
      if (n == -1)
        err(1, "sprintf");
      writep += n;
    } while (0);
  }
  if (bg > -1) {
    do {
      if (addcolon++)
        *writep++ = ';';
    } while (0);
    do {
      n = sprintf(writep, "%d", 40 + bg);
      if (n == -1)
        err(1, "sprintf");
      writep += n;
    } while (0);
  }
  do {
    n = sprintf(writep, "m");
    if (n == -1)
      err(1, "sprintf");
    writep += n;
  } while (0);
  return buf;
}


x86 assembly (clang, -O0):
<fmtesc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
lea    0x2ef9(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xeb2(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1173 <fmtesc+0x53>
mov    $0x1,%edi
lea    0xe95(%rip),%rsi
call   1030 <err@plt>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1209 <fmtesc+0xe9>
jmp    1193 <fmtesc+0x73>
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x1c(%rbp)
cmp    $0x0,%eax
je     11b9 <fmtesc+0x99>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x3b,(%rax)
jmp    11be <fmtesc+0x9e>
jmp    11c3 <fmtesc+0xa3>
mov    -0x18(%rbp),%rdi
lea    0xe3d(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11f3 <fmtesc+0xd3>
mov    $0x1,%edi
lea    0xe15(%rip),%rsi
call   1030 <err@plt>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1209 <fmtesc+0xe9>
cmpl   $0xffffffff,-0x4(%rbp)
jle    1294 <fmtesc+0x174>
jmp    1218 <fmtesc+0xf8>
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x1c(%rbp)
cmp    $0x0,%eax
je     123e <fmtesc+0x11e>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x3b,(%rax)
jmp    1243 <fmtesc+0x123>
jmp    1248 <fmtesc+0x128>
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%edx
add    $0x1e,%edx
lea    0xdb4(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    127e <fmtesc+0x15e>
mov    $0x1,%edi
lea    0xd8a(%rip),%rsi
call   1030 <err@plt>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1294 <fmtesc+0x174>
cmpl   $0xffffffff,-0x8(%rbp)
jle    131f <fmtesc+0x1ff>
jmp    12a3 <fmtesc+0x183>
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x1c(%rbp)
cmp    $0x0,%eax
je     12c9 <fmtesc+0x1a9>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x3b,(%rax)
jmp    12ce <fmtesc+0x1ae>
jmp    12d3 <fmtesc+0x1b3>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%edx
add    $0x28,%edx
lea    0xd29(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1309 <fmtesc+0x1e9>
mov    $0x1,%edi
lea    0xcff(%rip),%rsi
call   1030 <err@plt>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    131f <fmtesc+0x1ff>
jmp    1324 <fmtesc+0x204>
mov    -0x18(%rbp),%rdi
lea    0xce1(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1354 <fmtesc+0x234>
mov    $0x1,%edi
lea    0xcb4(%rip),%rsi
call   1030 <err@plt>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
lea    0x2cc5(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fmtesc>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%eax
movb   $0x0,0x2f03(%rip)
movw   $0x5b1b,0x2ef8(%rip)
1b 5b
test   %edx,%edx
je     115a <fmtesc+0x3a>
movw   $0x31,0x2eed(%rip)
test   %eax,%eax
js     11a4 <fmtesc+0x84>
movb   $0x3b,0x2ee3(%rip)
lea    0x2edd(%rip),%rbx
jmp    1169 <fmtesc+0x49>
lea    0x2ed2(%rip),%rbx
test   %eax,%eax
js     11fe <fmtesc+0xde>
add    $0x1e,%eax
lea    0xe98(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <sprintf@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1197 <fmtesc+0x77>
lea    0xe76(%rip),%rsi
mov    $0x1,%edi
call   1030 <err@plt>
movslq %ebp,%rax
add    %rax,%rbx
test   %r14d,%r14d
jns    11b0 <fmtesc+0x90>
jmp    11ed <fmtesc+0xcd>
lea    0x2e89(%rip),%rbx
test   %r14d,%r14d
js     11ed <fmtesc+0xcd>
movb   $0x3b,(%rbx)
add    $0x1,%rbx
add    $0x28,%r14d
lea    0xe49(%rip),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
xor    %eax,%eax
call   1040 <sprintf@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11e7 <fmtesc+0xc7>
lea    0xe26(%rip),%rsi
mov    $0x1,%edi
call   1030 <err@plt>
movslq %ebp,%rax
add    %rax,%rbx
movw   $0x6d,(%rbx)
lea    0x2e38(%rip),%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
test   %r14d,%r14d
jns    11b7 <fmtesc+0x97>
jmp    11ed <fmtesc+0xcd>

LLVM IR (-O0):
@fmtesc.buf = internal global [11 x i8] zeroinitializer
@.str = [3 x i8] c"\1B[\00"
@.str.1 = [8 x i8] c"sprintf\00"
@.str.2 = [2 x i8] c"1\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [2 x i8] c"m\00"
define i8* @fmtesc(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @fmtesc.buf, i64 0, i64 0), i8** %7
store i32 0, i32* %8
br label %10
10: ; preds = %3
%11 = load i8*, i8** %7
%12 = call i32 (i8*, i8*, ...) @sprintf(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %10
%18 = load i32, i32* %9
%19 = load i8*, i8** %7
%20 = sext i32 %18 to i64
%21 = getelementptr inbounds i8, i8* %19, i64 %20
store i8* %21, i8** %7
br label %22
22: ; preds = %17
%23 = load i32, i32* %6
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %48
25: ; preds = %22
br label %26
26: ; preds = %25
%27 = load i32, i32* %8
%28 = add nsw i32 %27, 1
store i32 %28, i32* %8
%29 = icmp ne i32 %27, 0
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i8*, i8** %7
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %7
store i8 59, i8* %31
br label %33
33: ; preds = %30, %26
br label %34
34: ; preds = %33
br label %35
35: ; preds = %34
%36 = load i8*, i8** %7
%37 = call i32 (i8*, i8*, ...) @sprintf(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %40, %35
%43 = load i32, i32* %9
%44 = load i8*, i8** %7
%45 = sext i32 %43 to i64
%46 = getelementptr inbounds i8, i8* %44, i64 %45
store i8* %46, i8** %7
br label %47
47: ; preds = %42
br label %48
48: ; preds = %47, %22
%49 = load i32, i32* %4
%50 = icmp sgt i32 %49, -1
br i1 %50, label %51, label %76
51: ; preds = %48
br label %52
52: ; preds = %51
%53 = load i32, i32* %8
%54 = add nsw i32 %53, 1
store i32 %54, i32* %8
%55 = icmp ne i32 %53, 0
br i1 %55, label %56, label %59
56: ; preds = %52
%57 = load i8*, i8** %7
%58 = getelementptr inbounds i8, i8* %57, i32 1
store i8* %58, i8** %7
store i8 59, i8* %57
br label %59
59: ; preds = %56, %52
br label %60
60: ; preds = %59
br label %61
61: ; preds = %60
%62 = load i8*, i8** %7
%63 = load i32, i32* %4
%64 = add nsw i32 30, %63
%65 = call i32 (i8*, i8*, ...) @sprintf(i8* %62, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %64)
store i32 %65, i32* %9
%66 = load i32, i32* %9
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %70
68: ; preds = %61
%69 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %70
70: ; preds = %68, %61
%71 = load i32, i32* %9
%72 = load i8*, i8** %7
%73 = sext i32 %71 to i64
%74 = getelementptr inbounds i8, i8* %72, i64 %73
store i8* %74, i8** %7
br label %75
75: ; preds = %70
br label %76
76: ; preds = %75, %48
%77 = load i32, i32* %5
%78 = icmp sgt i32 %77, -1
br i1 %78, label %79, label %104
79: ; preds = %76
br label %80
80: ; preds = %79
%81 = load i32, i32* %8
%82 = add nsw i32 %81, 1
store i32 %82, i32* %8
%83 = icmp ne i32 %81, 0
br i1 %83, label %84, label %87
84: ; preds = %80
%85 = load i8*, i8** %7
%86 = getelementptr inbounds i8, i8* %85, i32 1
store i8* %86, i8** %7
store i8 59, i8* %85
br label %87
87: ; preds = %84, %80
br label %88
88: ; preds = %87
br label %89
89: ; preds = %88
%90 = load i8*, i8** %7
%91 = load i32, i32* %5
%92 = add nsw i32 40, %91
%93 = call i32 (i8*, i8*, ...) @sprintf(i8* %90, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %92)
store i32 %93, i32* %9
%94 = load i32, i32* %9
%95 = icmp eq i32 %94, -1
br i1 %95, label %96, label %98
96: ; preds = %89
%97 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %98
98: ; preds = %96, %89
%99 = load i32, i32* %9
%100 = load i8*, i8** %7
%101 = sext i32 %99 to i64
%102 = getelementptr inbounds i8, i8* %100, i64 %101
store i8* %102, i8** %7
br label %103
103: ; preds = %98
br label %104
104: ; preds = %103, %76
br label %105
105: ; preds = %104
%106 = load i8*, i8** %7
%107 = call i32 (i8*, i8*, ...) @sprintf(i8* %106, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32 %107, i32* %9
%108 = load i32, i32* %9
%109 = icmp eq i32 %108, -1
br i1 %109, label %110, label %112
110: ; preds = %105
%111 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %112
112: ; preds = %110, %105
%113 = load i32, i32* %9
%114 = load i8*, i8** %7
%115 = sext i32 %113 to i64
%116 = getelementptr inbounds i8, i8* %114, i64 %115
store i8* %116, i8** %7
br label %117
117: ; preds = %112
ret i8* getelementptr inbounds ([11 x i8], [11 x i8]* @fmtesc.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@fmtesc.buf = internal global [11 x i8] zeroinitializer
@.str = [3 x i8] c"\1B[\00"
@.str.1 = [8 x i8] c"sprintf\00"
@.str.3 = [3 x i8] c"%d\00"
define i8* @fmtesc(i32 %0, i32 %1, i32 %2) {
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(3) getelementptr inbounds ([11 x i8], [11 x i8]* @fmtesc.buf, i64 0, i64 0), i8* align 1 dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i64 3, i1 false)
%4 = icmp eq i32 %2, 0
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = icmp sgt i32 %0, -1
br i1 %6, label %10, label %23
7: ; preds = %3
store i16 49, i16* bitcast (i8* getelementptr inbounds ([11 x i8], [11 x i8]* @fmtesc.buf, i64 0, i64 2) to i16*)
%8 = icmp sgt i32 %0, -1
br i1 %8, label %9, label %20
9: ; preds = %7
store i8 59, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @fmtesc.buf, i64 0, i64 3)
br label %10
10: ; preds = %5, %9
%11 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @fmtesc.buf, i64 0, i64 4), %9 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @fmtesc.buf, i64 0, i64 2), %5 ]
%12 = add nuw nsw i32 %0, 30
%13 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %11, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %10
%18 = sext i32 %13 to i64
%19 = getelementptr inbounds i8, i8* %11, i64 %18
br label %20
20: ; preds = %7, %17
%21 = phi i8* [ %19, %17 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @fmtesc.buf, i64 0, i64 3), %7 ]
%22 = icmp sgt i32 %1, -1
br i1 %22, label %25, label %37
23: ; preds = %5
%24 = icmp sgt i32 %1, -1
br i1 %24, label %27, label %37
25: ; preds = %20
%26 = getelementptr inbounds i8, i8* %21, i64 1
store i8 59, i8* %21
br label %27
27: ; preds = %23, %25
%28 = phi i8* [ %26, %25 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @fmtesc.buf, i64 0, i64 2), %23 ]
%29 = add nuw nsw i32 %1, 40
%30 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %28, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %27
%35 = sext i32 %30 to i64
%36 = getelementptr inbounds i8, i8* %28, i64 %35
br label %37
37: ; preds = %23, %20, %34
%38 = phi i8* [ %36, %34 ], [ %21, %20 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @fmtesc.buf, i64 0, i64 2), %23 ]
%39 = bitcast i8* %38 to i16*
store i16 109, i16* %39
ret i8* getelementptr inbounds ([11 x i8], [11 x i8]* @fmtesc.buf, i64 0, i64 0)
}