#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 */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned short);
scalar_t__ malloc(unsigned short);
int /*<<< orphan*/ memcpy(char *, char const *, unsigned short);
int /*<<< orphan*/ read_word(char const *, unsigned short *);
int /*<<< orphan*/ stderr;
int read_stringa(const char *ptr, char **value) {
  ;
  unsigned short len = 0;
  ptr += read_word(ptr, &len);
  *value = (char *)malloc(len + 1);
  if (!*value) {
    fprintf(stderr,
            "ERROR: read_stringa: failed to allocate %d bytes of memory\n",
            len + 1);
    exit(-1);
  }
  (*value)[len] = '\0';
  if (len) {
    memcpy(*value, ptr, len);
    return len + sizeof(short);
  }
  return 4;
}


x86 assembly (clang, -O0):
<read_stringa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movw   $0x0,-0x1a(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1a(%rbp),%rsi
call   1060 <read_word@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movzwl -0x1a(%rbp),%eax
add    $0x1,%eax
movzwl %ax,%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11d3 <read_stringa+0x83>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
movzwl -0x1a(%rbp),%eax
add    $0x1,%eax
lea    0xe3f(%rip),%rsi
movzwl %ax,%edx
call   1030 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movzwl -0x1a(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
cmpw   $0x0,-0x1a(%rbp)
je     1214 <read_stringa+0xc4>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1a(%rbp),%ax
movzwl %ax,%edx
call   1040 <memcpy@plt>
movzwl -0x1a(%rbp),%eax
add    $0x2,%rax
mov    %eax,-0x4(%rbp)
jmp    121b <read_stringa+0xcb>
movl   $0x4,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_stringa>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
movw   $0x0,0xe(%rsp)
lea    0xe(%rsp),%rsi
call   1060 <read_word@plt>
mov    %eax,%ebp
movzwl 0xe(%rsp),%eax
add    $0x1,%eax
movzwl %ax,%edi
call   1050 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11c8 <read_stringa+0x78>
movzwl 0xe(%rsp),%ecx
movb   $0x0,(%rax,%rcx,1)
movzwl 0xe(%rsp),%eax
test   %ax,%ax
je     11ba <read_stringa+0x6a>
movslq %ebp,%rcx
add    %rcx,%rbx
mov    (%r14),%rdi
movzwl %ax,%edx
mov    %rbx,%rsi
call   1040 <memcpy@plt>
movzwl 0xe(%rsp),%eax
add    $0x2,%eax
jmp    11bf <read_stringa+0x6f>
mov    $0x4,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
movzwl 0xe(%rsp),%eax
add    $0x1,%eax
movzwl %ax,%edx
lea    0xe1d(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [60 x i8] c"ERROR: read_stringa: failed to allocate %d bytes of memory\0A\00"
define i32 @read_stringa(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i16
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i16 0, i16* %6
%7 = load i8*, i8** %4
%8 = call i32 @read_word(i8* %7, i16* %6)
%9 = load i8*, i8** %4
%10 = sext i32 %8 to i64
%11 = getelementptr inbounds i8, i8* %9, i64 %10
store i8* %11, i8** %4
%12 = load i16, i16* %6
%13 = zext i16 %12 to i32
%14 = add nsw i32 %13, 1
%15 = trunc i32 %14 to i16
%16 = call i64 @malloc(i16 zeroext %15)
%17 = inttoptr i64 %16 to i8*
%18 = load i8**, i8*** %5
store i8* %17, i8** %18
%19 = load i8**, i8*** %5
%20 = load i8*, i8** %19
%21 = icmp ne i8* %20, null
br i1 %21, label %30, label %22
22: ; preds = %2
%23 = load i32, i32* @stderr
%24 = load i16, i16* %6
%25 = zext i16 %24 to i32
%26 = add nsw i32 %25, 1
%27 = trunc i32 %26 to i16
%28 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i16 zeroext %27)
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %2
%31 = load i8**, i8*** %5
%32 = load i8*, i8** %31
%33 = load i16, i16* %6
%34 = zext i16 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 0, i8* %35
%36 = load i16, i16* %6
%37 = icmp ne i16 %36, 0
br i1 %37, label %38, label %48
38: ; preds = %30
%39 = load i8**, i8*** %5
%40 = load i8*, i8** %39
%41 = load i8*, i8** %4
%42 = load i16, i16* %6
%43 = call i32 @memcpy(i8* %40, i8* %41, i16 zeroext %42)
%44 = load i16, i16* %6
%45 = zext i16 %44 to i64
%46 = add i64 %45, 2
%47 = trunc i64 %46 to i32
store i32 %47, i32* %3
br label %49
48: ; preds = %30
store i32 4, i32* %3
br label %49
49: ; preds = %48, %38
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [60 x i8] c"ERROR: read_stringa: failed to allocate %d bytes of memory\0A\00"
define i32 @read_stringa(i8* %0, i8** nocapture %1) {
%3 = alloca i16
%4 = bitcast i16* %3 to i8*
store i16 0, i16* %3
%5 = call i32 @read_word(i8* %0, i16* %3)
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = load i16, i16* %3
%9 = add i16 %8, 1
%10 = call i64 @malloc(i16 zeroext %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %1
%12 = icmp eq i64 %10, 0
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i16, i16* %3
%16 = add i16 %15, 1
%17 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i16 zeroext %16)
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %2
%20 = load i16, i16* %3
%21 = zext i16 %20 to i64
%22 = getelementptr inbounds i8, i8* %11, i64 %21
store i8 0, i8* %22
%23 = load i16, i16* %3
%24 = icmp eq i16 %23, 0
br i1 %24, label %31, label %25
25: ; preds = %19
%26 = load i8*, i8** %1
%27 = call i32 @memcpy(i8* %26, i8* %7, i16 zeroext %23)
%28 = load i16, i16* %3
%29 = zext i16 %28 to i32
%30 = add nuw nsw i32 %29, 2
br label %31
31: ; preds = %19, %25
%32 = phi i32 [ %30, %25 ], [ 4, %19 ]
ret i32 %32
}