#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*/ fprintf(int /*<<< orphan*/, char *, size_t);
int /*<<< orphan*/ stderr;
char *readLineFromBuffer(char *buffer, size_t *buffersize, char *line,
                         size_t len) {
  int ichar;
  size_t ipos = 0;
  while (*buffersize) {
    ichar = *buffer;
    (*buffersize)--;
    buffer++;
    if (ichar == '\r')
      break;
    if (ichar == '\n')
      break;
    line[ipos++] = ichar;
    if (ipos >= len) {
      fprintf(stderr,
              "readLineFromBuffer: end of line not found (maxlen = %ld)!\n",
              len);
      break;
    }
  }
  line[ipos] = 0;
  if (*buffersize == 0 && ipos == 0)
    buffer = NULL;
  return (buffer);
}


x86 assembly (clang, -O0):
<readLineFromBuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     11cc <readLineFromBuffer+0xbc>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
cmpl   $0xd,-0x24(%rbp)
jne    1171 <readLineFromBuffer+0x61>
jmp    11cc <readLineFromBuffer+0xbc>
cmpl   $0xa,-0x24(%rbp)
jne    1180 <readLineFromBuffer+0x70>
jmp    11cc <readLineFromBuffer+0xbc>
mov    -0x24(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x30(%rbp)
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     11c7 <readLineFromBuffer+0xb7>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe43(%rip),%rsi
call   1030 <fprintf@plt>
jmp    11cc <readLineFromBuffer+0xbc>
jmp    1130 <readLineFromBuffer+0x20>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11f9 <readLineFromBuffer+0xe9>
cmpq   $0x0,-0x30(%rbp)
jne    11f9 <readLineFromBuffer+0xe9>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readLineFromBuffer>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
cmp    $0x2,%rcx
mov    $0x1,%r12d
cmovae %rcx,%r12
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r14),%rsi
test   %rsi,%rsi
je     1186 <readLineFromBuffer+0x76>
movzbl (%rbx),%eax
add    $0xffffffffffffffff,%rsi
mov    %rsi,(%r14)
add    $0x1,%rbx
cmp    $0xa,%al
je     1186 <readLineFromBuffer+0x76>
cmp    $0xd,%al
je     1186 <readLineFromBuffer+0x76>
mov    %al,(%r15,%rdx,1)
add    $0x1,%rdx
cmp    %rdx,%r12
jne    1140 <readLineFromBuffer+0x30>
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
lea    0xe85(%rip),%rsi
mov    %rcx,%rdx
call   1030 <fprintf@plt>
mov    %r12,%rdx
movb   $0x0,(%r15,%rdx,1)
xor    %eax,%eax
or     (%r14),%rdx
cmovne %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [59 x i8] c"readLineFromBuffer: end of line not found (maxlen = %ld)!\0A\00"
define i8* @readLineFromBuffer(i8* %0, i64* %1, i8* %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i64
store i8* %0, i8** %5
store i64* %1, i64** %6
store i8* %2, i8** %7
store i64 %3, i64* %8
store i64 0, i64* %10
br label %11
11: ; preds = %45, %4
%12 = load i64*, i64** %6
%13 = load i64, i64* %12
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %46
15: ; preds = %11
%16 = load i8*, i8** %5
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
store i32 %18, i32* %9
%19 = load i64*, i64** %6
%20 = load i64, i64* %19
%21 = add i64 %20, -1
store i64 %21, i64* %19
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %5
%24 = load i32, i32* %9
%25 = icmp eq i32 %24, 13
br i1 %25, label %26, label %27
26: ; preds = %15
br label %46
27: ; preds = %15
%28 = load i32, i32* %9
%29 = icmp eq i32 %28, 10
br i1 %29, label %30, label %31
30: ; preds = %27
br label %46
31: ; preds = %27
%32 = load i32, i32* %9
%33 = trunc i32 %32 to i8
%34 = load i8*, i8** %7
%35 = load i64, i64* %10
%36 = add i64 %35, 1
store i64 %36, i64* %10
%37 = getelementptr inbounds i8, i8* %34, i64 %35
store i8 %33, i8* %37
%38 = load i64, i64* %10
%39 = load i64, i64* %8
%40 = icmp uge i64 %38, %39
br i1 %40, label %41, label %45
41: ; preds = %31
%42 = load i32, i32* @stderr
%43 = load i64, i64* %8
%44 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i64 %43)
br label %46
45: ; preds = %31
br label %11
46: ; preds = %41, %30, %26, %11
%47 = load i8*, i8** %7
%48 = load i64, i64* %10
%49 = getelementptr inbounds i8, i8* %47, i64 %48
store i8 0, i8* %49
%50 = load i64*, i64** %6
%51 = load i64, i64* %50
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %46
%54 = load i64, i64* %10
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %53
store i8* null, i8** %5
br label %57
57: ; preds = %56, %53, %46
%58 = load i8*, i8** %5
ret i8* %58
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [59 x i8] c"readLineFromBuffer: end of line not found (maxlen = %ld)!\0A\00"
define i8* @readLineFromBuffer(i8* %0, i64* nocapture %1, i8* nocapture writeonly %2, i64 %3) {
%5 = call i64 @llvm.umax.i64(i64 %3, i64 1)
br label %6
6: ; preds = %15, %4
%7 = phi i8* [ %0, %4 ], [ %14, %15 ]
%8 = phi i64 [ 0, %4 ], [ %16, %15 ]
%9 = load i64, i64* %1
%10 = icmp eq i64 %9, 0
br i1 %10, label %22, label %11
11: ; preds = %6
%12 = load i8, i8* %7
%13 = add i64 %9, -1
store i64 %13, i64* %1
%14 = getelementptr inbounds i8, i8* %7, i64 1
switch i8 %12, label %15 [
i8 13, label %22
i8 10, label %22
]
15: ; preds = %11
%16 = add nuw i64 %8, 1
%17 = getelementptr inbounds i8, i8* %2, i64 %8
store i8 %12, i8* %17
%18 = icmp eq i64 %16, %5
br i1 %18, label %19, label %6
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i64 %3)
br label %22
22: ; preds = %11, %11, %6, %19
%23 = phi i8* [ %14, %19 ], [ %14, %11 ], [ %7, %6 ], [ %14, %11 ]
%24 = phi i64 [ %5, %19 ], [ %8, %6 ], [ %8, %11 ], [ %8, %11 ]
%25 = getelementptr inbounds i8, i8* %2, i64 %24
store i8 0, i8* %25
%26 = load i64, i64* %1
%27 = icmp eq i64 %26, 0
%28 = icmp eq i64 %24, 0
%29 = select i1 %27, i1 %28, i1 false
%30 = select i1 %29, i8* null, i8* %23
ret i8* %30
}