#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*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *);
int strlen(char *);
void refine(FILE *stream_out, FILE *stream_in, const size_t limit) {
  int len;
  char word[limit];
  while (!feof(stream_in)) {
    fscanf(stream_in, "%s", word);
    if (len + strlen(word) + 1 <= limit + 1) {
      fprintf(stream_out, "%s ", word);
      len += strlen(word) + 1;
    } else {
      fprintf(stream_out, "\n%s ", word);
      len = strlen(word) + 1;
    }
  }
  fprintf(stream_out, "\n");
}


x86 assembly (clang, -O0):
<refine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1199 <refine+0x59>
jmp    1230 <refine+0xf0>
mov    -0x38(%rbp),%rdx
mov    -0x10(%rbp),%rdi
lea    0xe58(%rip),%rsi
call   1040 <fscanf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
mov    -0x18(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
ja     1206 <refine+0xc6>
mov    -0x38(%rbp),%rdx
mov    -0x8(%rbp),%rdi
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    122b <refine+0xeb>
mov    -0x38(%rbp),%rdx
mov    -0x8(%rbp),%rdi
lea    0xdee(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    117b <refine+0x3b>
mov    -0x8(%rbp),%rdi
lea    0xdcd(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<refine>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,-0x30(%rbp)
mov    %rsp,%rbx
lea    0xf(%rdx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rsi,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     11a2 <refine+0x52>
mov    $0xa,%edi
mov    -0x30(%rbp),%rsi
call   1050 <fputc@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
add    $0x1,%r15
jmp    11e5 <refine+0x95>
nopl   0x0(%rax,%rax,1)
mov    -0x30(%rbp),%rdi
lea    0xe48(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r14d
mov    $0x1,%r13d
add    %r14d,%r13d
mov    %r12,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    1185 <refine+0x35>
mov    %r12,%rdi
lea    0xe11(%rip),%rsi
mov    %rbx,%rdx
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    %r13d,%eax
add    $0x1,%eax
cltq
cmp    %rax,%r15
jb     11b0 <refine+0x60>
mov    %r13d,%r14d
add    $0x1,%r14d
mov    -0x30(%rbp),%rdi
lea    0xde6(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r13d
jmp    11d6 <refine+0x86>

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [4 x i8] c"%s \00"
@.str.2 = [5 x i8] c"\0A%s \00"
@.str.3 = [2 x i8] c"\0A\00"
define void @refine(i32* %0, i32* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
store i32* %0, i32** %4
store i32* %1, i32** %5
store i64 %2, i64* %6
%10 = load i64, i64* %6
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %8
%12 = alloca i8, i64 %10
store i64 %10, i64* %9
br label %13
13: ; preds = %41, %3
%14 = load i32*, i32** %5
%15 = call i32 @feof(i32* %14)
%16 = icmp ne i32 %15, 0
%17 = xor i1 %16, true
br i1 %17, label %18, label %42
18: ; preds = %13
%19 = load i32*, i32** %5
%20 = call i32 @fscanf(i32* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %12)
%21 = load i32, i32* %7
%22 = call i32 @strlen(i8* %12)
%23 = add nsw i32 %21, %22
%24 = add nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = load i64, i64* %6
%27 = add i64 %26, 1
%28 = icmp ule i64 %25, %27
br i1 %28, label %29, label %36
29: ; preds = %18
%30 = load i32*, i32** %4
%31 = call i32 (i32*, i8*, ...) @fprintf(i32* %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%32 = call i32 @strlen(i8* %12)
%33 = add nsw i32 %32, 1
%34 = load i32, i32* %7
%35 = add nsw i32 %34, %33
store i32 %35, i32* %7
br label %41
36: ; preds = %18
%37 = load i32*, i32** %4
%38 = call i32 (i32*, i8*, ...) @fprintf(i32* %37, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%39 = call i32 @strlen(i8* %12)
%40 = add nsw i32 %39, 1
store i32 %40, i32* %7
br label %41
41: ; preds = %36, %29
br label %13
42: ; preds = %13
%43 = load i32*, i32** %4
%44 = call i32 (i32*, i8*, ...) @fprintf(i32* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%45 = load i8*, i8** %8
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [4 x i8] c"%s \00"
@.str.2 = [5 x i8] c"\0A%s \00"
define void @refine(i32* nocapture %0, i32* nocapture %1, i64 %2) {
%4 = alloca i8, i64 %2
%5 = call i32 @feof(i32* %1)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %29
7: ; preds = %3
%8 = add i64 %2, 1
br label %9
9: ; preds = %7, %23
%10 = phi i32 [ undef, %7 ], [ %26, %23 ]
%11 = call i32 @fscanf(i32* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %4)
%12 = call i32 @strlen(i8* %4)
%13 = add i32 %10, 1
%14 = add i32 %13, %12
%15 = sext i32 %14 to i64
%16 = icmp ult i64 %8, %15
br i1 %16, label %20, label %17
17: ; preds = %9
%18 = call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%19 = call i32 @strlen(i8* %4)
br label %23
20: ; preds = %9
%21 = call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%22 = call i32 @strlen(i8* %4)
br label %23
23: ; preds = %20, %17
%24 = phi i32 [ 1, %20 ], [ %19, %17 ]
%25 = phi i32 [ %22, %20 ], [ %13, %17 ]
%26 = add i32 %25, %24
%27 = call i32 @feof(i32* %1)
%28 = icmp eq i32 %27, 0
br i1 %28, label %9, label %29
29: ; preds = %23, %3
%30 = call i32 @fputc(i32 10, i32* %0)
ret void
}