#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 ser_getc(int, char *);
int ser_readln(int fd, char *b) {
  char c;
  int i;
  int r;
  for (i = 0; i < 1000; i++) {
    r = ser_getc(fd, &c);
    if ((c == '\r') || (c == '\n')) {
      *(b + i) = '\0';
      return i;
    } else if (r != -1) {
      *(b + i) = c;
    } else {
      return -1;
    }
  }
  *(b + i + 1) = '\0';
  return i + 1;
}


x86 assembly (clang, -O0):
<ser_readln>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x3e8,-0x18(%rbp)
jge    11b4 <ser_readln+0xa4>
mov    -0x8(%rbp),%edi
lea    -0x11(%rbp),%rsi
call   1030 <ser_getc@plt>
mov    %eax,-0x1c(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0xd,%eax
je     115c <ser_readln+0x4c>
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
jne    1173 <ser_readln+0x63>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ca <ser_readln+0xba>
cmpl   $0xffffffff,-0x1c(%rbp)
je     1190 <ser_readln+0x80>
mov    -0x11(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    119c <ser_readln+0x8c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ca <ser_readln+0xba>
jmp    11a1 <ser_readln+0x91>
jmp    11a6 <ser_readln+0x96>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1126 <ser_readln+0x16>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ser_readln>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
lea    0x7(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %r14,%rsi
call   1030 <ser_getc@plt>
movzbl 0x7(%rsp),%ecx
cmp    $0xd,%cl
je     116b <ser_readln+0x5b>
cmp    $0xa,%cl
je     116b <ser_readln+0x5b>
cmp    $0xffffffff,%eax
je     1174 <ser_readln+0x64>
mov    %cl,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    $0x3e8,%rbx
jne    1130 <ser_readln+0x20>
mov    $0x3e9,%eax
mov    $0x3e9,%ebx
jmp    116d <ser_readln+0x5d>
mov    %ebx,%eax
movb   $0x0,(%r15,%rbx,1)
jmp    1179 <ser_readln+0x69>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @ser_readln(i32 %0, i8* nocapture writeonly %1) {
%3 = alloca i8
br label %4
4: ; preds = %2, %13
%5 = phi i64 [ 0, %2 ], [ %15, %13 ]
%6 = call i32 @ser_getc(i32 %0, i8* %3)
%7 = load i8, i8* %3
switch i8 %7, label %11 [
i8 13, label %8
i8 10, label %8
]
8: ; preds = %4, %4
%9 = trunc i64 %5 to i32
%10 = and i64 %5, 4294967295
br label %17
11: ; preds = %4
%12 = icmp eq i32 %6, -1
br i1 %12, label %21, label %13
13: ; preds = %11
%14 = getelementptr inbounds i8, i8* %1, i64 %5
store i8 %7, i8* %14
%15 = add nuw nsw i64 %5, 1
%16 = icmp eq i64 %15, 1000
br i1 %16, label %17, label %4
17: ; preds = %13, %8
%18 = phi i64 [ %10, %8 ], [ 1001, %13 ]
%19 = phi i32 [ %9, %8 ], [ 1001, %13 ]
%20 = getelementptr inbounds i8, i8* %1, i64 %18
store i8 0, i8* %20
br label %21
21: ; preds = %11, %17
%22 = phi i32 [ %19, %17 ], [ -1, %11 ]
ret i32 %22
}