#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 /*<<< orphan*/ add_word(char *);
int /*<<< orphan*/ clear_line();
int /*<<< orphan*/ flush_line();
int read_word(char *, int);
int space_remaining();
int /*<<< orphan*/ write_line();
int main(void) {
  char word[20 + 2];
  int word_len = 0;
  clear_line();
  for (;;) {
    word_len = read_word(word, 20 + 1);
    if (word_len == 0) {
      flush_line();
      return 0;
    }
    if (word_len > 20) {
      word[20] = '*';
    }
    if (word_len + 1 > space_remaining()) {
      write_line();
      clear_line();
    }
    add_word(word);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0x0,%al
call   1040 <clear_line@plt>
lea    -0x20(%rbp),%rdi
mov    $0x15,%esi
call   1060 <read_word@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11a7 <main+0x47>
mov    $0x0,%al
call   1080 <flush_line@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret
cmpl   $0x14,-0x24(%rbp)
jle    11b5 <main+0x55>
movb   $0x2a,-0xc(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <space_remaining@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cmp    %ecx,%eax
jle    11e0 <main+0x80>
mov    $0x0,%al
call   1070 <write_line@plt>
mov    $0x0,%al
call   1040 <clear_line@plt>
lea    -0x20(%rbp),%rdi
call   1050 <add_word@plt>
jmp    117d <main+0x1d>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   1040 <clear_line@plt>
mov    %rsp,%rdi
mov    $0x15,%esi
call   1060 <read_word@plt>
test   %eax,%eax
je     11d0 <main+0x70>
mov    %eax,%ebp
mov    %rsp,%rbx
jmp    11ab <main+0x4b>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1050 <add_word@plt>
mov    %rbx,%rdi
mov    $0x15,%esi
call   1060 <read_word@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11d0 <main+0x70>
cmp    $0x15,%ebp
jl     11b5 <main+0x55>
movb   $0x2a,0x14(%rsp)
xor    %eax,%eax
call   1030 <space_remaining@plt>
cmp    %eax,%ebp
jl     1190 <main+0x30>
xor    %eax,%eax
call   1070 <write_line@plt>
xor    %eax,%eax
call   1040 <clear_line@plt>
jmp    1190 <main+0x30>
xor    %eax,%eax
call   1080 <flush_line@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @main() {
%1 = alloca i32
%2 = alloca [22 x i8]
%3 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
%4 = call i32 (...) @clear_line()
br label %5
5: ; preds = %25, %0
%6 = getelementptr inbounds [22 x i8], [22 x i8]* %2, i64 0, i64 0
%7 = call i32 @read_word(i8* %6, i32 21)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = call i32 (...) @flush_line()
ret i32 0
12: ; preds = %5
%13 = load i32, i32* %3
%14 = icmp sgt i32 %13, 20
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = getelementptr inbounds [22 x i8], [22 x i8]* %2, i64 0, i64 20
store i8 42, i8* %16
br label %17
17: ; preds = %15, %12
%18 = load i32, i32* %3
%19 = add nsw i32 %18, 1
%20 = call i32 (...) @space_remaining()
%21 = icmp sgt i32 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = call i32 (...) @write_line()
%24 = call i32 (...) @clear_line()
br label %25
25: ; preds = %22, %17
%26 = getelementptr inbounds [22 x i8], [22 x i8]* %2, i64 0, i64 0
%27 = call i32 @add_word(i8* %26)
br label %5
}

LLVM IR (-O2):
define i32 @main() {
%1 = alloca [22 x i8]
%2 = getelementptr inbounds [22 x i8], [22 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @clear_line()
%4 = call i32 @read_word(i8* %2, i32 21)
%5 = icmp eq i32 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %0
%7 = getelementptr inbounds [22 x i8], [22 x i8]* %1, i64 0, i64 20
br label %10
8: ; preds = %20, %0
%9 = call i32 (...) @flush_line()
ret i32 0
10: ; preds = %6, %20
%11 = phi i32 [ %4, %6 ], [ %22, %20 ]
%12 = icmp sgt i32 %11, 20
br i1 %12, label %13, label %14
13: ; preds = %10
store i8 42, i8* %7
br label %14
14: ; preds = %13, %10
%15 = call i32 (...) @space_remaining()
%16 = icmp slt i32 %11, %15
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = call i32 (...) @write_line()
%19 = call i32 (...) @clear_line()
br label %20
20: ; preds = %17, %14
%21 = call i32 @add_word(i8* %2)
%22 = call i32 @read_word(i8* %2, i32 21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %8, label %10
}