#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 read_char();
void read_word(char *word, int len) {
  int ch, pos = 0;
  while ((ch = read_char()) == ' ') {
    if (pos < len)
      word[pos++] = ch;
    ch = read_char();
  }
  word[pos] = '\0';
}


x86 assembly (clang, -O0):
<read_word>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1030 <read_char@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x20,%eax
jne    116e <read_word+0x5e>
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    115f <read_word+0x4f>
mov    -0x10(%rbp),%eax
mov    %al,%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)
mov    $0x0,%al
call   1030 <read_char@plt>
mov    %eax,-0x10(%rbp)
jmp    1126 <read_word+0x16>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_word>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <read_char@plt>
cmp    $0x20,%eax
jne    1154 <read_word+0x44>
xor    %ebx,%ebx
jmp    1143 <read_word+0x33>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <read_char@plt>
xor    %eax,%eax
call   1030 <read_char@plt>
cmp    $0x20,%eax
jne    1154 <read_word+0x44>
cmp    %ebp,%ebx
jge    1130 <read_word+0x20>
movslq %ebx,%rax
add    $0x1,%ebx
movb   $0x20,(%r14,%rax,1)
jmp    1130 <read_word+0x20>
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @read_word(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %6
br label %7
7: ; preds = %22, %2
%8 = call i32 (...) @read_char()
store i32 %8, i32* %5
%9 = icmp eq i32 %8, 32
br i1 %9, label %10, label %24
10: ; preds = %7
%11 = load i32, i32* %6
%12 = load i32, i32* %4
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %22
14: ; preds = %10
%15 = load i32, i32* %5
%16 = trunc i32 %15 to i8
%17 = load i8*, i8** %3
%18 = load i32, i32* %6
%19 = add nsw i32 %18, 1
store i32 %19, i32* %6
%20 = sext i32 %18 to i64
%21 = getelementptr inbounds i8, i8* %17, i64 %20
store i8 %16, i8* %21
br label %22
22: ; preds = %14, %10
%23 = call i32 (...) @read_char()
store i32 %23, i32* %5
br label %7
24: ; preds = %7
%25 = load i8*, i8** %3
%26 = load i32, i32* %6
%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 @read_word(i8* nocapture writeonly %0, i32 %1) {
%3 = tail call i32 (...) @read_char()
%4 = icmp eq i32 %3, 32
br i1 %4, label %5, label %17
5: ; preds = %2, %12
%6 = phi i32 [ %13, %12 ], [ 0, %2 ]
%7 = icmp slt i32 %6, %1
br i1 %7, label %8, label %12
8: ; preds = %5
%9 = add nsw i32 %6, 1
%10 = sext i32 %6 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 32, i8* %11
br label %12
12: ; preds = %8, %5
%13 = phi i32 [ %9, %8 ], [ %6, %5 ]
%14 = tail call i32 (...) @read_char()
%15 = tail call i32 (...) @read_char()
%16 = icmp eq i32 %15, 32
br i1 %16, label %5, label %17
17: ; preds = %12, %2
%18 = phi i32 [ 0, %2 ], [ %13, %12 ]
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %0, i64 %19
store i8 0, i8* %20
ret void
}