#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*/ uint8_t;

/* Variables and functions */
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/ *, size_t);
int _parse_string(uint8_t *data, size_t len, char **out) {
  char *buffer;
  if (!len)
    return 2;
  buffer = malloc(len + 1);
  if (!buffer)
    return 1;
  memcpy(buffer, data, len);
  buffer[len] = '\0';
  *out = buffer;
  return 0;
}


x86 assembly (clang, -O0):
<_parse_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    114b <_parse_string+0x2b>
movl   $0x2,-0x4(%rbp)
jmp    11a2 <_parse_string+0x82>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1173 <_parse_string+0x53>
movl   $0x1,-0x4(%rbp)
jmp    11a2 <_parse_string+0x82>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_parse_string>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     1161 <_parse_string+0x41>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x1(%rsi),%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     1168 <_parse_string+0x48>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <memcpy@plt>
movb   $0x0,(%r12,%rbx,1)
mov    %r12,(%r14)
xor    %eax,%eax
jmp    116d <_parse_string+0x4d>
mov    $0x2,%eax
jmp    116d <_parse_string+0x4d>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @_parse_string(i32* %0, i64 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i8**
%8 = alloca i8*
store i32* %0, i32** %5
store i64 %1, i64* %6
store i8** %2, i8*** %7
%9 = load i64, i64* %6
%10 = icmp ne i64 %9, 0
br i1 %10, label %12, label %11
11: ; preds = %3
store i32 2, i32* %4
br label %29
12: ; preds = %3
%13 = load i64, i64* %6
%14 = add i64 %13, 1
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %8
%16 = load i8*, i8** %8
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %12
store i32 1, i32* %4
br label %29
19: ; preds = %12
%20 = load i8*, i8** %8
%21 = load i32*, i32** %5
%22 = load i64, i64* %6
%23 = call i32 @memcpy(i8* %20, i32* %21, i64 %22)
%24 = load i8*, i8** %8
%25 = load i64, i64* %6
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8 0, i8* %26
%27 = load i8*, i8** %8
%28 = load i8**, i8*** %7
store i8* %27, i8** %28
store i32 0, i32* %4
br label %29
29: ; preds = %19, %18, %11
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
define i32 @_parse_string(i32* %0, i64 %1, i8** nocapture writeonly %2) {
%4 = icmp eq i64 %1, 0
br i1 %4, label %12, label %5
5: ; preds = %3
%6 = add i64 %1, 1
%7 = tail call i8* @malloc(i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %5
%10 = tail call i32 @memcpy(i8* %7, i32* %0, i64 %1)
%11 = getelementptr inbounds i8, i8* %7, i64 %1
store i8 0, i8* %11
store i8* %7, i8** %2
br label %12
12: ; preds = %5, %3, %9
%13 = phi i32 [ 0, %9 ], [ 2, %3 ], [ 1, %5 ]
ret i32 %13
}