#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int get_byte(int /*<<< orphan*/ *);
char *malloc(int);
char *get_bcpl_string(FILE *fp, int max_len) {
  char *output;
  size_t result;
  int len = get_byte(fp);
  if (len < 0 || len > max_len - 1)
    return NULL;
  output = malloc(len + 1);
  if (!output)
    return NULL;
  result = fread(output, 1, len, fp);
  if (result != (size_t)len) {
    free(output);
    return NULL;
  }
  output[len] = 0;
  fseek(fp, max_len - 1 - len, SEEK_CUR);
  return output;
}


x86 assembly (clang, -O0):
<get_bcpl_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <get_byte@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     1186 <get_bcpl_string+0x36>
mov    -0x2c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    1193 <get_bcpl_string+0x43>
movq   $0x0,-0x8(%rbp)
jmp    1229 <get_bcpl_string+0xd9>
mov    -0x2c(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ba <get_bcpl_string+0x6a>
movq   $0x0,-0x8(%rbp)
jmp    1229 <get_bcpl_string+0xd9>
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cmp    %rcx,%rax
je     11fa <get_bcpl_string+0xaa>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1229 <get_bcpl_string+0xd9>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
sub    $0x1,%esi
sub    -0x2c(%rbp),%esi
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_bcpl_string>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
call   1050 <get_byte@plt>
xor    %ebx,%ebx
test   %eax,%eax
js     11c0 <get_bcpl_string+0x70>
mov    %eax,%ebp
cmp    %r15d,%eax
jge    11c0 <get_bcpl_string+0x70>
lea    0x1(%rbp),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11be <get_bcpl_string+0x6e>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
mov    %ebp,%ecx
cmp    %rcx,%rax
jne    11b6 <get_bcpl_string+0x66>
movb   $0x0,(%rbx,%rax,1)
not    %ebp
add    %r15d,%ebp
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %ebp,%esi
call   1070 <fseek@plt>
jmp    11c0 <get_bcpl_string+0x70>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_CUR = global i32 0
define i8* @get_bcpl_string(i32* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%9 = load i32*, i32** %4
%10 = call i32 @get_byte(i32* %9)
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = icmp slt i32 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %2
%14 = load i32, i32* %8
%15 = load i32, i32* %5
%16 = sub nsw i32 %15, 1
%17 = icmp sgt i32 %14, %16
br i1 %17, label %18, label %19
18: ; preds = %13, %2
store i8* null, i8** %3
br label %51
19: ; preds = %13
%20 = load i32, i32* %8
%21 = add nsw i32 %20, 1
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = icmp ne i8* %23, null
br i1 %24, label %26, label %25
25: ; preds = %19
store i8* null, i8** %3
br label %51
26: ; preds = %19
%27 = load i8*, i8** %6
%28 = load i32, i32* %8
%29 = load i32*, i32** %4
%30 = call i64 @fread(i8* %27, i32 1, i32 %28, i32* %29)
store i64 %30, i64* %7
%31 = load i64, i64* %7
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = icmp ne i64 %31, %33
br i1 %34, label %35, label %38
35: ; preds = %26
%36 = load i8*, i8** %6
%37 = call i32 @free(i8* %36)
store i8* null, i8** %3
br label %51
38: ; preds = %26
%39 = load i8*, i8** %6
%40 = load i32, i32* %8
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 0, i8* %42
%43 = load i32*, i32** %4
%44 = load i32, i32* %5
%45 = sub nsw i32 %44, 1
%46 = load i32, i32* %8
%47 = sub nsw i32 %45, %46
%48 = load i32, i32* @SEEK_CUR
%49 = call i32 @fseek(i32* %43, i32 %47, i32 %48)
%50 = load i8*, i8** %6
store i8* %50, i8** %3
br label %51
51: ; preds = %38, %35, %25, %18
%52 = load i8*, i8** %3
ret i8* %52
}

LLVM IR (-O2):
@SEEK_CUR = global i32 0
define noalias i8* @get_bcpl_string(i32* %0, i32 %1) {
%3 = tail call i32 @get_byte(i32* %0)
%4 = icmp sgt i32 %3, -1
%5 = icmp slt i32 %3, %1
%6 = and i1 %4, %5
br i1 %6, label %7, label %23
7: ; preds = %2
%8 = add nuw nsw i32 %3, 1
%9 = tail call i8* @malloc(i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %23, label %11
11: ; preds = %7
%12 = tail call i64 @fread(i8* %9, i32 1, i32 %3, i32* %0)
%13 = zext i32 %3 to i64
%14 = icmp eq i64 %12, %13
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = tail call i32 @free(i8* %9)
br label %23
17: ; preds = %11
%18 = getelementptr inbounds i8, i8* %9, i64 %12
store i8 0, i8* %18
%19 = xor i32 %3, -1
%20 = add i32 %19, %1
%21 = load i32, i32* @SEEK_CUR
%22 = tail call i32 @fseek(i32* %0, i32 %20, i32 %21)
br label %23
23: ; preds = %7, %2, %17, %15
%24 = phi i8* [ null, %15 ], [ %9, %17 ], [ null, %2 ], [ null, %7 ]
ret i8* %24
}