#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 scalar_t__ uint8_t;
typedef int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ append_char(char **, size_t *, size_t *, char);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ init_char(char **, size_t *, size_t *);
scalar_t__ read8(int, int, scalar_t__ *);
int /*<<< orphan*/ stderr;
char *readcstr(int dbfd, uint32_t addr) {
  char *buf;
  size_t l, i;
  init_char(&buf, &l, &i);
  uint8_t b;
  do {
    if (read8(dbfd, addr, &b)) {
      fprintf(stderr, "persist: readcstr: failed to read byte at 0x%08x\n",
              addr);
      free(buf);
      return (NULL);
    }
    append_char(&buf, &l, &i, (char)b);
    addr++;
  } while (b);
  return (buf);
}


x86 assembly (clang, -O0):
<readcstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1040 <init_char@plt>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
lea    -0x30(%rbp),%rdx
call   1050 <read8@plt>
cmp    $0x0,%rax
je     11b6 <readcstr+0x66>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%edx
lea    0xe65(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ea <readcstr+0x9a>
mov    -0x30(%rbp),%rax
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
movsbl %al,%ecx
call   1070 <append_char@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    116f <readcstr+0x1f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readcstr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
lea    0x10(%rsp),%r14
lea    0x20(%rsp),%r15
lea    0x18(%rsp),%r12
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <init_char@plt>
lea    0x8(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %ebx,%esi
mov    %r13,%rdx
call   1050 <read8@plt>
test   %rax,%rax
jne    11c6 <readcstr+0x76>
movsbl 0x8(%rsp),%ecx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1070 <append_char@plt>
add    $0x1,%ebx
cmpq   $0x0,0x8(%rsp)
jne    1190 <readcstr+0x40>
mov    0x10(%rsp),%rax
jmp    11e9 <readcstr+0x99>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
mov    %ebx,%edx
call   1060 <fprintf@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [50 x i8] c"persist: readcstr: failed to read byte at 0x%08x\0A\00"
define i8* @readcstr(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = call i32 @init_char(i8** %6, i64* %7, i64* %8)
br label %11
11: ; preds = %28, %2
%12 = load i32, i32* %4
%13 = load i32, i32* %5
%14 = call i64 @read8(i32 %12, i32 %13, i64* %9)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = load i32, i32* %5
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = load i8*, i8** %6
%21 = call i32 @free(i8* %20)
store i8* null, i8** %3
br label %33
22: ; preds = %11
%23 = load i64, i64* %9
%24 = trunc i64 %23 to i8
%25 = call i32 @append_char(i8** %6, i64* %7, i64* %8, i8 signext %24)
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
store i32 %27, i32* %5
br label %28
28: ; preds = %22
%29 = load i64, i64* %9
%30 = icmp ne i64 %29, 0
br i1 %30, label %11, label %31
31: ; preds = %28
%32 = load i8*, i8** %6
store i8* %32, i8** %3
br label %33
33: ; preds = %31, %16
%34 = load i8*, i8** %3
ret i8* %34
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [50 x i8] c"persist: readcstr: failed to read byte at 0x%08x\0A\00"
define i8* @readcstr(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = bitcast i8** %3 to i8*
%8 = bitcast i64* %4 to i8*
%9 = bitcast i64* %5 to i8*
%10 = call i32 @init_char(i8** %3, i64* %4, i64* %5)
%11 = bitcast i64* %6 to i8*
br label %12
12: ; preds = %21, %2
%13 = phi i32 [ %1, %2 ], [ %25, %21 ]
%14 = call i64 @read8(i32 %0, i32 %13, i64* %6)
%15 = icmp eq i64 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %13)
%19 = load i8*, i8** %3
%20 = call i32 @free(i8* %19)
br label %30
21: ; preds = %12
%22 = load i64, i64* %6
%23 = trunc i64 %22 to i8
%24 = call i32 @append_char(i8** %3, i64* %4, i64* %5, i8 signext %23)
%25 = add nsw i32 %13, 1
%26 = load i64, i64* %6
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %12
28: ; preds = %21
%29 = load i8*, i8** %3
br label %30
30: ; preds = %28, %16
%31 = phi i8* [ null, %16 ], [ %29, %28 ]
ret i8* %31
}