#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 */
char *calloc(size_t, int);
char *escape_hex(char *, size_t);
int /*<<< orphan*/ free(char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *str_to_c(char *bytes, size_t szBytes) {
  char *encoded, *buffer;
  size_t szEncoded;
  szEncoded = strlen("char payload[] = \"\"") + szBytes * 4 + 1;
  encoded = calloc(szEncoded, sizeof(*encoded));
  if (!encoded)
    return NULL;
  strncpy(encoded, "char payload[] = \"", szEncoded);
  buffer = escape_hex(bytes, szBytes);
  if (!buffer) {
    free(encoded);
    return NULL;
  }
  strncat(encoded, buffer, szEncoded - strlen(encoded));
  strncat(encoded, "\"", szEncoded - strlen(encoded));
  encoded[szEncoded - 1] = '\0';
  free(buffer);
  return encoded;
}


x86 assembly (clang, -O0):
<str_to_c>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe89(%rip),%rdi
call   1060 <strlen@plt>
cltq
mov    -0x18(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bb <str_to_c+0x5b>
movq   $0x0,-0x8(%rbp)
jmp    1295 <str_to_c+0x135>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    0xe4a(%rip),%rsi
call   1040 <strncpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <escape_hex@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1201 <str_to_c+0xa1>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1295 <str_to_c+0x135>
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1070 <strncat@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
sub    %rcx,%rax
mov    %eax,%edx
lea    0xdb6(%rip),%rsi
call   1070 <strncat@plt>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_to_c>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r12
mov    %rdi,%r15
lea    0xe8a(%rip),%rdi
call   1060 <strlen@plt>
movslq %eax,%r13
lea    0x1(,%r12,4),%r14
add    %r13,%r14
mov    %r14,%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
test   %rax,%rax
je     120a <str_to_c+0xaa>
mov    %rax,%rbx
lea    0xe6f(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1040 <strncpy@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <escape_hex@plt>
test   %rax,%rax
je     120e <str_to_c+0xae>
mov    %rax,%r15
lea    0x0(,%r12,4),%r12
add    %r13,%r12
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r14d,%edx
sub    %eax,%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <strncat@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
sub    %eax,%r14d
lea    0xe2d(%rip),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
call   1070 <strncat@plt>
movb   $0x0,(%rbx,%r12,1)
jmp    1213 <str_to_c+0xb3>
xor    %ebx,%ebx
jmp    121b <str_to_c+0xbb>
mov    %rbx,%r15
xor    %ebx,%ebx
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [20 x i8] c"char payload[] = \22\22\00"
@.str.1 = [19 x i8] c"char payload[] = \22\00"
@.str.2 = [2 x i8] c"\22\00"
define i8* @str_to_c(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%9 = call i32 @strlen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%10 = sext i32 %9 to i64
%11 = load i64, i64* %5
%12 = mul i64 %11, 4
%13 = add i64 %10, %12
%14 = add i64 %13, 1
store i64 %14, i64* %8
%15 = load i64, i64* %8
%16 = call i8* @calloc(i64 %15, i32 1)
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = icmp ne i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %2
store i8* null, i8** %3
br label %57
20: ; preds = %2
%21 = load i8*, i8** %6
%22 = load i64, i64* %8
%23 = call i32 @strncpy(i8* %21, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i64 %22)
%24 = load i8*, i8** %4
%25 = load i64, i64* %5
%26 = call i8* @escape_hex(i8* %24, i64 %25)
store i8* %26, i8** %7
%27 = load i8*, i8** %7
%28 = icmp ne i8* %27, null
br i1 %28, label %32, label %29
29: ; preds = %20
%30 = load i8*, i8** %6
%31 = call i32 @free(i8* %30)
store i8* null, i8** %3
br label %57
32: ; preds = %20
%33 = load i8*, i8** %6
%34 = load i8*, i8** %7
%35 = load i64, i64* %8
%36 = load i8*, i8** %6
%37 = call i32 @strlen(i8* %36)
%38 = sext i32 %37 to i64
%39 = sub i64 %35, %38
%40 = trunc i64 %39 to i32
%41 = call i32 @strncat(i8* %33, i8* %34, i32 %40)
%42 = load i8*, i8** %6
%43 = load i64, i64* %8
%44 = load i8*, i8** %6
%45 = call i32 @strlen(i8* %44)
%46 = sext i32 %45 to i64
%47 = sub i64 %43, %46
%48 = trunc i64 %47 to i32
%49 = call i32 @strncat(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %48)
%50 = load i8*, i8** %6
%51 = load i64, i64* %8
%52 = sub i64 %51, 1
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8 0, i8* %53
%54 = load i8*, i8** %7
%55 = call i32 @free(i8* %54)
%56 = load i8*, i8** %6
store i8* %56, i8** %3
br label %57
57: ; preds = %32, %29, %19
%58 = load i8*, i8** %3
ret i8* %58
}

LLVM IR (-O2):
@.str = [20 x i8] c"char payload[] = \22\22\00"
@.str.1 = [19 x i8] c"char payload[] = \22\00"
@.str.2 = [2 x i8] c"\22\00"
define i8* @str_to_c(i8* %0, i64 %1) {
%3 = tail call i32 @strlen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%4 = sext i32 %3 to i64
%5 = shl i64 %1, 2
%6 = add i64 %5, %4
%7 = add i64 %6, 1
%8 = tail call i8* @calloc(i64 %7, i32 1)
%9 = icmp eq i8* %8, null
br i1 %9, label %27, label %10
10: ; preds = %2
%11 = tail call i32 @strncpy(i8* %8, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i64 %7)
%12 = tail call i8* @escape_hex(i8* %0, i64 %1)
%13 = icmp eq i8* %12, null
br i1 %13, label %23, label %14
14: ; preds = %10
%15 = tail call i32 @strlen(i8* %8)
%16 = trunc i64 %7 to i32
%17 = sub i32 %16, %15
%18 = tail call i32 @strncat(i8* %8, i8* %12, i32 %17)
%19 = tail call i32 @strlen(i8* %8)
%20 = sub i32 %16, %19
%21 = tail call i32 @strncat(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds i8, i8* %8, i64 %6
store i8 0, i8* %22
br label %23
23: ; preds = %10, %14
%24 = phi i8* [ %12, %14 ], [ %8, %10 ]
%25 = phi i8* [ %8, %14 ], [ null, %10 ]
%26 = tail call i32 @free(i8* %24)
br label %27
27: ; preds = %23, %2
%28 = phi i8* [ null, %2 ], [ %25, %23 ]
ret i8* %28
}