#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 scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ read(int, char *, int);
int read_line(int fd, char *buf, int *count) {
  unsigned int i = 0;
  char a = 0;
  ssize_t ret = 1;
  ret = read(fd, &a, 1);
  while (a != '\n' && ret > 0) {
    buf[i] = a;
    i++;
    ret = read(fd, &a, 1);
  }
  *count = i;
  return ret;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movb   $0x0,-0x1d(%rbp)
movq   $0x1,-0x28(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,-0x28(%rbp)
movsbl -0x1d(%rbp),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x29(%rbp)
je     1168 <read_line+0x58>
cmpq   $0x0,-0x28(%rbp)
setg   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1178 <read_line+0x68>
jmp    11a8 <read_line+0x98>
mov    -0x1d(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,-0x28(%rbp)
jmp    114b <read_line+0x3b>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r12
mov    %edi,%ebp
movb   $0x0,0xf(%rsp)
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
xor    %ebx,%ebx
test   %rax,%rax
jle    1177 <read_line+0x67>
mov    0xf(%rsp),%cl
cmp    $0xa,%cl
je     1177 <read_line+0x67>
xor    %ebx,%ebx
lea    0xf(%rsp),%r15
nop
mov    %ebx,%eax
mov    %cl,(%r12,%rax,1)
add    $0x1,%ebx
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
jle    1177 <read_line+0x67>
movzbl 0xf(%rsp),%ecx
cmp    $0xa,%cl
jne    1150 <read_line+0x40>
mov    %ebx,(%r14)
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @read_line(i32 %0, i8* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca i8
store i8 0, i8* %4
%5 = call i64 @read(i32 %0, i8* %4, i32 1)
%6 = load i8, i8* %4
%7 = icmp ne i8 %6, 10
%8 = icmp sgt i64 %5, 0
%9 = and i1 %8, %7
br i1 %9, label %10, label %21
10: ; preds = %3, %10
%11 = phi i8 [ %17, %10 ], [ %6, %3 ]
%12 = phi i32 [ %15, %10 ], [ 0, %3 ]
%13 = zext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %1, i64 %13
store i8 %11, i8* %14
%15 = add i32 %12, 1
%16 = call i64 @read(i32 %0, i8* %4, i32 1)
%17 = load i8, i8* %4
%18 = icmp ne i8 %17, 10
%19 = icmp sgt i64 %16, 0
%20 = and i1 %19, %18
br i1 %20, label %10, label %21
21: ; preds = %10, %3
%22 = phi i32 [ 0, %3 ], [ %15, %10 ]
%23 = phi i64 [ %5, %3 ], [ %16, %10 ]
store i32 %22, i32* %2
%24 = trunc i64 %23 to i32
ret i32 %24
}