#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 */
unsigned int d_getub(unsigned char const *);
int /*<<< orphan*/ memcpy(char *, unsigned char const *, unsigned int);
void d_fetchstr(const unsigned char **ptr, char *dest, unsigned size) {
  unsigned len;
  len = d_getub(*ptr);
  if (len > 0 && len < size)
    memcpy(dest, *ptr + 1, len);
  else
    len = 0;
  dest[len] = 0;
  *ptr += size;
}


x86 assembly (clang, -O0):
<d_fetchstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <d_getub@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jbe    1174 <d_fetchstr+0x54>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1174 <d_fetchstr+0x54>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
add    $0x1,%rsi
mov    -0x18(%rbp),%edx
call   1030 <memcpy@plt>
jmp    117b <d_fetchstr+0x5b>
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
mov    %edx,%edx
add    %rdx,%rcx
mov    %rcx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<d_fetchstr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%rdi
call   1040 <d_getub@plt>
mov    %eax,%ebp
xor    %eax,%eax
test   %ebp,%ebp
je     1158 <d_fetchstr+0x38>
cmp    %r15d,%ebp
jae    1158 <d_fetchstr+0x38>
mov    (%rbx),%rsi
add    $0x1,%rsi
mov    %r14,%rdi
mov    %ebp,%edx
call   1030 <memcpy@plt>
mov    %ebp,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r15d,%eax
add    %rax,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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