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


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x4(%rbp),%edi
movslq -0x8(%rbp),%rax
lea    -0x410(%rbp),%rsi
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x0,%rax
jle    1182 <read_line+0x62>
movslq -0x8(%rbp),%rax
movsbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    1174 <read_line+0x54>
jmp    1182 <read_line+0x62>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1135 <read_line+0x15>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x8(%rbp)
cltq
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1040 <strndup@plt>
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %edi,%ebp
mov    %rsp,%r14
mov    $0x1,%ebx
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
jle    117a <read_line+0x5a>
xor    %ebx,%ebx
nopl   0x0(%rax)
cmpb   $0xa,(%rsp,%rbx,1)
je     1173 <read_line+0x53>
lea    (%rsp,%rbx,1),%rsi
add    $0x1,%rsi
add    $0x1,%rbx
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
jg     1150 <read_line+0x30>
lea    (%rsp,%rbx,1),%r14
add    $0x1,%ebx
movb   $0x0,(%r14)
mov    %rsp,%rdi
mov    %ebx,%esi
call   1040 <strndup@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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