#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 */
char EOF;
char getchar();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int readline(char *buf, int count) {
  char ch;
  int i = 0;
  memset(buf, 0, count);
  while ((ch = getchar()) != EOF && ch != '\n') {
    buf[i] = ch;
    i++;
  }
  buf[i] = '\0';
  return i;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %al,-0xd(%rbp)
movsbl %al,%ecx
mov    0x2e90(%rip),%rax
movsbl (%rax),%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x15(%rbp)
je     1175 <readline+0x55>
movsbl -0xd(%rbp),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1185 <readline+0x65>
jmp    11a1 <readline+0x81>
mov    -0xd(%rbp),%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    1144 <readline+0x24>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%edx
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %esi,%esi
call   1030 <memset@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0xa,%al
je     1168 <readline+0x48>
mov    0x2ea3(%rip),%r15
cmp    (%r15),%al
je     1168 <readline+0x48>
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0xa,%al
je     1168 <readline+0x48>
cmp    (%r15),%al
jne    1150 <readline+0x30>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EOF = global i8 0
define i32 @readline(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %6
%7 = load i8*, i8** %3
%8 = load i32, i32* %4
%9 = call i32 @memset(i8* %7, i32 0, i32 %8)
br label %10
10: ; preds = %22, %2
%11 = call signext i8 (...) @getchar()
store i8 %11, i8* %5
%12 = sext i8 %11 to i32
%13 = load i8, i8* @EOF
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %12, %14
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = load i8, i8* %5
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 10
br label %20
20: ; preds = %16, %10
%21 = phi i1 [ false, %10 ], [ %19, %16 ]
br i1 %21, label %22, label %30
22: ; preds = %20
%23 = load i8, i8* %5
%24 = load i8*, i8** %3
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 %23, i8* %27
%28 = load i32, i32* %6
%29 = add nsw i32 %28, 1
store i32 %29, i32* %6
br label %10
30: ; preds = %20
%31 = load i8*, i8** %3
%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):
@EOF = global i8 0
define i32 @readline(i8* %0, i32 %1) {
%3 = tail call i32 @memset(i8* %0, i32 0, i32 %1)
%4 = tail call signext i8 (...) @getchar()
%5 = load i8, i8* @EOF
%6 = icmp ne i8 %4, %5
%7 = icmp ne i8 %4, 10
%8 = and i1 %7, %6
br i1 %8, label %9, label %21
9: ; preds = %2, %9
%10 = phi i64 [ %13, %9 ], [ 0, %2 ]
%11 = phi i8 [ %14, %9 ], [ %4, %2 ]
%12 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 %11, i8* %12
%13 = add nuw i64 %10, 1
%14 = tail call signext i8 (...) @getchar()
%15 = load i8, i8* @EOF
%16 = icmp ne i8 %14, %15
%17 = icmp ne i8 %14, 10
%18 = and i1 %17, %16
br i1 %18, label %9, label %19
19: ; preds = %9
%20 = trunc i64 %13 to i32
br label %21
21: ; preds = %19, %2
%22 = phi i32 [ 0, %2 ], [ %20, %19 ]
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %0, i64 %23
store i8 0, i8* %24
ret i32 %22
}