#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(scalar_t__, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char *str_to_nasm(char *bytes, size_t szBytes) {
  char *buffer, *backup;
  buffer = calloc(strlen("payload db ") + szBytes * 5 + 2, sizeof(*buffer));
  backup = buffer;
  strcpy(buffer, "payload db ");
  buffer += strlen("payload db ");
  while (szBytes) {
    snprintf(buffer, 6, "0x%02x,", *bytes & 0xff);
    buffer += 5;
    bytes++;
    szBytes--;
  }
  strcat(buffer, "0");
  return backup;
}


x86 assembly (clang, -O0):
<str_to_nasm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe99(%rip),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
imul   $0x5,-0x10(%rbp),%rax
add    %rax,%rdi
add    $0x2,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe64(%rip),%rsi
call   1030 <strcpy@plt>
lea    0xe58(%rip),%rdi
call   1040 <strlen@plt>
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     120d <str_to_nasm+0xbd>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
and    $0xff,%eax
mov    $0x6,%esi
lea    0xe30(%rip),%rdx
movsbl %al,%ecx
call   1050 <snprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    11b5 <str_to_nasm+0x65>
mov    -0x18(%rbp),%rdi
lea    0xdfc(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_to_nasm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r15
lea    (%rsi,%rsi,4),%rdi
add    $0xd,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r14
lea    0xe93(%rip),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rbx
add    $0xb,%rbx
test   %r12,%r12
je     11b2 <str_to_nasm+0x72>
xor    %ebp,%ebp
lea    0xe82(%rip),%r13
nopw   0x0(%rax,%rax,1)
movsbl (%r15,%rbp,1),%ecx
mov    %rbx,%rdi
mov    $0x6,%esi
mov    %r13,%rdx
call   1040 <snprintf@plt>
add    $0x5,%rbx
add    $0x1,%rbp
cmp    %rbp,%r12
jne    1190 <str_to_nasm+0x50>
lea    0xe5b(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcat@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"payload db \00"
@.str.1 = [8 x i8] c"0x%02x,\00"
@.str.2 = [2 x i8] c"0\00"
define i8* @str_to_nasm(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = call i64 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = load i64, i64* %4
%9 = mul i64 %8, 5
%10 = add i64 %7, %9
%11 = add i64 %10, 2
%12 = call i8* @calloc(i64 %11, i32 1)
store i8* %12, i8** %5
%13 = load i8*, i8** %5
store i8* %13, i8** %6
%14 = load i8*, i8** %5
%15 = call i32 @strcpy(i8* %14, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%16 = call i64 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%17 = load i8*, i8** %5
%18 = getelementptr inbounds i8, i8* %17, i64 %16
store i8* %18, i8** %5
br label %19
19: ; preds = %22, %2
%20 = load i64, i64* %4
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %36
22: ; preds = %19
%23 = load i8*, i8** %5
%24 = load i8*, i8** %3
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = and i32 %26, 255
%28 = trunc i32 %27 to i8
%29 = call i32 @snprintf(i8* %23, i32 6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8 signext %28)
%30 = load i8*, i8** %5
%31 = getelementptr inbounds i8, i8* %30, i64 5
store i8* %31, i8** %5
%32 = load i8*, i8** %3
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %3
%34 = load i64, i64* %4
%35 = add i64 %34, -1
store i64 %35, i64* %4
br label %19
36: ; preds = %19
%37 = load i8*, i8** %5
%38 = call i32 @strcat(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%39 = load i8*, i8** %6
ret i8* %39
}

LLVM IR (-O2):
@.str = [12 x i8] c"payload db \00"
@.str.1 = [8 x i8] c"0x%02x,\00"
@.str.2 = [2 x i8] c"0\00"
define i8* @str_to_nasm(i8* nocapture %0, i64 %1) {
%3 = mul i64 %1, 5
%4 = add i64 %3, 13
%5 = tail call i8* @calloc(i64 %4, i32 1)
%6 = tail call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds i8, i8* %5, i64 11
%8 = icmp eq i64 %1, 0
br i1 %8, label %19, label %9
9: ; preds = %2, %9
%10 = phi i8* [ %16, %9 ], [ %0, %2 ]
%11 = phi i8* [ %15, %9 ], [ %7, %2 ]
%12 = phi i64 [ %17, %9 ], [ %1, %2 ]
%13 = load i8, i8* %10
%14 = tail call i32 @snprintf(i8* %11, i32 6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8 signext %13)
%15 = getelementptr inbounds i8, i8* %11, i64 5
%16 = getelementptr inbounds i8, i8* %10, i64 1
%17 = add i64 %12, -1
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %9
19: ; preds = %9, %2
%20 = phi i8* [ %7, %2 ], [ %15, %9 ]
%21 = tail call i32 @strcat(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
ret i8* %5
}