#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*/ tmp;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ isprint(unsigned char);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned char);
int /*<<< orphan*/ stderr;
char *strdup(char *);
char *test_format_data(const char *data, size_t sz) {
  static char buf[1024];
  char *optr;
  size_t olen;
  optr = buf;
  olen = 0;
  for (size_t i = 0; i < sz; i++) {
    char c;
    c = data[i];
    if (c == '"' || c == '\r' || c == '\n' || c == '\t') {
      if (olen >= sizeof(buf) - 2)
        goto overflow;
      *optr++ = '\\';
      if (c == '"') {
        *optr++ = data[i];
      } else if (c == '\r') {
        *optr++ = 'r';
      } else if (c == '\n') {
        *optr++ = 'n';
      } else if (c == '\t') {
        *optr++ = 't';
      }
      olen += 2;
    } else if (isprint((unsigned char)c)) {
      if (olen >= sizeof(buf) - 1)
        goto overflow;
      *optr++ = data[i];
      olen += 1;
    } else {
      char tmp[5];
      if (olen >= sizeof(buf) - 4)
        goto overflow;
      snprintf(tmp, sizeof(tmp), "\\x%02x", (unsigned char)data[i]);
      *optr++ = tmp[0];
      *optr++ = tmp[1];
      *optr++ = tmp[2];
      *optr++ = tmp[3];
      olen += 4;
    }
  }
  *optr = '\0';
  return strdup(buf);
overflow:
  fprintf(stderr, "data too large for buffer\n");
  abort();
}


