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

/* Variables and functions */
int /*<<< orphan*/ tty_read_line_cb(char *);
void tty_read_line_splitter(const int n, const char *buff_rd) {
  static char buff[256 + 1] = "";
  static int buff_len = 0;
  const char *p;
  p = buff_rd;
  while (p - buff_rd < n) {
    if (buff_len == sizeof(buff) - 1) {
      tty_read_line_cb(buff);
      *buff = '\0';
      buff_len = 0;
    }
    if (*p && *p != '\r' && *p != '\n') {
      buff[buff_len] = *p;
      buff[++buff_len] = '\0';
    } else if ((!*p || *p == '\n') && buff_len > 0) {
      tty_read_line_cb(buff);
      *buff = '\0';
      buff_len = 0;
    }
    p++;
  }
}


x86 assembly (clang, -O0):
<tty_read_line_splitter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
movslq -0x4(%rbp),%rcx
cmp    %rcx,%rax
jge    1235 <tty_read_line_splitter+0x125>
movslq 0x2ffe(%rip),%rax
cmp    $0x100,%rax
jne    116f <tty_read_line_splitter+0x5f>
lea    0x2ee7(%rip),%rdi
call   1030 <tty_read_line_cb@plt>
movb   $0x0,0x2edb(%rip)
movl   $0x0,0x2fd5(%rip)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11d8 <tty_read_line_splitter+0xc8>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xd,%eax
je     11d8 <tty_read_line_splitter+0xc8>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     11d8 <tty_read_line_splitter+0xc8>
mov    -0x18(%rbp),%rax
mov    (%rax),%dl
movslq 0x2f98(%rip),%rcx
lea    0x2e8d(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    0x2f88(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2f7f(%rip)
movslq %eax,%rcx
lea    0x2e71(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
jmp    1224 <tty_read_line_splitter+0x114>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     11f5 <tty_read_line_splitter+0xe5>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    121f <tty_read_line_splitter+0x10f>
cmpl   $0x0,0x2f48(%rip)
jle    121f <tty_read_line_splitter+0x10f>
lea    0x2e37(%rip),%rdi
call   1030 <tty_read_line_cb@plt>
movb   $0x0,0x2e2b(%rip)
movl   $0x0,0x2f25(%rip)
jmp    1224 <tty_read_line_splitter+0x114>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1127 <tty_read_line_splitter+0x17>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_read_line_splitter>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %edi,%edi
jle    11bb <tty_read_line_splitter+0xab>
mov    %rsi,%r15
movslq %edi,%r12
mov    0x3018(%rip),%eax
xor    %ebx,%ebx
lea    0x2f0b(%rip),%r14
jmp    1160 <tty_read_line_splitter+0x50>
nopw   0x0(%rax,%rax,1)
movslq %eax,%rdx
mov    %cl,(%rdx,%r14,1)
lea    0x1(%rdx),%rax
mov    %eax,0x2ff3(%rip)
movb   $0x0,0x1(%rdx,%r14,1)
add    $0x1,%rbx
cmp    %r12,%rbx
jge    11bb <tty_read_line_splitter+0xab>
cmp    $0x100,%eax
jne    1182 <tty_read_line_splitter+0x72>
mov    %r14,%rdi
call   1030 <tty_read_line_cb@plt>
movb   $0x0,0x2eca(%rip)
movl   $0x0,0x2fc4(%rip)
xor    %eax,%eax
movzbl (%r15,%rbx,1),%ecx
test   %cl,%cl
je     119a <tty_read_line_splitter+0x8a>
cmp    $0xa,%cl
je     1195 <tty_read_line_splitter+0x85>
cmp    $0xd,%cl
jne    1140 <tty_read_line_splitter+0x30>
cmp    $0xa,%cl
jne    1157 <tty_read_line_splitter+0x47>
test   %eax,%eax
jle    1157 <tty_read_line_splitter+0x47>
mov    %r14,%rdi
call   1030 <tty_read_line_cb@plt>
movb   $0x0,0x2e93(%rip)
movl   $0x0,0x2f8d(%rip)
xor    %eax,%eax
jmp    1157 <tty_read_line_splitter+0x47>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@tty_read_line_splitter.buff = internal global [257 x i8] zeroinitializer
@tty_read_line_splitter.buff_len = internal global i32 0
define void @tty_read_line_splitter(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%6 = load i8*, i8** %4
store i8* %6, i8** %5
br label %7
7: ; preds = %62, %2
%8 = load i8*, i8** %5
%9 = load i8*, i8** %4
%10 = ptrtoint i8* %8 to i64
%11 = ptrtoint i8* %9 to i64
%12 = sub i64 %10, %11
%13 = load i32, i32* %3
%14 = sext i32 %13 to i64
%15 = icmp slt i64 %12, %14
br i1 %15, label %16, label %65
16: ; preds = %7
%17 = load i32, i32* @tty_read_line_splitter.buff_len
%18 = sext i32 %17 to i64
%19 = icmp eq i64 %18, 256
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @tty_read_line_cb(i8* getelementptr inbounds ([257 x i8], [257 x i8]* @tty_read_line_splitter.buff, i64 0, i64 0))
store i8 0, i8* getelementptr inbounds ([257 x i8], [257 x i8]* @tty_read_line_splitter.buff, i64 0, i64 0)
store i32 0, i32* @tty_read_line_splitter.buff_len
br label %22
22: ; preds = %20, %16
%23 = load i8*, i8** %5
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %47
27: ; preds = %22
%28 = load i8*, i8** %5
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 13
br i1 %31, label %32, label %47
32: ; preds = %27
%33 = load i8*, i8** %5
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 10
br i1 %36, label %37, label %47
37: ; preds = %32
%38 = load i8*, i8** %5
%39 = load i8, i8* %38
%40 = load i32, i32* @tty_read_line_splitter.buff_len
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [257 x i8], [257 x i8]* @tty_read_line_splitter.buff, i64 0, i64 %41
store i8 %39, i8* %42
%43 = load i32, i32* @tty_read_line_splitter.buff_len
%44 = add nsw i32 %43, 1
store i32 %44, i32* @tty_read_line_splitter.buff_len
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [257 x i8], [257 x i8]* @tty_read_line_splitter.buff, i64 0, i64 %45
store i8 0, i8* %46
br label %62
47: ; preds = %32, %27, %22
%48 = load i8*, i8** %5
%49 = load i8, i8* %48
%50 = icmp ne i8 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %47
%52 = load i8*, i8** %5
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 10
br i1 %55, label %56, label %61
56: ; preds = %51, %47
%57 = load i32, i32* @tty_read_line_splitter.buff_len
%58 = icmp sgt i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %56
%60 = call i32 @tty_read_line_cb(i8* getelementptr inbounds ([257 x i8], [257 x i8]* @tty_read_line_splitter.buff, i64 0, i64 0))
store i8 0, i8* getelementptr inbounds ([257 x i8], [257 x i8]* @tty_read_line_splitter.buff, i64 0, i64 0)
store i32 0, i32* @tty_read_line_splitter.buff_len
br label %61
61: ; preds = %59, %56, %51
br label %62
62: ; preds = %61, %37
%63 = load i8*, i8** %5
%64 = getelementptr inbounds i8, i8* %63, i32 1
store i8* %64, i8** %5
br label %7
65: ; preds = %7
ret void
}

LLVM IR (-O2):
@tty_read_line_splitter.buff = internal global [257 x i8] zeroinitializer
@tty_read_line_splitter.buff_len = internal unnamed_addr global i32 0
define void @tty_read_line_splitter(i32 %0, i8* %1) {
%3 = ptrtoint i8* %1 to i64
%4 = sext i32 %0 to i64
%5 = icmp sgt i32 %0, 0
br i1 %5, label %6, label %40
6: ; preds = %2
%7 = load i32, i32* @tty_read_line_splitter.buff_len
br label %8
8: ; preds = %6, %33
%9 = phi i32 [ %34, %33 ], [ %7, %6 ]
%10 = phi i32 [ %35, %33 ], [ %7, %6 ]
%11 = phi i8* [ %36, %33 ], [ %1, %6 ]
%12 = icmp eq i32 %10, 256
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @tty_read_line_cb(i8* getelementptr inbounds ([257 x i8], [257 x i8]* @tty_read_line_splitter.buff, i64 0, i64 0))
store i8 0, i8* getelementptr inbounds ([257 x i8], [257 x i8]* @tty_read_line_splitter.buff, i64 0, i64 0)
store i32 0, i32* @tty_read_line_splitter.buff_len
br label %15
15: ; preds = %13, %8
%16 = phi i32 [ 0, %13 ], [ %9, %8 ]
%17 = phi i32 [ 0, %13 ], [ %10, %8 ]
%18 = load i8, i8* %11
switch i8 %18, label %19 [
i8 0, label %29
i8 10, label %25
i8 13, label %25
]
19: ; preds = %15
%20 = sext i32 %16 to i64
%21 = getelementptr inbounds [257 x i8], [257 x i8]* @tty_read_line_splitter.buff, i64 0, i64 %20
store i8 %18, i8* %21
%22 = add nsw i32 %16, 1
store i32 %22, i32* @tty_read_line_splitter.buff_len
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [257 x i8], [257 x i8]* @tty_read_line_splitter.buff, i64 0, i64 %23
store i8 0, i8* %24
br label %33
25: ; preds = %15, %15
%26 = icmp eq i8 %18, 10
%27 = icmp sgt i32 %17, 0
%28 = select i1 %26, i1 %27, i1 false
br i1 %28, label %31, label %33
29: ; preds = %15
%30 = icmp sgt i32 %16, 0
br i1 %30, label %31, label %33
31: ; preds = %25, %29
%32 = tail call i32 @tty_read_line_cb(i8* getelementptr inbounds ([257 x i8], [257 x i8]* @tty_read_line_splitter.buff, i64 0, i64 0))
store i8 0, i8* getelementptr inbounds ([257 x i8], [257 x i8]* @tty_read_line_splitter.buff, i64 0, i64 0)
store i32 0, i32* @tty_read_line_splitter.buff_len
br label %33
33: ; preds = %25, %29, %31, %19
%34 = phi i32 [ %16, %25 ], [ %16, %29 ], [ 0, %31 ], [ %22, %19 ]
%35 = phi i32 [ %17, %25 ], [ %16, %29 ], [ 0, %31 ], [ %22, %19 ]
%36 = getelementptr inbounds i8, i8* %11, i64 1
%37 = ptrtoint i8* %36 to i64
%38 = sub i64 %37, %3
%39 = icmp slt i64 %38, %4
br i1 %39, label %8, label %40
40: ; preds = %33, %2
ret void
}