#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 read(int, char *, int);
int read_line(int fd, char *line) {
  char buf[1];
  int n = 0;
  while (read(fd, buf, 1) == 1) {
    if (buf[0] == '\n') {
      line[n] = 0;
      break;
    }
    line[n++] = buf[0];
  }
  line[n] = 0;
  return (n);
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    117b <read_line+0x6b>
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
jne    115e <read_line+0x4e>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    117b <read_line+0x6b>
mov    -0x11(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1126 <read_line+0x16>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
xor    %ebx,%ebx
cmp    $0x1,%eax
jne    116e <read_line+0x5e>
xor    %ebx,%ebx
lea    0x7(%rsp),%r15
nopl   0x0(%rax)
movzbl 0x7(%rsp),%eax
cmp    $0xa,%al
je     1167 <read_line+0x57>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
je     1140 <read_line+0x30>
jmp    116e <read_line+0x5e>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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