x86 assembly (clang, -O0):
<test_format_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0x2ef9(%rip),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    13a3 <test_format_data+0x253>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x29(%rbp)
movsbl -0x29(%rbp),%eax
cmp    $0x22,%eax
je     11cb <test_format_data+0x7b>
movsbl -0x29(%rbp),%eax
cmp    $0xd,%eax
je     11cb <test_format_data+0x7b>
movsbl -0x29(%rbp),%eax
cmp    $0xa,%eax
je     11cb <test_format_data+0x7b>
movsbl -0x29(%rbp),%eax
cmp    $0x9,%eax
jne    12a5 <test_format_data+0x155>
cmpq   $0x3fe,-0x20(%rbp)
jb     11de <test_format_data+0x8e>
jmp    13bc <test_format_data+0x26c>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x5c,(%rax)
movsbl -0x29(%rbp),%eax
cmp    $0x22,%eax
jne    121e <test_format_data+0xce>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    1294 <test_format_data+0x144>
movsbl -0x29(%rbp),%eax
cmp    $0xd,%eax
jne    1242 <test_format_data+0xf2>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x72,(%rax)
jmp    128f <test_format_data+0x13f>
movsbl -0x29(%rbp),%eax
cmp    $0xa,%eax
jne    1266 <test_format_data+0x116>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x6e,(%rax)
jmp    128a <test_format_data+0x13a>
movsbl -0x29(%rbp),%eax
cmp    $0x9,%eax
jne    1285 <test_format_data+0x135>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x74,(%rax)
jmp    128a <test_format_data+0x13a>
jmp    128f <test_format_data+0x13f>
jmp    1294 <test_format_data+0x144>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
jmp    138d <test_format_data+0x23d>
movzbl -0x29(%rbp),%edi
call   1060 <isprint@plt>
cmp    $0x0,%rax
je     12f8 <test_format_data+0x1a8>
cmpq   $0x3ff,-0x20(%rbp)
jb     12cb <test_format_data+0x17b>
jmp    13bc <test_format_data+0x26c>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1388 <test_format_data+0x238>
cmpq   $0x3fc,-0x20(%rbp)
jb     130b <test_format_data+0x1bb>
jmp    13bc <test_format_data+0x26c>
lea    -0x2e(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    $0x5,%esi
lea    0xcdd(%rip),%rdx
movzbl (%rax,%rcx,1),%ecx
call   1040 <snprintf@plt>
mov    -0x2e(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x2d(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x2c(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x2b(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
jmp    138d <test_format_data+0x23d>
jmp    1392 <test_format_data+0x242>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    117b <test_format_data+0x2b>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
lea    0x2caf(%rip),%rdi
call   1070 <strdup@plt>
add    $0x30,%rsp
pop    %rbp
ret
mov    0x2c1d(%rip),%rax
mov    (%rax),%edi
lea    0xc3b(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>

x86 assembly (clang, -O2):
<test_format_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     128b <test_format_data+0x13b>
mov    %rsi,%r14
mov    %rdi,%r13
lea    0x2eef(%rip),%rbp
xor    %ebx,%ebx
movabs $0x400002600,%r12
lea    0xe7c(%rip),%rdi
xor    %r15d,%r15d
jmp    11a9 <test_format_data+0x59>
mov    $0x74,%cl
mov    %cl,0x1(%rbp)
add    $0x2,%rbp
mov    %rbp,%rax
add    $0x2,%r15
mov    %rax,%rbp
add    $0x1,%rbx
cmp    %rbx,%r14
je     1292 <test_format_data+0x142>
movzbl 0x0(%r13,%rbx,1),%ecx
cmp    $0x22,%rcx
ja     11f0 <test_format_data+0xa0>
bt     %rcx,%r12
jae    11f0 <test_format_data+0xa0>
cmp    $0x3fd,%r15
ja     12b1 <test_format_data+0x161>
lea    0x1(%rbp),%rax
movb   $0x5c,0x0(%rbp)
lea    -0x9(%rcx),%edx
cmp    $0x19,%dl
ja     1195 <test_format_data+0x45>
mov    $0x72,%cl
movzbl %dl,%edx
movslq (%rdi,%rdx,4),%rdx
add    %rdi,%rdx
jmp    *%rdx
mov    $0x6e,%cl
jmp    118b <test_format_data+0x3b>
nopw   0x0(%rax,%rax,1)
movzbl %cl,%edi
call   1060 <isprint@plt>
test   %rax,%rax
je     1232 <test_format_data+0xe2>
cmp    $0x3fe,%r15
lea    0xdf5(%rip),%rdi
ja     12b1 <test_format_data+0x161>
movzbl 0x0(%r13,%rbx,1),%eax
mov    %al,0x0(%rbp)
add    $0x1,%rbp
add    $0x1,%r15
jmp    119c <test_format_data+0x4c>
movzbl 0x0(%r13,%rbx,1),%ecx
jmp    118b <test_format_data+0x3b>
cmp    $0x3fc,%r15
jae    12b1 <test_format_data+0x161>
movzbl 0x0(%r13,%rbx,1),%ecx
lea    0x3(%rsp),%rdi
mov    $0x5,%esi
lea    0xe16(%rip),%rdx
call   1040 <snprintf@plt>
movzbl 0x3(%rsp),%eax
mov    %al,0x0(%rbp)
movzbl 0x4(%rsp),%eax
mov    %al,0x1(%rbp)
movzbl 0x5(%rsp),%eax
mov    %al,0x2(%rbp)
movzbl 0x6(%rsp),%eax
mov    %al,0x3(%rbp)
add    $0x4,%rbp
add    $0x4,%r15
lea    0xd7a(%rip),%rdi
jmp    119c <test_format_data+0x4c>
lea    0x2dce(%rip),%rbp
movb   $0x0,0x0(%rbp)
lea    0x2dc3(%rip),%rdi
call   1070 <strdup@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@test_format_data.buf = internal global [1024 x i8] zeroinitializer
@.str = [7 x i8] c"\\x%02x\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"data too large for buffer\0A\00"
define i8* @test_format_data(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8
%9 = alloca [5 x i8]
store i8* %0, i8** %3
store i64 %1, i64* %4
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @test_format_data.buf, i64 0, i64 0), i8** %5
store i64 0, i64* %6
store i64 0, i64* %7
br label %10
10: ; preds = %126, %2
%11 = load i64, i64* %7
%12 = load i64, i64* %4
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %129
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = load i64, i64* %7
%17 = getelementptr inbounds i8, i8* %15, i64 %16
%18 = load i8, i8* %17
store i8 %18, i8* %8
%19 = load i8, i8* %8
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 34
br i1 %21, label %34, label %22
22: ; preds = %14
%23 = load i8, i8* %8
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 13
br i1 %25, label %34, label %26
26: ; preds = %22
%27 = load i8, i8* %8
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 10
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i8, i8* %8
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 9
br i1 %33, label %34, label %78
34: ; preds = %30, %26, %22, %14
%35 = load i64, i64* %6
%36 = icmp uge i64 %35, 1022
br i1 %36, label %37, label %38
37: ; preds = %34
br label %132
38: ; preds = %34
%39 = load i8*, i8** %5
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %5
store i8 92, i8* %39
%41 = load i8, i8* %8
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 34
br i1 %43, label %44, label %51
44: ; preds = %38
%45 = load i8*, i8** %3
%46 = load i64, i64* %7
%47 = getelementptr inbounds i8, i8* %45, i64 %46
%48 = load i8, i8* %47
%49 = load i8*, i8** %5
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %5
store i8 %48, i8* %49
br label %75
51: ; preds = %38
%52 = load i8, i8* %8
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 13
br i1 %54, label %55, label %58
55: ; preds = %51
%56 = load i8*, i8** %5
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %5
store i8 114, i8* %56
br label %74
58: ; preds = %51
%59 = load i8, i8* %8
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %60, 10
br i1 %61, label %62, label %65
62: ; preds = %58
%63 = load i8*, i8** %5
%64 = getelementptr inbounds i8, i8* %63, i32 1
store i8* %64, i8** %5
store i8 110, i8* %63
br label %73
65: ; preds = %58
%66 = load i8, i8* %8
%67 = sext i8 %66 to i32
%68 = icmp eq i32 %67, 9
br i1 %68, label %69, label %72
69: ; preds = %65
%70 = load i8*, i8** %5
%71 = getelementptr inbounds i8, i8* %70, i32 1
store i8* %71, i8** %5
store i8 116, i8* %70
br label %72
72: ; preds = %69, %65
br label %73
73: ; preds = %72, %62
br label %74
74: ; preds = %73, %55
br label %75
75: ; preds = %74, %44
%76 = load i64, i64* %6
%77 = add i64 %76, 2
store i64 %77, i64* %6
br label %125
78: ; preds = %30
%79 = load i8, i8* %8
%80 = call i64 @isprint(i8 zeroext %79)
%81 = icmp ne i64 %80, 0
br i1 %81, label %82, label %95
82: ; preds = %78
%83 = load i64, i64* %6
%84 = icmp uge i64 %83, 1023
br i1 %84, label %85, label %86
85: ; preds = %82
br label %132
86: ; preds = %82
%87 = load i8*, i8** %3
%88 = load i64, i64* %7
%89 = getelementptr inbounds i8, i8* %87, i64 %88
%90 = load i8, i8* %89
%91 = load i8*, i8** %5
%92 = getelementptr inbounds i8, i8* %91, i32 1
store i8* %92, i8** %5
store i8 %90, i8* %91
%93 = load i64, i64* %6
%94 = add i64 %93, 1
store i64 %94, i64* %6
br label %124
95: ; preds = %78
%96 = load i64, i64* %6
%97 = icmp uge i64 %96, 1020
br i1 %97, label %98, label %99
98: ; preds = %95
br label %132
99: ; preds = %95
%100 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%101 = load i8*, i8** %3
%102 = load i64, i64* %7
%103 = getelementptr inbounds i8, i8* %101, i64 %102
%104 = load i8, i8* %103
%105 = call i32 @snprintf(i8* %100, i32 5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %104)
%106 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%107 = load i8, i8* %106
%108 = load i8*, i8** %5
%109 = getelementptr inbounds i8, i8* %108, i32 1
store i8* %109, i8** %5
store i8 %107, i8* %108
%110 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 1
%111 = load i8, i8* %110
%112 = load i8*, i8** %5
%113 = getelementptr inbounds i8, i8* %112, i32 1
store i8* %113, i8** %5
store i8 %111, i8* %112
%114 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 2
%115 = load i8, i8* %114
%116 = load i8*, i8** %5
%117 = getelementptr inbounds i8, i8* %116, i32 1
store i8* %117, i8** %5
store i8 %115, i8* %116
%118 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 3
%119 = load i8, i8* %118
%120 = load i8*, i8** %5
%121 = getelementptr inbounds i8, i8* %120, i32 1
store i8* %121, i8** %5
store i8 %119, i8* %120
%122 = load i64, i64* %6
%123 = add i64 %122, 4
store i64 %123, i64* %6
br label %124
124: ; preds = %99, %86
br label %125
125: ; preds = %124, %75
br label %126
126: ; preds = %125
%127 = load i64, i64* %7
%128 = add i64 %127, 1
store i64 %128, i64* %7
br label %10
129: ; preds = %10
%130 = load i8*, i8** %5
store i8 0, i8* %130
%131 = call i8* @strdup(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @test_format_data.buf, i64 0, i64 0))
ret i8* %131
132: ; preds = %98, %85, %37
%133 = load i32, i32* @stderr
%134 = call i32 @fprintf(i32 %133, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%135 = call i32 (...) @abort()
unreachable
}

LLVM IR (-O2):
@test_format_data.buf = internal global [1024 x i8] zeroinitializer
@.str = [7 x i8] c"\\x%02x\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"data too large for buffer\0A\00"
define noalias i8* @test_format_data(i8* nocapture %0, i64 %1) {
%3 = alloca [5 x i8]
%4 = icmp eq i64 %1, 0
br i1 %4, label %63, label %5
5: ; preds = %2
%6 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 1
%8 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 2
%9 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 3
br label %10
10: ; preds = %5, %54
%11 = phi i64 [ 0, %5 ], [ %57, %54 ]
%12 = phi i64 [ 0, %5 ], [ %56, %54 ]
%13 = phi i8* [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @test_format_data.buf, i64 0, i64 0), %5 ], [ %55, %54 ]
%14 = getelementptr inbounds i8, i8* %0, i64 %11
%15 = load i8, i8* %14
switch i8 %15, label %30 [
i8 34, label %16
i8 13, label %16
i8 10, label %16
i8 9, label %16
]
16: ; preds = %10, %10, %10, %10
%17 = icmp ugt i64 %12, 1021
br i1 %17, label %59, label %18
18: ; preds = %16
%19 = getelementptr inbounds i8, i8* %13, i64 1
store i8 92, i8* %13
switch i8 %15, label %27 [
i8 34, label %20
i8 13, label %24
i8 10, label %22
i8 9, label %23
]
20: ; preds = %18
%21 = load i8, i8* %14
br label %24
22: ; preds = %18
br label %24
23: ; preds = %18
br label %24
24: ; preds = %18, %20, %22, %23
%25 = phi i8 [ 116, %23 ], [ 110, %22 ], [ %21, %20 ], [ 114, %18 ]
%26 = getelementptr inbounds i8, i8* %13, i64 2
store i8 %25, i8* %19
br label %27
27: ; preds = %24, %18
%28 = phi i8* [ %19, %18 ], [ %26, %24 ]
%29 = add nuw nsw i64 %12, 2
br label %54
30: ; preds = %10
%31 = call i64 @isprint(i8 zeroext %15)
%32 = icmp eq i64 %31, 0
br i1 %32, label %39, label %33
33: ; preds = %30
%34 = icmp ugt i64 %12, 1022
br i1 %34, label %59, label %35
35: ; preds = %33
%36 = load i8, i8* %14
%37 = getelementptr inbounds i8, i8* %13, i64 1
store i8 %36, i8* %13
%38 = add nuw nsw i64 %12, 1
br label %54
39: ; preds = %30
%40 = icmp ugt i64 %12, 1019
br i1 %40, label %41, label %42
41: ; preds = %39
br label %59
42: ; preds = %39
%43 = load i8, i8* %14
%44 = call i32 @snprintf(i8* %6, i32 5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %43)
%45 = load i8, i8* %6
%46 = getelementptr inbounds i8, i8* %13, i64 1
store i8 %45, i8* %13
%47 = load i8, i8* %7
%48 = getelementptr inbounds i8, i8* %13, i64 2
store i8 %47, i8* %46
%49 = load i8, i8* %8
%50 = getelementptr inbounds i8, i8* %13, i64 3
store i8 %49, i8* %48
%51 = load i8, i8* %9
%52 = getelementptr inbounds i8, i8* %13, i64 4
store i8 %51, i8* %50
%53 = add nuw nsw i64 %12, 4
br label %54
54: ; preds = %42, %35, %27
%55 = phi i8* [ %52, %42 ], [ %37, %35 ], [ %28, %27 ]
%56 = phi i64 [ %53, %42 ], [ %38, %35 ], [ %29, %27 ]
%57 = add nuw i64 %11, 1
%58 = icmp eq i64 %57, %1
br i1 %58, label %63, label %10
59: ; preds = %16, %33, %41
%60 = load i32, i32* @stderr
%61 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%62 = call i32 (...) @abort()
unreachable
63: ; preds = %54, %2
%64 = phi i8* [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @test_format_data.buf, i64 0, i64 0), %2 ], [ %55, %54 ]
store i8 0, i8* %64
%65 = call i8* @strdup(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @test_format_data.buf, i64 0, i64 0))
ret i8* %65
}