#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 uint32_t;
typedef int /*<<< orphan*/ len;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *malloc(int);
int ntohl(int);
char *unmarshal_string(int (*getchar_func)(void *ctx, size_t count, void *c),
                       void *ctx) {
  uint32_t len;
  char *string;
  if (getchar_func(ctx, sizeof(len), &len)) {
    return NULL;
  }
  len = ntohl(len);
  string = malloc(len + 1);
  if (getchar_func(ctx, len, string)) {
    free(string);
    return NULL;
  }
  string[len] = 0;
  return string;
}


x86 assembly (clang, -O0):
<unmarshal_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rdx
mov    $0x4,%esi
call   *%rax
cmp    $0x0,%eax
je     1169 <unmarshal_string+0x39>
movq   $0x0,-0x8(%rbp)
jmp    11c8 <unmarshal_string+0x98>
mov    -0x1c(%rbp),%edi
call   1050 <ntohl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   *%rax
cmp    $0x0,%eax
je     11b4 <unmarshal_string+0x84>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c8 <unmarshal_string+0x98>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unmarshal_string>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x4(%rsp),%rdx
mov    $0x4,%esi
mov    %r15,%rdi
call   *%r14
test   %eax,%eax
je     1156 <unmarshal_string+0x26>
xor    %ebx,%ebx
jmp    1198 <unmarshal_string+0x68>
mov    0x4(%rsp),%edi
call   1050 <ntohl@plt>
mov    %eax,%r12d
mov    %eax,0x4(%rsp)
lea    0x1(%r12),%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
movslq %r12d,%rsi
mov    %r15,%rdi
mov    %rax,%rdx
call   *%r14
test   %eax,%eax
je     118f <unmarshal_string+0x5f>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
jmp    1198 <unmarshal_string+0x68>
movslq 0x4(%rsp),%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @unmarshal_string(i32 (i8*, i64, i8*)* nocapture %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 %0(i8* %1, i64 4, i8* %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %21
7: ; preds = %2
%8 = load i32, i32* %3
%9 = call i32 @ntohl(i32 %8)
store i32 %9, i32* %3
%10 = add nsw i32 %9, 1
%11 = call i8* @malloc(i32 %10)
%12 = sext i32 %9 to i64
%13 = call i32 %0(i8* %1, i64 %12, i8* %11)
%14 = icmp eq i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %7
%16 = call i32 @free(i8* %11)
br label %21
17: ; preds = %7
%18 = load i32, i32* %3
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %11, i64 %19
store i8 0, i8* %20
br label %21
21: ; preds = %2, %17, %15
%22 = phi i8* [ null, %15 ], [ %11, %17 ], [ null, %2 ]
ret i8* %22
}