#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*/ perror(char *);
int read(int, char *, int);
extern int serial_readline(int fd, char buffer[], int size) {
  int nrd, rsize;
  char c;
  rsize = 0;
  do {
    nrd = read(fd, &c, 1);
    if (nrd < 0)
      break;
    buffer[rsize] = c;
    rsize += nrd;
  } while (nrd == 1 && rsize < size && c != '\n');
  buffer[rsize] = '\0';
  if (nrd >= 0)
    return (rsize);
  else {
    perror("khepera: serial_readline()");
    return (nrd);
  }
}


x86 assembly (clang, -O0):
<serial_readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    115c <serial_readline+0x3c>
jmp    11ab <serial_readline+0x8b>
mov    -0x1d(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x1,-0x18(%rbp)
mov    %al,-0x1e(%rbp)
jne    11a0 <serial_readline+0x80>
mov    -0x1c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x1e(%rbp)
jge    11a0 <serial_readline+0x80>
movsbl -0x1d(%rbp),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x1e(%rbp)
mov    -0x1e(%rbp),%al
test   $0x1,%al
jne    1139 <serial_readline+0x19>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x0,-0x18(%rbp)
jl     11cc <serial_readline+0xac>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11de <serial_readline+0xbe>
lea    0xe2d(%rip),%rdi
call   1040 <perror@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%r13d
xor    %ebx,%ebx
lea    0x7(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,%ebp
movslq %ebx,%rbx
test   %eax,%eax
js     118b <serial_readline+0x6b>
movzbl 0x7(%rsp),%eax
mov    %al,(%r14,%rbx,1)
add    %ebp,%ebx
cmp    $0x1,%ebp
jne    1172 <serial_readline+0x52>
cmp    %r15d,%ebx
jge    1172 <serial_readline+0x52>
cmp    $0xa,%al
jne    1140 <serial_readline+0x20>
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
movb   $0x0,(%r14,%rbx,1)
lea    0xe69(%rip),%rdi
call   1040 <perror@plt>
mov    %ebp,%ebx
jmp    117a <serial_readline+0x5a>

LLVM IR (-O0):
@.str = [27 x i8] c"khepera: serial_readline()\00"
define i32 @serial_readline(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
br label %11
11: ; preds = %37, %3
%12 = load i32, i32* %5
%13 = call i32 @read(i32 %12, i8* %10, i32 1)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %11
br label %39
17: ; preds = %11
%18 = load i8, i8* %10
%19 = load i8*, i8** %6
%20 = load i32, i32* %9
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
store i8 %18, i8* %22
%23 = load i32, i32* %8
%24 = load i32, i32* %9
%25 = add nsw i32 %24, %23
store i32 %25, i32* %9
br label %26
26: ; preds = %17
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, 1
br i1 %28, label %29, label %37
29: ; preds = %26
%30 = load i32, i32* %9
%31 = load i32, i32* %7
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i8, i8* %10
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 10
br label %37
37: ; preds = %33, %29, %26
%38 = phi i1 [ false, %29 ], [ false, %26 ], [ %36, %33 ]
br i1 %38, label %11, label %39
39: ; preds = %37, %16
%40 = load i8*, i8** %6
%41 = load i32, i32* %9
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
%44 = load i32, i32* %8
%45 = icmp sge i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %39
%47 = load i32, i32* %9
store i32 %47, i32* %4
br label %51
48: ; preds = %39
%49 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%50 = load i32, i32* %8
store i32 %50, i32* %4
br label %51
51: ; preds = %48, %46
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
@.str = [27 x i8] c"khepera: serial_readline()\00"
define i32 @serial_readline(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca i8
br label %5
5: ; preds = %15, %3
%6 = phi i32 [ 0, %3 ], [ %13, %15 ]
%7 = call i32 @read(i32 %0, i8* %4, i32 1)
%8 = icmp slt i32 %7, 0
br i1 %8, label %22, label %9
9: ; preds = %5
%10 = load i8, i8* %4
%11 = sext i32 %6 to i64
%12 = getelementptr inbounds i8, i8* %1, i64 %11
store i8 %10, i8* %12
%13 = add nsw i32 %7, %6
%14 = icmp eq i32 %7, 1
br i1 %14, label %15, label %19
15: ; preds = %9
%16 = icmp slt i32 %13, %2
%17 = icmp ne i8 %10, 10
%18 = select i1 %16, i1 %17, i1 false
br i1 %18, label %5, label %19
19: ; preds = %9, %15
%20 = sext i32 %13 to i64
%21 = getelementptr inbounds i8, i8* %1, i64 %20
store i8 0, i8* %21
br label %26
22: ; preds = %5
%23 = sext i32 %6 to i64
%24 = getelementptr inbounds i8, i8* %1, i64 %23
store i8 0, i8* %24
%25 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %19, %22
%27 = phi i32 [ %7, %22 ], [ %13, %19 ]
ret i32 %27
}