#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*/ isdigit(unsigned char);
scalar_t__ isprint(unsigned char);
int sprintf(char *, char *, unsigned char);
char *charstr(char *ptr, unsigned char c, unsigned char escape,
              unsigned char next) {
  if (isprint(c)) {
    if (c == escape || c == '\\')
      *ptr++ = '\\';
    *ptr++ = c;
    return ptr;
  }
  *ptr++ = '\\';
  switch (c) {
  case '\n':
    *ptr++ = 'n';
    return ptr;
  case '\t':
    *ptr++ = 't';
    return ptr;
  }
  if (!isdigit(next))
    return ptr + sprintf(ptr, "%o", c);
  return ptr + sprintf(ptr, "%03o", c);
}


x86 assembly (clang, -O0):
<charstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %cl,%al
mov    %dl,%cl
mov    %sil,%dl
mov    %rdi,-0x10(%rbp)
mov    %dl,-0x11(%rbp)
mov    %cl,-0x12(%rbp)
mov    %al,-0x13(%rbp)
movzbl -0x11(%rbp),%edi
call   1040 <isprint@plt>
cmp    $0x0,%rax
je     11af <charstr+0x7f>
movzbl -0x11(%rbp),%eax
movzbl -0x12(%rbp),%ecx
cmp    %ecx,%eax
je     117c <charstr+0x4c>
movzbl -0x11(%rbp),%eax
cmp    $0x5c,%eax
jne    118e <charstr+0x5e>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x5c,(%rax)
mov    -0x11(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1294 <charstr+0x164>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x5c,(%rax)
movzbl -0x11(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0x9,%eax
je     1206 <charstr+0xd6>
jmp    11d6 <charstr+0xa6>
mov    -0x18(%rbp),%eax
sub    $0xa,%eax
jne    1225 <charstr+0xf5>
jmp    11e7 <charstr+0xb7>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x6e,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1294 <charstr+0x164>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x74,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1294 <charstr+0x164>
movzbl -0x13(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    1268 <charstr+0x138>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdb6(%rip),%rsi
movzbl -0x11(%rbp),%edx
call   1050 <sprintf@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    1294 <charstr+0x164>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xd88(%rip),%rsi
movzbl -0x11(%rbp),%edx
call   1050 <sprintf@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<charstr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %esi,%edi
call   1040 <isprint@plt>
test   %rax,%rax
je     1175 <charstr+0x45>
cmp    $0x5c,%bpl
je     1159 <charstr+0x29>
cmp    %r15b,%bpl
jne    1160 <charstr+0x30>
movb   $0x5c,(%rbx)
add    $0x1,%rbx
mov    %bpl,(%rbx)
add    $0x1,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
movb   $0x5c,(%rbx)
cmp    $0x9,%bpl
je     118e <charstr+0x5e>
cmp    $0xa,%bpl
jne    1198 <charstr+0x68>
movb   $0x6e,0x1(%rbx)
add    $0x2,%rbx
jmp    1167 <charstr+0x37>
movb   $0x74,0x1(%rbx)
add    $0x2,%rbx
jmp    1167 <charstr+0x37>
add    $0x1,%rbx
movzbl %r14b,%edi
call   1030 <isdigit@plt>
movzbl %bpl,%edx
test   %eax,%eax
je     11b6 <charstr+0x86>
lea    0xe4f(%rip),%rsi
jmp    11bd <charstr+0x8d>
lea    0xe43(%rip),%rsi
mov    %rbx,%rdi
call   1050 <sprintf@plt>
cltq
add    %rax,%rbx
jmp    1167 <charstr+0x37>

LLVM IR (-O0):
@.str = [3 x i8] c"%o\00"
@.str.1 = [5 x i8] c"%03o\00"
define i8* @charstr(i8* %0, i8 zeroext %1, i8 zeroext %2, i8 zeroext %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
store i8* %0, i8** %6
store i8 %1, i8* %7
store i8 %2, i8* %8
store i8 %3, i8* %9
%10 = load i8, i8* %7
%11 = call i64 @isprint(i8 zeroext %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %31
13: ; preds = %4
%14 = load i8, i8* %7
%15 = zext i8 %14 to i32
%16 = load i8, i8* %8
%17 = zext i8 %16 to i32
%18 = icmp eq i32 %15, %17
br i1 %18, label %23, label %19
19: ; preds = %13
%20 = load i8, i8* %7
%21 = zext i8 %20 to i32
%22 = icmp eq i32 %21, 92
br i1 %22, label %23, label %26
23: ; preds = %19, %13
%24 = load i8*, i8** %6
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %6
store i8 92, i8* %24
br label %26
26: ; preds = %23, %19
%27 = load i8, i8* %7
%28 = load i8*, i8** %6
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %6
store i8 %27, i8* %28
%30 = load i8*, i8** %6
store i8* %30, i8** %5
br label %62
31: ; preds = %4
%32 = load i8*, i8** %6
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %6
store i8 92, i8* %32
%34 = load i8, i8* %7
%35 = zext i8 %34 to i32
switch i32 %35, label %44 [
i32 10, label %36
i32 9, label %40
]
36: ; preds = %31
%37 = load i8*, i8** %6
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %6
store i8 110, i8* %37
%39 = load i8*, i8** %6
store i8* %39, i8** %5
br label %62
40: ; preds = %31
%41 = load i8*, i8** %6
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %6
store i8 116, i8* %41
%43 = load i8*, i8** %6
store i8* %43, i8** %5
br label %62
44: ; preds = %31
%45 = load i8, i8* %9
%46 = call i32 @isdigit(i8 zeroext %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %55, label %48
48: ; preds = %44
%49 = load i8*, i8** %6
%50 = load i8*, i8** %6
%51 = load i8, i8* %7
%52 = call i32 @sprintf(i8* %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 zeroext %51)
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %49, i64 %53
store i8* %54, i8** %5
br label %62
55: ; preds = %44
%56 = load i8*, i8** %6
%57 = load i8*, i8** %6
%58 = load i8, i8* %7
%59 = call i32 @sprintf(i8* %57, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %58)
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %56, i64 %60
store i8* %61, i8** %5
br label %62
62: ; preds = %55, %48, %40, %36, %26
%63 = load i8*, i8** %5
ret i8* %63
}

LLVM IR (-O2):
@.str = [3 x i8] c"%o\00"
@.str.1 = [5 x i8] c"%03o\00"
define i8* @charstr(i8* writeonly %0, i8 zeroext %1, i8 zeroext %2, i8 zeroext %3) {
%5 = tail call i64 @isprint(i8 zeroext %1)
%6 = icmp eq i64 %5, 0
br i1 %6, label %16, label %7
7: ; preds = %4
%8 = icmp eq i8 %1, %2
%9 = icmp eq i8 %1, 92
%10 = or i1 %9, %8
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = getelementptr inbounds i8, i8* %0, i64 1
store i8 92, i8* %0
br label %13
13: ; preds = %7, %11
%14 = phi i8* [ %12, %11 ], [ %0, %7 ]
%15 = getelementptr inbounds i8, i8* %14, i64 1
store i8 %1, i8* %14
br label %33
16: ; preds = %4
%17 = getelementptr inbounds i8, i8* %0, i64 1
store i8 92, i8* %0
switch i8 %1, label %22 [
i8 10, label %18
i8 9, label %20
]
18: ; preds = %16
%19 = getelementptr inbounds i8, i8* %0, i64 2
store i8 110, i8* %17
br label %33
20: ; preds = %16
%21 = getelementptr inbounds i8, i8* %0, i64 2
store i8 116, i8* %17
br label %33
22: ; preds = %16
%23 = tail call i32 @isdigit(i8 zeroext %3)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = tail call i32 @sprintf(i8* %17, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 zeroext %1)
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %17, i64 %27
br label %33
29: ; preds = %22
%30 = tail call i32 @sprintf(i8* %17, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %1)
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %17, i64 %31
br label %33
33: ; preds = %29, %25, %20, %18, %13
%34 = phi i8* [ %15, %13 ], [ %32, %29 ], [ %28, %25 ], [ %21, %20 ], [ %19, %18 ]
ret i8* %34
}