#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 snprintf(char *, int, char *, char);
int prepare_printable_name(char *buf, int bufsz, char *name) {
  int i, x = 0;
  if (!name || !buf) {
    return 0;
  }
  for (i = 0; name[i]; i++) {
    switch (name[i]) {
    case '\a':
    case '\b':
    case '\f':
    case '\n':
    case '\r':
    case '\v':
      x += snprintf(buf + x, bufsz - x, "\\%03o", name[i]);
      break;
    default:
      x += snprintf(buf + x, bufsz - x, "%c", name[i]);
    }
  }
  return x;
}


x86 assembly (clang, -O0):
<prepare_printable_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1140 <prepare_printable_name+0x30>
cmpq   $0x0,-0x10(%rbp)
jne    114c <prepare_printable_name+0x3c>
movl   $0x0,-0x4(%rbp)
jmp    1215 <prepare_printable_name+0x105>
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     120f <prepare_printable_name+0xff>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x2c(%rbp)
add    $0xfffffff9,%eax
sub    $0x2,%eax
jb     1199 <prepare_printable_name+0x89>
jmp    1185 <prepare_printable_name+0x75>
mov    -0x2c(%rbp),%eax
add    $0xfffffff6,%eax
sub    $0x3,%eax
ja     11cd <prepare_printable_name+0xbd>
jmp    1199 <prepare_printable_name+0x89>
mov    -0x10(%rbp),%rdi
movslq -0x28(%rbp),%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
sub    -0x28(%rbp),%esi
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
lea    0xe47(%rip),%rdx
movsbl (%rax,%rcx,1),%ecx
call   1030 <snprintf@plt>
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    11fc <prepare_printable_name+0xec>
mov    -0x10(%rbp),%rdi
movslq -0x28(%rbp),%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
sub    -0x28(%rbp),%esi
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
lea    0xe19(%rip),%rdx
movsbl (%rax,%rcx,1),%ecx
call   1030 <snprintf@plt>
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    1201 <prepare_printable_name+0xf1>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1153 <prepare_printable_name+0x43>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepare_printable_name>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
test   %rdi,%rdi
je     118a <prepare_printable_name+0x7a>
mov    %rdx,%rbx
test   %rdx,%rdx
je     118a <prepare_printable_name+0x7a>
mov    (%rbx),%cl
test   %cl,%cl
je     118a <prepare_printable_name+0x7a>
mov    %esi,%r14d
mov    %rdi,%r13
add    $0x1,%rbx
xor    %eax,%eax
lea    0xec3(%rip),%r15
lea    0xec2(%rip),%r12
jmp    1170 <prepare_printable_name+0x60>
nopl   0x0(%rax)
movslq %eax,%rbp
mov    %r13,%rdi
add    %rbp,%rdi
mov    %r14d,%esi
sub    %ebp,%esi
call   1030 <snprintf@plt>
add    %ebp,%eax
movzbl (%rbx),%ecx
add    $0x1,%rbx
test   %cl,%cl
je     118a <prepare_printable_name+0x7a>
movsbl %cl,%ecx
lea    -0x7(%rcx),%esi
mov    %r15,%rdx
cmp    $0x6,%esi
ja     1150 <prepare_printable_name+0x40>
movslq %esi,%rdx
movslq (%r12,%rdx,4),%rdx
add    %r12,%rdx
jmp    1150 <prepare_printable_name+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"\\%03o\00"
@.str.1 = [3 x i8] c"%c\00"
@reltable.prepare_printable_name = [7 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([6 x i8]* @.str to i64), i64 ptrtoint ([7 x i32]* @reltable.prepare_printable_name to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([6 x i8]* @.str to i64), i64 ptrtoint ([7 x i32]* @reltable.prepare_printable_name to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.1 to i64), i64 ptrtoint ([7 x i32]* @reltable.prepare_printable_name to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([6 x i8]* @.str to i64), i64 ptrtoint ([7 x i32]* @reltable.prepare_printable_name to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([6 x i8]* @.str to i64), i64 ptrtoint ([7 x i32]* @reltable.prepare_printable_name to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([6 x i8]* @.str to i64), i64 ptrtoint ([7 x i32]* @reltable.prepare_printable_name to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([6 x i8]* @.str to i64), i64 ptrtoint ([7 x i32]* @reltable.prepare_printable_name to i64)) to i32)]
define i32 @prepare_printable_name(i8* %0, i32 %1, i8* %2) {
%4 = icmp ne i8* %2, null
%5 = icmp ne i8* %0, null
%6 = and i1 %5, %4
br i1 %6, label %7, label %32
7: ; preds = %3
%8 = load i8, i8* %2
%9 = icmp eq i8 %8, 0
br i1 %9, label %32, label %10
10: ; preds = %7, %21
%11 = phi i64 [ %28, %21 ], [ 0, %7 ]
%12 = phi i8 [ %30, %21 ], [ %8, %7 ]
%13 = phi i32 [ %27, %21 ], [ 0, %7 ]
%14 = sext i8 %12 to i32
%15 = add nsw i32 %14, -7
%16 = icmp ult i32 %15, 7
br i1 %16, label %17, label %21
17: ; preds = %10
%18 = sext i32 %15 to i64
%19 = shl i64 %18, 2
%20 = call i8* @llvm.load.relative.i64(i8* bitcast ([7 x i32]* @reltable.prepare_printable_name to i8*), i64 %19)
br label %21
21: ; preds = %10, %17
%22 = phi i8* [ %20, %17 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %10 ]
%23 = sext i32 %13 to i64
%24 = getelementptr inbounds i8, i8* %0, i64 %23
%25 = sub nsw i32 %1, %13
%26 = tail call i32 @snprintf(i8* %24, i32 %25, i8* %22, i8 signext %12)
%27 = add nsw i32 %26, %13
%28 = add nuw i64 %11, 1
%29 = getelementptr inbounds i8, i8* %2, i64 %28
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 0
br i1 %31, label %32, label %10
32: ; preds = %21, %7, %3
%33 = phi i32 [ 0, %3 ], [ 0, %7 ], [ %27, %21 ]
ret i32 %33
}