#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_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ msg(int, char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *xmalloc(size_t);
char *read_binary_string(char **from, size_t len, const char *max) {
  char *result;
  if (*from + len > max) {
    msg(-3, "Attempt to read beyond end of file, corrupt file?\n");
    exit(EXIT_FAILURE);
  }
  result = xmalloc(len);
  strncpy(result, *from, len);
  *from += len;
  return result;
}


x86 assembly (clang, -O0):
<read_binary_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
add    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    1188 <read_binary_string+0x48>
mov    $0xfffffffd,%edi
lea    0xe8b(%rip),%rsi
call   1060 <msg@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1040 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_binary_string>:
push   %r15
push   %r14
push   %rbx
mov    (%rdi),%rax
add    %rsi,%rax
cmp    %rdx,%rax
ja     117b <read_binary_string+0x3b>
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <xmalloc@plt>
mov    %rax,%r15
mov    (%r14),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
add    %rbx,(%r14)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe7e(%rip),%rsi
mov    $0xfffffffd,%edi
call   1060 <msg@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [51 x i8] c"Attempt to read beyond end of file, corrupt file?\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @read_binary_string(i8** %0, i64 %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
store i8** %0, i8*** %4
store i64 %1, i64* %5
store i8* %2, i8** %6
%8 = load i8**, i8*** %4
%9 = load i8*, i8** %8
%10 = load i64, i64* %5
%11 = getelementptr inbounds i8, i8* %9, i64 %10
%12 = load i8*, i8** %6
%13 = icmp ugt i8* %11, %12
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = call i32 @msg(i32 -3, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %3
%19 = load i64, i64* %5
%20 = call i8* @xmalloc(i64 %19)
store i8* %20, i8** %7
%21 = load i8*, i8** %7
%22 = load i8**, i8*** %4
%23 = load i8*, i8** %22
%24 = load i64, i64* %5
%25 = call i32 @strncpy(i8* %21, i8* %23, i64 %24)
%26 = load i64, i64* %5
%27 = load i8**, i8*** %4
%28 = load i8*, i8** %27
%29 = getelementptr inbounds i8, i8* %28, i64 %26
store i8* %29, i8** %27
%30 = load i8*, i8** %7
ret i8* %30
}

LLVM IR (-O2):
@.str = [51 x i8] c"Attempt to read beyond end of file, corrupt file?\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @read_binary_string(i8** nocapture %0, i64 %1, i8* readnone %2) {
%4 = load i8*, i8** %0
%5 = getelementptr inbounds i8, i8* %4, i64 %1
%6 = icmp ugt i8* %5, %2
br i1 %6, label %7, label %11
7: ; preds = %3
%8 = tail call i32 @msg(i32 -3, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %3
%12 = tail call i8* @xmalloc(i64 %1)
%13 = load i8*, i8** %0
%14 = tail call i32 @strncpy(i8* %12, i8* %13, i64 %1)
%15 = load i8*, i8** %0
%16 = getelementptr inbounds i8, i8* %15, i64 %1
store i8* %16, i8** %0
ret i8* %12
}