#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 EOF;
int getc(int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
void read_line(char *buf, int buf_sz) {
  int c, i = 0;
  while ((i < buf_sz - 1) && ((c = getc(stdin)) != EOF)) {
    if (c == '\n') {
      break;
    }
    buf[i] = c;
    ++i;
  }
  buf[i] = 0;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x15(%rbp)
jge    115c <read_line+0x4c>
mov    0x2ea5(%rip),%rax
mov    (%rax),%edi
call   1030 <getc@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e8c(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    116c <read_line+0x5c>
jmp    1199 <read_line+0x89>
cmpl   $0xa,-0x10(%rbp)
jne    117b <read_line+0x6b>
jmp    1199 <read_line+0x89>
mov    -0x10(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1126 <read_line+0x16>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
xor    %ebp,%ebp
cmp    $0x2,%esi
jl     1164 <read_line+0x54>
mov    %esi,%r14d
add    $0xffffffff,%r14d
mov    %r14d,%ebx
xor    %ebp,%ebp
mov    0x2eb0(%rip),%r12
mov    0x2ea1(%rip),%r13
nop
mov    (%r12),%edi
call   1030 <getc@plt>
cmp    $0xa,%eax
je     1164 <read_line+0x54>
cmp    0x0(%r13),%eax
je     1164 <read_line+0x54>
mov    %al,(%r15,%rbp,1)
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1140 <read_line+0x30>
mov    %r14d,%ebp
mov    %ebp,%eax
movb   $0x0,(%r15,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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