#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*/ fflush(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ *stdout;
int strlen(char *);
int main(int argc, char *argv[]) {
  char buffer[1024];
  int i, current_length, last_length = 0;
  FILE *fd;
  if (argc > 1)
    fd = fopen(argv[1], "w+");
  while (fgets(buffer, 1024, stdin)) {
    buffer[1024] = 0;
    current_length = strlen(buffer);
    for (i = current_length; i < last_length; i++)
      buffer[i] = ' ';
    buffer[i] = 0;
    if (argc > 1) {
      rewind(fd);
      fprintf(fd, buffer);
      fflush(fd);
    } else {
      printf("\r%s", buffer);
      fflush(stdout);
    }
    last_length = current_length;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x41c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11b8 <main+0x48>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe54(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x428(%rbp)
jmp    11bd <main+0x4d>
lea    -0x410(%rbp),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     12b4 <main+0x144>
movb   $0x0,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x418(%rbp)
mov    -0x418(%rbp),%eax
mov    %eax,-0x414(%rbp)
mov    -0x414(%rbp),%eax
cmp    -0x41c(%rbp),%eax
jge    1238 <main+0xc8>
movslq -0x414(%rbp),%rax
movb   $0x20,-0x410(%rbp,%rax,1)
20
mov    -0x414(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x414(%rbp)
jmp    1203 <main+0x93>
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
cmpl   $0x1,-0x8(%rbp)
jle    1281 <main+0x111>
mov    -0x428(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x428(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1070 <fprintf@plt>
mov    -0x428(%rbp),%rdi
call   1080 <fflush@plt>
jmp    12a3 <main+0x133>
lea    -0x410(%rbp),%rsi
lea    0xd74(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d3d(%rip),%rax
mov    (%rax),%rdi
call   1080 <fflush@plt>
mov    -0x418(%rbp),%eax
mov    %eax,-0x41c(%rbp)
jmp    11bd <main+0x4d>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %edi,%r14d
cmp    $0x2,%edi
jl     11ac <main+0x2c>
mov    0x8(%rsi),%rdi
lea    0xe5c(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r12
mov    0x2e35(%rip),%r13
mov    0x0(%r13),%edx
mov    %rsp,%rdi
mov    $0x400,%esi
call   1070 <fgets@plt>
test   %rax,%rax
je     126f <main+0xef>
xor    %ebx,%ebx
mov    %rsp,%rbp
jmp    1217 <main+0x97>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0xe1c(%rip),%rdi
mov    %rbp,%rsi
call   1040 <printf@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%rdi
call   1090 <fflush@plt>
mov    0x0(%r13),%edx
mov    %rbp,%rdi
mov    $0x400,%esi
call   1070 <fgets@plt>
mov    %r15d,%ebx
test   %rax,%rax
je     126f <main+0xef>
movb   $0x0,0x400(%rsp)
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    %eax,%r15d
cltq
cmp    %ebx,%r15d
jge    124d <main+0xcd>
lea    (%rsp,%rax,1),%rdi
mov    %r15d,%edx
not    %edx
add    %ebx,%edx
add    $0x1,%rdx
mov    $0x20,%esi
call   1060 <memset@plt>
movslq %ebx,%rax
movb   $0x0,(%rsp,%rax,1)
cmp    $0x2,%r14d
jl     11e0 <main+0x60>
mov    %r12,%rdi
call   1050 <rewind@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
call   1080 <fprintf@plt>
mov    %r12,%rdi
jmp    11f9 <main+0x79>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"w+\00"
@stdin = global i32 0
@.str.1 = [4 x i8] c"\0D%s\00"
@stdout = global i32* null
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %10
br label %18
18: ; preds = %13, %2
br label %19
19: ; preds = %59, %18
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%21 = load i32, i32* @stdin
%22 = call i64 @fgets(i8* %20, i32 1024, i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %61
24: ; preds = %19
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 1024
store i8 0, i8* %25
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%27 = call i32 @strlen(i8* %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
store i32 %28, i32* %7
br label %29
29: ; preds = %37, %24
%30 = load i32, i32* %7
%31 = load i32, i32* %9
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %40
33: ; preds = %29
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %35
store i8 32, i8* %36
br label %37
37: ; preds = %33
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
store i32 %39, i32* %7
br label %29
40: ; preds = %29
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %42
store i8 0, i8* %43
%44 = load i32, i32* %4
%45 = icmp sgt i32 %44, 1
br i1 %45, label %46, label %54
46: ; preds = %40
%47 = load i32*, i32** %10
%48 = call i32 @rewind(i32* %47)
%49 = load i32*, i32** %10
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%51 = call i32 @fprintf(i32* %49, i8* %50)
%52 = load i32*, i32** %10
%53 = call i32 @fflush(i32* %52)
br label %59
54: ; preds = %40
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%56 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %55)
%57 = load i32*, i32** @stdout
%58 = call i32 @fflush(i32* %57)
br label %59
59: ; preds = %54, %46
%60 = load i32, i32* %8
store i32 %60, i32* %9
br label %19
61: ; preds = %19
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"w+\00"
@stdin = global i32 0
@.str.1 = [4 x i8] c"\0D%s\00"
@stdout = global i32* null
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %6, %2
%11 = phi i32* [ %9, %6 ], [ undef, %2 ]
%12 = load i32, i32* @stdin
%13 = call i64 @fgets(i8* %4, i32 1024, i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %44, label %15
15: ; preds = %10
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 1024
br label %17
17: ; preds = %15, %38
%18 = phi i32 [ 0, %15 ], [ %19, %38 ]
store i8 0, i8* %16
%19 = call i32 @strlen(i8* %4)
%20 = icmp slt i32 %19, %18
%21 = sext i32 %19 to i64
br i1 %20, label %22, label %29
22: ; preds = %17
%23 = getelementptr [1024 x i8], [1024 x i8]* %3, i64 0, i64 %21
%24 = xor i32 %19, -1
%25 = add i32 %18, %24
%26 = zext i32 %25 to i64
%27 = add nuw nsw i64 %26, 1
%28 = sext i32 %18 to i64
br label %29
29: ; preds = %22, %17
%30 = phi i64 [ %21, %17 ], [ %28, %22 ]
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %30
store i8 0, i8* %31
br i1 %5, label %32, label %35
32: ; preds = %29
%33 = call i32 @rewind(i32* %11)
%34 = call i32 @fprintf(i32* %11, i8* %4)
br label %38
35: ; preds = %29
%36 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%37 = load i32*, i32** @stdout
br label %38
38: ; preds = %35, %32
%39 = phi i32* [ %37, %35 ], [ %11, %32 ]
%40 = call i32 @fflush(i32* %39)
%41 = load i32, i32* @stdin
%42 = call i64 @fgets(i8* %4, i32 1024, i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %17
44: ; preds = %38, %10
ret i32 0
}