#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 */
struct filepirate {
  int dummy;
};
struct candidate_list {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ candidate_list_dump(struct candidate_list *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
struct candidate_list *fp_candidate_list_create(int);
int /*<<< orphan*/ fp_candidate_list_destroy(struct candidate_list *);
int /*<<< orphan*/ fp_get_candidates(struct filepirate *, char *, int,
                                     struct candidate_list *);
unsigned int getchar();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
void filepirate_interactive_test(struct filepirate *fp) {
  char buffer[80] = {0};
  int buffer_ptr = 0;
  unsigned int c;
  struct candidate_list *candidates;
  candidates = fp_candidate_list_create(20);
  while (true) {
    fp_get_candidates(fp, buffer, buffer_ptr, candidates);
    candidate_list_dump(candidates);
    printf("\n> %s", buffer);
    fflush(stdout);
    c = getchar();
    printf("\n");
    if (c == '\n' || c == '\r')
      break;
    else if (c == 127) {
      if (buffer_ptr > 0) {
        buffer_ptr--;
        buffer[buffer_ptr] = '\0';
      }
    } else {
      buffer[buffer_ptr++] = c;
      buffer[buffer_ptr] = '\0';
    }
  }
  fp_candidate_list_destroy(candidates);
}


x86 assembly (clang, -O0):
<filepirate_interactive_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1050 <memset@plt>
movl   $0x0,-0x64(%rbp)
mov    $0x14,%edi
call   1070 <fp_candidate_list_create@plt>
mov    %rax,-0x70(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x60(%rbp),%rsi
mov    -0x64(%rbp),%edx
mov    -0x70(%rbp),%rcx
call   1030 <fp_get_candidates@plt>
mov    -0x70(%rbp),%rdi
call   1060 <candidate_list_dump@plt>
lea    -0x60(%rbp),%rsi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    $0x0,%al
call   1080 <getchar@plt>
mov    %eax,-0x68(%rbp)
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0xa,-0x68(%rbp)
je     121a <filepirate_interactive_test+0x9a>
cmpl   $0xd,-0x68(%rbp)
jne    121f <filepirate_interactive_test+0x9f>
jmp    1273 <filepirate_interactive_test+0xf3>
cmpl   $0x7f,-0x68(%rbp)
jne    124a <filepirate_interactive_test+0xca>
cmpl   $0x0,-0x64(%rbp)
jle    1245 <filepirate_interactive_test+0xc5>
mov    -0x64(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x64(%rbp)
movslq -0x64(%rbp),%rax
movb   $0x0,-0x60(%rbp,%rax,1)
jmp    1269 <filepirate_interactive_test+0xe9>
mov    -0x68(%rbp),%eax
mov    %al,%cl
mov    -0x64(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x64(%rbp)
cltq
mov    %cl,-0x60(%rbp,%rax,1)
movslq -0x64(%rbp),%rax
movb   $0x0,-0x60(%rbp,%rax,1)
jmp    126e <filepirate_interactive_test+0xee>
jmp    11b1 <filepirate_interactive_test+0x31>
mov    -0x70(%rbp),%rdi
call   10a0 <fp_candidate_list_destroy@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filepirate_interactive_test>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rdi,%r14
xorps  %xmm0,%xmm0
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    $0x14,%edi
call   1070 <fp_candidate_list_create@plt>
mov    %rax,%r13
mov    %rsp,%r15
mov    0x2e1d(%rip),%r12
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
mov    %r13,%rcx
call   1040 <fp_get_candidates@plt>
mov    %r13,%rdi
call   1060 <candidate_list_dump@plt>
lea    0xe11(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r12),%edi
call   1090 <fflush@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
mov    %eax,%ebp
mov    $0xa,%edi
call   1030 <putchar@plt>
cmp    $0x7f,%ebp
jne    1227 <filepirate_interactive_test+0xa7>
test   %ebx,%ebx
jle    11d0 <filepirate_interactive_test+0x50>
add    $0xffffffff,%ebx
movb   $0x0,(%rsp,%rbx,1)
jmp    11d0 <filepirate_interactive_test+0x50>
cmp    $0xa,%ebp
je     1244 <filepirate_interactive_test+0xc4>
cmp    $0xd,%ebp
je     1244 <filepirate_interactive_test+0xc4>
movslq %ebx,%rax
add    $0x1,%ebx
mov    %bpl,(%rsp,%rax,1)
movslq %ebx,%rbx
movb   $0x0,(%rsp,%rbx,1)
jmp    11d0 <filepirate_interactive_test+0x50>
mov    %r13,%rdi
call   10a0 <fp_candidate_list_destroy@plt>
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.filepirate = type { i32 }
%struct.candidate_list = type { i32 }
@.str = [6 x i8] c"\0A> %s\00"
@stdout = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
define void @filepirate_interactive_test(%struct.filepirate* %0) {
%2 = alloca %struct.filepirate*
%3 = alloca [80 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.candidate_list*
store %struct.filepirate* %0, %struct.filepirate** %2
%7 = bitcast [80 x i8]* %3 to i8*
store i32 0, i32* %4
%8 = call %struct.candidate_list* @fp_candidate_list_create(i32 20)
store %struct.candidate_list* %8, %struct.candidate_list** %6
br label %9
9: ; preds = %1, %53
%10 = load %struct.filepirate*, %struct.filepirate** %2
%11 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%12 = load i32, i32* %4
%13 = load %struct.candidate_list*, %struct.candidate_list** %6
%14 = call i32 @fp_get_candidates(%struct.filepirate* %10, i8* %11, i32 %12, %struct.candidate_list* %13)
%15 = load %struct.candidate_list*, %struct.candidate_list** %6
%16 = call i32 @candidate_list_dump(%struct.candidate_list* %15)
%17 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @stdout
%20 = call i32 @fflush(i32 %19)
%21 = call i32 (...) @getchar()
store i32 %21, i32* %5
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, 10
br i1 %24, label %28, label %25
25: ; preds = %9
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, 13
br i1 %27, label %28, label %29
28: ; preds = %25, %9
br label %54
29: ; preds = %25
%30 = load i32, i32* %5
%31 = icmp eq i32 %30, 127
br i1 %31, label %32, label %42
32: ; preds = %29
%33 = load i32, i32* %4
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %32
%36 = load i32, i32* %4
%37 = add nsw i32 %36, -1
store i32 %37, i32* %4
%38 = load i32, i32* %4
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 %39
store i8 0, i8* %40
br label %41
41: ; preds = %35, %32
br label %52
42: ; preds = %29
%43 = load i32, i32* %5
%44 = trunc i32 %43 to i8
%45 = load i32, i32* %4
%46 = add nsw i32 %45, 1
store i32 %46, i32* %4
%47 = sext i32 %45 to i64
%48 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 %47
store i8 %44, i8* %48
%49 = load i32, i32* %4
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 %50
store i8 0, i8* %51
br label %52
52: ; preds = %42, %41
br label %53
53: ; preds = %52
br label %9
54: ; preds = %28
%55 = load %struct.candidate_list*, %struct.candidate_list** %6
%56 = call i32 @fp_candidate_list_destroy(%struct.candidate_list* %55)
ret void
}

LLVM IR (-O2):
%struct.filepirate = type { i32 }
%struct.candidate_list = type { i32 }
@.str = [6 x i8] c"\0A> %s\00"
@stdout = global i32 0
define void @filepirate_interactive_test(%struct.filepirate* %0) {
%2 = alloca [80 x i8]
%3 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%4 = tail call %struct.candidate_list* @fp_candidate_list_create(i32 20)
br label %5
5: ; preds = %26, %1
%6 = phi i32 [ %28, %26 ], [ 0, %1 ]
%7 = icmp sgt i32 %6, 0
br label %8
8: ; preds = %5, %16
%9 = call i32 @fp_get_candidates(%struct.filepirate* %0, i8* %3, i32 %6, %struct.candidate_list* %4)
%10 = call i32 @candidate_list_dump(%struct.candidate_list* %4)
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %3)
%12 = load i32, i32* @stdout
%13 = call i32 @fflush(i32 %12)
%14 = call i32 (...) @getchar()
%15 = call i32 @putchar(i32 10)
switch i32 %14, label %20 [
i32 13, label %30
i32 10, label %30
i32 127, label %16
]
16: ; preds = %8
br i1 %7, label %17, label %8
17: ; preds = %16
%18 = add nsw i32 %6, -1
%19 = zext i32 %18 to i64
br label %26
20: ; preds = %8
%21 = trunc i32 %14 to i8
%22 = add nsw i32 %6, 1
%23 = sext i32 %6 to i64
%24 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 %23
store i8 %21, i8* %24
%25 = sext i32 %22 to i64
br label %26
26: ; preds = %17, %20
%27 = phi i64 [ %25, %20 ], [ %19, %17 ]
%28 = phi i32 [ %22, %20 ], [ %18, %17 ]
%29 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 %27
store i8 0, i8* %29
br label %5
30: ; preds = %8, %8
%31 = call i32 @fp_candidate_list_destroy(%struct.candidate_list* %4)
ret void
}