#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char fgetc(int /*<<< orphan*/ *);
void readline(char str[], FILE *fin) {
  int n = 0;
  char c;
  while ((c = fgetc(fin)) != '\n' && n < 255)
    str[n++] = c;
  str[n] = '\0';
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %al,-0x15(%rbp)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x16(%rbp)
je     1151 <readline+0x41>
cmpl   $0xff,-0x14(%rbp)
setl   %al
mov    %al,-0x16(%rbp)
mov    -0x16(%rbp),%al
test   $0x1,%al
jne    1161 <readline+0x51>
jmp    117e <readline+0x6e>
mov    -0x15(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1127 <readline+0x17>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <fgetc@plt>
cmp    $0xa,%al
jne    112f <readline+0x1f>
xor    %r12d,%r12d
jmp    1160 <readline+0x50>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0x1(%rbx),%r12
mov    %al,(%r14,%rbx,1)
mov    %r15,%rdi
call   1030 <fgetc@plt>
cmp    $0xa,%al
je     1160 <readline+0x50>
cmp    $0xfe,%rbx
mov    %r12,%rbx
jb     1140 <readline+0x30>
movb   $0x0,(%r14,%r12,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @readline(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8
store i8* %0, i8** %3
store i32* %1, i32** %4
store i32 0, i32* %5
br label %7
7: ; preds = %17, %2
%8 = load i32*, i32** %4
%9 = call signext i8 @fgetc(i32* %8)
store i8 %9, i8* %6
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 10
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 255
br label %15
15: ; preds = %12, %7
%16 = phi i1 [ false, %7 ], [ %14, %12 ]
br i1 %16, label %17, label %24
17: ; preds = %15
%18 = load i8, i8* %6
%19 = load i8*, i8** %3
%20 = load i32, i32* %5
%21 = add nsw i32 %20, 1
store i32 %21, i32* %5
%22 = sext i32 %20 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
store i8 %18, i8* %23
br label %7
24: ; preds = %15
%25 = load i8*, i8** %3
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 0, i8* %28
ret void
}

LLVM IR (-O2):
define void @readline(i8* nocapture writeonly %0, i32* nocapture %1) {
%3 = tail call signext i8 @fgetc(i32* %1)
%4 = icmp eq i8 %3, 10
br i1 %4, label %14, label %5
5: ; preds = %2, %5
%6 = phi i64 [ %8, %5 ], [ 0, %2 ]
%7 = phi i8 [ %10, %5 ], [ %3, %2 ]
%8 = add nuw nsw i64 %6, 1
%9 = getelementptr inbounds i8, i8* %0, i64 %6
store i8 %7, i8* %9
%10 = tail call signext i8 @fgetc(i32* %1)
%11 = icmp ne i8 %10, 10
%12 = icmp ult i64 %6, 254
%13 = select i1 %11, i1 %12, i1 false
br i1 %13, label %5, label %14
14: ; preds = %5, %2
%15 = phi i64 [ 0, %2 ], [ %8, %5 ]
%16 = getelementptr inbounds i8, i8* %0, i64 %15
store i8 0, i8* %16
ret void
}