#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 readline(char *buf, size_t 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):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movslq -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    $0x1,%rdx
xor    %eax,%eax
cmp    %rdx,%rcx
mov    %al,-0x19(%rbp)
jae    1161 <readline+0x51>
mov    0x2ea0(%rip),%rax
mov    (%rax),%edi
call   1030 <getc@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e87(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1171 <readline+0x61>
jmp    119e <readline+0x8e>
cmpl   $0xa,-0x14(%rbp)
jne    1180 <readline+0x70>
jmp    119e <readline+0x8e>
mov    -0x14(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1127 <readline+0x17>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
mov    %rsi,%r12
add    $0xffffffffffffffff,%r12
je     1175 <readline+0x65>
mov    %rsi,%r14
mov    $0xffffffff,%eax
add    %rax,%r14
xor    %ebx,%ebx
mov    0x2ead(%rip),%r13
mov    0x2e9e(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x0(%r13),%edi
call   1030 <getc@plt>
cmp    $0xa,%eax
je     1177 <readline+0x67>
cmp    0x0(%rbp),%eax
je     1177 <readline+0x67>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1150 <readline+0x40>
mov    %r14,%rbx
jmp    1177 <readline+0x67>
xor    %ebx,%ebx
mov    %ebx,%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 @readline(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
store i32 0, i32* %6
br label %7
7: ; preds = %24, %2
%8 = load i32, i32* %6
%9 = sext i32 %8 to i64
%10 = load i64, i64* %4
%11 = sub i64 %10, 1
%12 = icmp ult i64 %9, %11
br i1 %12, label %13, label %18
13: ; preds = %7
%14 = load i32, i32* @stdin
%15 = call i32 @getc(i32 %14)
store i32 %15, i32* %5
%16 = load i32, i32* @EOF
%17 = icmp ne i32 %15, %16
br label %18
18: ; preds = %13, %7
%19 = phi i1 [ false, %7 ], [ %17, %13 ]
br i1 %19, label %20, label %33
20: ; preds = %18
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, 10
br i1 %22, label %23, label %24
23: ; preds = %20
br label %33
24: ; preds = %20
%25 = load i32, i32* %5
%26 = trunc i32 %25 to i8
%27 = load i8*, i8** %3
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 %26, i8* %30
%31 = load i32, i32* %6
%32 = add nsw i32 %31, 1
store i32 %32, i32* %6
br label %7
33: ; preds = %23, %18
%34 = load i8*, i8** %3
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 0, i8* %37
ret void
}

LLVM IR (-O2):
@stdin = global i32 0
@EOF = global i32 0
define void @readline(i8* nocapture writeonly %0, i64 %1) {
%3 = add i64 %1, -1
%4 = icmp eq i64 %3, 0
br i1 %4, label %20, label %5
5: ; preds = %2
%6 = add i64 %1, 4294967295
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 i64 %8, 1
%19 = icmp eq i64 %18, %3
br i1 %19, label %20, label %7
20: ; preds = %7, %15, %2
%21 = phi i64 [ 0, %2 ], [ %6, %15 ], [ %8, %7 ]
%22 = and i64 %21, 4294967295
%23 = getelementptr inbounds i8, i8* %0, i64 %22
store i8 0, i8* %23
ret void
}