#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 readc(int);
int readb(int fd, char *buf, int *idx) {
  int c = readc(fd);
  int wasnl;
  if (c < 0) {
    return -1;
  }
  wasnl = c == '\n';
  buf[*idx] = c == '\n' ? '\0' : c;
  (*idx)++;
  return wasnl;
}


x86 assembly (clang, -O0):
<readb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <readc@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1144 <readb+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    1191 <readb+0x81>
cmpl   $0xa,-0x1c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0xa,-0x1c(%rbp)
jne    1167 <readb+0x57>
xor    %eax,%eax
mov    %eax,-0x24(%rbp)
jmp    116d <readb+0x5d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq (%rcx),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readb>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
call   1030 <readc@plt>
test   %eax,%eax
js     113e <readb+0x2e>
mov    %eax,%ecx
xor    %edx,%edx
xor    %eax,%eax
cmp    $0xa,%ecx
sete   %al
cmovne %ecx,%edx
movslq (%rbx),%rcx
mov    %dl,(%r14,%rcx,1)
addl   $0x1,(%rbx)
jmp    1143 <readb+0x33>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @readb(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%10 = load i32, i32* %5
%11 = call i32 @readc(i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %36
15: ; preds = %3
%16 = load i32, i32* %8
%17 = icmp eq i32 %16, 10
%18 = zext i1 %17 to i32
store i32 %18, i32* %9
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, 10
br i1 %20, label %21, label %22
21: ; preds = %15
br label %24
22: ; preds = %15
%23 = load i32, i32* %8
br label %24
24: ; preds = %22, %21
%25 = phi i32 [ 0, %21 ], [ %23, %22 ]
%26 = trunc i32 %25 to i8
%27 = load i8*, i8** %6
%28 = load i32*, i32** %7
%29 = load i32, i32* %28
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %27, i64 %30
store i8 %26, i8* %31
%32 = load i32*, i32** %7
%33 = load i32, i32* %32
%34 = add nsw i32 %33, 1
store i32 %34, i32* %32
%35 = load i32, i32* %9
store i32 %35, i32* %4
br label %36
36: ; preds = %24, %14
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
define i32 @readb(i32 %0, i8* nocapture writeonly %1, i32* nocapture %2) {
%4 = tail call i32 @readc(i32 %0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %16, label %6
6: ; preds = %3
%7 = icmp eq i32 %4, 10
%8 = zext i1 %7 to i32
%9 = trunc i32 %4 to i8
%10 = select i1 %7, i8 0, i8 %9
%11 = load i32, i32* %2
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %1, i64 %12
store i8 %10, i8* %13
%14 = load i32, i32* %2
%15 = add nsw i32 %14, 1
store i32 %15, i32* %2
br label %16
16: ; preds = %3, %6
%17 = phi i32 [ %8, %6 ], [ -1, %3 ]
ret i32 %17
}