#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 */
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ getc(int /*<<< orphan*/ *);
scalar_t__ whitespace(char);
int readline(FILE *fd, char *line, int len) {
  char c, lastc;
  int pos;
  pos = 0;
  lastc = ' ';
  c = ' ';
  while ((!feof(fd)) && (c != '\n')) {
    c = (char)getc(fd);
    if (pos == len - 1) {
      line[len] = (char)0;
      return 1;
    } else {
      if ((!(whitespace(c))) || (!(whitespace(lastc)))) {
        if (whitespace(c))
          line[pos] = ' ';
        else
          line[pos] = c;
        lastc = c;
        pos++;
      }
    }
  }
  line[pos] = (char)0;
  return 0;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
movb   $0x20,-0x1e(%rbp)
movb   $0x20,-0x1d(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x25(%rbp)
jne    1178 <readline+0x48>
movsbl -0x1d(%rbp),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1188 <readline+0x58>
jmp    122e <readline+0xfe>
mov    -0x10(%rbp),%rdi
call   1050 <getc@plt>
mov    %al,-0x1d(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11bd <readline+0x8d>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x4(%rbp)
jmp    1241 <readline+0x111>
movsbl -0x1d(%rbp),%edi
call   1040 <whitespace@plt>
cmp    $0x0,%rax
je     11e3 <readline+0xb3>
movsbl -0x1e(%rbp),%edi
call   1040 <whitespace@plt>
cmp    $0x0,%rax
jne    1224 <readline+0xf4>
movsbl -0x1d(%rbp),%edi
call   1040 <whitespace@plt>
cmp    $0x0,%rax
je     1207 <readline+0xd7>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x20,(%rax,%rcx,1)
jmp    1215 <readline+0xe5>
mov    -0x1d(%rbp),%dl
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1d(%rbp),%al
mov    %al,-0x1e(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1229 <readline+0xf9>
jmp    1152 <readline+0x22>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,0x8(%rsp)
mov    %rsi,0x10(%rsp)
mov    %rdi,%r12
call   1030 <feof@plt>
mov    %eax,%ecx
xor    %r13d,%r13d
mov    $0x0,%eax
test   %ecx,%ecx
jne    11f9 <readline+0xc9>
mov    0x8(%rsp),%rax
lea    -0x1(%rax),%r14d
mov    $0x20,%r15b
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1050 <getc@plt>
cmp    %r14d,%r13d
je     11ec <readline+0xbc>
mov    %rax,%rbx
movsbl %bl,%ebp
mov    %ebp,%edi
call   1040 <whitespace@plt>
test   %rax,%rax
je     11ad <readline+0x7d>
movsbl %r15b,%edi
call   1040 <whitespace@plt>
test   %rax,%rax
jne    11d5 <readline+0xa5>
mov    %ebp,%edi
call   1040 <whitespace@plt>
test   %rax,%rax
movslq %r13d,%rax
movzbl %bpl,%ecx
mov    $0x20,%edx
cmovne %edx,%ecx
mov    0x10(%rsp),%rdx
mov    %cl,(%rdx,%rax,1)
add    $0x1,%r13d
mov    %ebx,%r15d
mov    %r12,%rdi
call   1030 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xa,%bl
je     11f9 <readline+0xc9>
test   %ecx,%ecx
je     1180 <readline+0x50>
jmp    11f9 <readline+0xc9>
mov    $0x1,%eax
mov    0x8(%rsp),%rcx
mov    %ecx,%r13d
movslq %r13d,%rcx
mov    0x10(%rsp),%rdx
movb   $0x0,(%rdx,%rcx,1)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @readline(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
%10 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %10
store i8 32, i8* %9
store i8 32, i8* %8
br label %11
11: ; preds = %62, %3
%12 = load i32*, i32** %5
%13 = call i32 @feof(i32* %12)
%14 = icmp ne i32 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = load i8, i8* %8
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %17, 10
br label %19
19: ; preds = %15, %11
%20 = phi i1 [ false, %11 ], [ %18, %15 ]
br i1 %20, label %21, label %63
21: ; preds = %19
%22 = load i32*, i32** %5
%23 = call i64 @getc(i32* %22)
%24 = trunc i64 %23 to i8
store i8 %24, i8* %8
%25 = load i32, i32* %10
%26 = load i32, i32* %7
%27 = sub nsw i32 %26, 1
%28 = icmp eq i32 %25, %27
br i1 %28, label %29, label %34
29: ; preds = %21
%30 = load i8*, i8** %6
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 0, i8* %33
store i32 1, i32* %4
br label %68
34: ; preds = %21
%35 = load i8, i8* %8
%36 = call i64 @whitespace(i8 signext %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i8, i8* %9
%40 = call i64 @whitespace(i8 signext %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %61, label %42
42: ; preds = %38, %34
%43 = load i8, i8* %8
%44 = call i64 @whitespace(i8 signext %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %42
%47 = load i8*, i8** %6
%48 = load i32, i32* %10
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 32, i8* %50
br label %57
51: ; preds = %42
%52 = load i8, i8* %8
%53 = load i8*, i8** %6
%54 = load i32, i32* %10
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
store i8 %52, i8* %56
br label %57
57: ; preds = %51, %46
%58 = load i8, i8* %8
store i8 %58, i8* %9
%59 = load i32, i32* %10
%60 = add nsw i32 %59, 1
store i32 %60, i32* %10
br label %61
61: ; preds = %57, %38
br label %62
62: ; preds = %61
br label %11
63: ; preds = %19
%64 = load i8*, i8** %6
%65 = load i32, i32* %10
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %64, i64 %66
store i8 0, i8* %67
store i32 0, i32* %4
br label %68
68: ; preds = %63, %29
%69 = load i32, i32* %4
ret i32 %69
}

LLVM IR (-O2):
define i32 @readline(i32* nocapture %0, i8* nocapture writeonly %1, i32 %2) {
%4 = tail call i32 @feof(i32* %0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %34
6: ; preds = %3
%7 = add nsw i32 %2, -1
br label %8
8: ; preds = %6, %27
%9 = phi i32 [ 0, %6 ], [ %29, %27 ]
%10 = phi i8 [ 32, %6 ], [ %28, %27 ]
%11 = tail call i64 @getc(i32* %0)
%12 = trunc i64 %11 to i8
%13 = icmp eq i32 %9, %7
br i1 %13, label %34, label %14
14: ; preds = %8
%15 = tail call i64 @whitespace(i8 signext %12)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = tail call i64 @whitespace(i8 signext %10)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %17, %14
%21 = tail call i64 @whitespace(i8 signext %12)
%22 = icmp eq i64 %21, 0
%23 = sext i32 %9 to i64
%24 = getelementptr inbounds i8, i8* %1, i64 %23
%25 = select i1 %22, i8 %12, i8 32
store i8 %25, i8* %24
%26 = add nsw i32 %9, 1
br label %27
27: ; preds = %17, %20
%28 = phi i8 [ %10, %17 ], [ %12, %20 ]
%29 = phi i32 [ %9, %17 ], [ %26, %20 ]
%30 = tail call i32 @feof(i32* %0)
%31 = icmp eq i32 %30, 0
%32 = icmp ne i8 %12, 10
%33 = and i1 %32, %31
br i1 %33, label %8, label %34
34: ; preds = %27, %8, %3
%35 = phi i32 [ 0, %3 ], [ %2, %8 ], [ %29, %27 ]
%36 = phi i32 [ 0, %3 ], [ 1, %8 ], [ 0, %27 ]
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %1, i64 %37
store i8 0, i8* %38
ret i32 %36
}