#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 + 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
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    11d2 <main+0x72>
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     11c6 <main+0x66>
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     11c6 <main+0x66>
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 = %20, %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 = add nsw i32 %13, 1
%15 = call i32 (...) @space_remaining()
%16 = icmp sgt i32 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = call i32 (...) @write_line()
%19 = call i32 (...) @clear_line()
br label %20
20: ; preds = %17, %12
%21 = getelementptr inbounds [22 x i8], [22 x i8]* %2, i64 0, i64 0
%22 = call i32 @add_word(i8* %21)
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 %6, label %8
6: ; preds = %15, %0
%7 = call i32 (...) @flush_line()
ret i32 0
8: ; preds = %0, %15
%9 = phi i32 [ %17, %15 ], [ %4, %0 ]
%10 = call i32 (...) @space_remaining()
%11 = icmp slt i32 %9, %10
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = call i32 (...) @write_line()
%14 = call i32 (...) @clear_line()
br label %15
15: ; preds = %12, %8
%16 = call i32 @add_word(i8* %2)
%17 = call i32 @read_word(i8* %2, i32 21)
%18 = icmp eq i32 %17, 0
br i1 %18, label %6, label %8
}