#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 *malloc(unsigned int);
int /*<<< orphan*/ memcpy(char *, char *, unsigned int);
int parse_read(char **buf, char **readbuf, unsigned int len) {
  *readbuf = NULL;
  if (len) {
    *readbuf = malloc(len + 1);
    if (!*readbuf)
      return -1;
    memcpy(*readbuf, *buf, len);
    (*readbuf)[len] = 0;
    *buf += len;
  }
  return len;
}


x86 assembly (clang, -O0):
<parse_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
cmpl   $0x0,-0x1c(%rbp)
je     11ad <parse_read+0x8d>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1177 <parse_read+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b3 <parse_read+0x93>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x1c(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%edx
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
mov    %edx,%edx
add    %rdx,%rcx
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_read>:
push   %r15
push   %r14
push   %rbx
movq   $0x0,(%rsi)
test   %edx,%edx
je     1163 <parse_read+0x43>
mov    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x1(%rbx),%edi
call   1040 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     1167 <parse_read+0x47>
mov    (%r14),%rsi
mov    %rax,%rdi
mov    %ebx,%edx
call   1030 <memcpy@plt>
mov    (%r15),%rax
mov    %ebx,%ecx
movb   $0x0,(%rax,%rcx,1)
add    %rcx,(%r14)
jmp    116c <parse_read+0x4c>
xor    %ebx,%ebx
jmp    116c <parse_read+0x4c>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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