#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 */
char getchar();
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ puts(char *);
int strlen(char *);
void getline(char *prompt, char *line, int n) {
  int i;
  char c;
  puts(prompt);
  puts(line);
  i = strlen(line);
  while (i < n - 1) {
    c = getchar();
    if (c >= ' ' && c < 0x7F) {
      putchar(c);
      line[i] = c;
      i++;
    } else if (c == '\r') {
      putchar('\n');
      line[i] = '\0';
      i = n - 1;
    } else if (c == '\b' || c == 0x7F) {
      if (i > 0) {
        putchar('\b');
        putchar(' ');
        putchar('\b');
        i--;
      }
    }
  }
  line[n - 1] = '\0';
}


x86 assembly (clang, -O0):
<getline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1040 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    125b <getline+0x11b>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%eax
cmp    $0x20,%eax
jl     11cb <getline+0x8b>
movsbl -0x19(%rbp),%eax
cmp    $0x7f,%eax
jge    11cb <getline+0x8b>
movsbl -0x19(%rbp),%edi
call   1030 <putchar@plt>
mov    -0x19(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1256 <getline+0x116>
movsbl -0x19(%rbp),%eax
cmp    $0xd,%eax
jne    11fc <getline+0xbc>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1251 <getline+0x111>
movsbl -0x19(%rbp),%eax
cmp    $0x8,%eax
je     1216 <getline+0xd6>
movsbl -0x19(%rbp),%eax
cmp    $0x7f,%eax
jne    124c <getline+0x10c>
cmpl   $0x0,-0x18(%rbp)
jle    1247 <getline+0x107>
mov    $0x8,%edi
call   1030 <putchar@plt>
mov    $0x20,%edi
call   1030 <putchar@plt>
mov    $0x8,%edi
call   1030 <putchar@plt>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    124c <getline+0x10c>
jmp    1251 <getline+0x111>
jmp    1256 <getline+0x116>
jmp    1171 <getline+0x31>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
call   1040 <puts@plt>
mov    %r14,%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
movslq %ebx,%r15
add    $0xffffffffffffffff,%r15
cmp    %r15d,%eax
jge    11f5 <getline+0xb5>
mov    %eax,%ebp
jmp    1197 <getline+0x57>
cs nopw 0x0(%rax,%rax,1)
nop
movsbl %bl,%edi
call   1030 <putchar@plt>
movslq %ebp,%rax
mov    %bl,(%r14,%rax,1)
add    $0x1,%ebp
cmp    %r15d,%ebp
jge    11f5 <getline+0xb5>
xor    %eax,%eax
call   1060 <getchar@plt>
mov    %eax,%ebx
lea    -0x20(%rbx),%eax
cmp    $0x5e,%al
jbe    1180 <getline+0x40>
cmp    $0xd,%bl
je     11e3 <getline+0xa3>
cmp    $0x8,%bl
setne  %al
cmp    $0x7f,%bl
setne  %cl
test   %cl,%al
jne    1192 <getline+0x52>
test   %ebp,%ebp
jle    1192 <getline+0x52>
mov    $0x8,%edi
call   1030 <putchar@plt>
mov    $0x20,%edi
call   1030 <putchar@plt>
mov    $0x8,%edi
call   1030 <putchar@plt>
add    $0xffffffff,%ebp
jmp    1192 <getline+0x52>
mov    $0xa,%edi
call   1030 <putchar@plt>
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
movb   $0x0,(%r14,%r15,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @getline(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = call i32 @puts(i8* %9)
%11 = load i8*, i8** %5
%12 = call i32 @puts(i8* %11)
%13 = load i8*, i8** %5
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %7
br label %15
15: ; preds = %71, %3
%16 = load i32, i32* %7
%17 = load i32, i32* %6
%18 = sub nsw i32 %17, 1
%19 = icmp slt i32 %16, %18
br i1 %19, label %20, label %72
20: ; preds = %15
%21 = call signext i8 (...) @getchar()
store i8 %21, i8* %8
%22 = load i8, i8* %8
%23 = sext i8 %22 to i32
%24 = icmp sge i32 %23, 32
br i1 %24, label %25, label %39
25: ; preds = %20
%26 = load i8, i8* %8
%27 = sext i8 %26 to i32
%28 = icmp slt i32 %27, 127
br i1 %28, label %29, label %39
29: ; preds = %25
%30 = load i8, i8* %8
%31 = call i32 @putchar(i8 signext %30)
%32 = load i8, i8* %8
%33 = load i8*, i8** %5
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 %32, i8* %36
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
store i32 %38, i32* %7
br label %71
39: ; preds = %25, %20
%40 = load i8, i8* %8
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 13
br i1 %42, label %43, label %51
43: ; preds = %39
%44 = call i32 @putchar(i8 signext 10)
%45 = load i8*, i8** %5
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 0, i8* %48
%49 = load i32, i32* %6
%50 = sub nsw i32 %49, 1
store i32 %50, i32* %7
br label %70
51: ; preds = %39
%52 = load i8, i8* %8
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 8
br i1 %54, label %59, label %55
55: ; preds = %51
%56 = load i8, i8* %8
%57 = sext i8 %56 to i32
%58 = icmp eq i32 %57, 127
br i1 %58, label %59, label %69
59: ; preds = %55, %51
%60 = load i32, i32* %7
%61 = icmp sgt i32 %60, 0
br i1 %61, label %62, label %68
62: ; preds = %59
%63 = call i32 @putchar(i8 signext 8)
%64 = call i32 @putchar(i8 signext 32)
%65 = call i32 @putchar(i8 signext 8)
%66 = load i32, i32* %7
%67 = add nsw i32 %66, -1
store i32 %67, i32* %7
br label %68
68: ; preds = %62, %59
br label %69
69: ; preds = %68, %55
br label %70
70: ; preds = %69, %43
br label %71
71: ; preds = %70, %29
br label %15
72: ; preds = %15
%73 = load i8*, i8** %5
%74 = load i32, i32* %6
%75 = sub nsw i32 %74, 1
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %73, i64 %76
store i8 0, i8* %77
ret void
}

LLVM IR (-O2):
define void @getline(i8* nocapture %0, i8* %1, i32 %2) {
%4 = tail call i32 @puts(i8* dereferenceable(1) %0)
%5 = tail call i32 @puts(i8* dereferenceable(1) %1)
%6 = tail call i32 @strlen(i8* %1)
%7 = add nsw i32 %2, -1
%8 = icmp slt i32 %6, %7
br i1 %8, label %9, label %39
9: ; preds = %3, %36
%10 = phi i32 [ %37, %36 ], [ %6, %3 ]
%11 = tail call signext i8 (...) @getchar()
%12 = add i8 %11, -32
%13 = icmp ult i8 %12, 95
br i1 %13, label %14, label %19
14: ; preds = %9
%15 = tail call i32 @putchar(i8 signext %11)
%16 = sext i32 %10 to i64
%17 = getelementptr inbounds i8, i8* %1, i64 %16
store i8 %11, i8* %17
%18 = add nsw i32 %10, 1
br label %36
19: ; preds = %9
%20 = icmp eq i8 %11, 13
br i1 %20, label %21, label %25
21: ; preds = %19
%22 = tail call i32 @putchar(i8 signext 10)
%23 = sext i32 %10 to i64
%24 = getelementptr inbounds i8, i8* %1, i64 %23
store i8 0, i8* %24
br label %39
25: ; preds = %19
%26 = icmp eq i8 %11, 8
%27 = icmp eq i8 %11, 127
%28 = or i1 %26, %27
%29 = icmp sgt i32 %10, 0
%30 = select i1 %28, i1 %29, i1 false
br i1 %30, label %31, label %36
31: ; preds = %25
%32 = tail call i32 @putchar(i8 signext 8)
%33 = tail call i32 @putchar(i8 signext 32)
%34 = tail call i32 @putchar(i8 signext 8)
%35 = add nsw i32 %10, -1
br label %36
36: ; preds = %31, %25, %14
%37 = phi i32 [ %18, %14 ], [ %35, %31 ], [ %10, %25 ]
%38 = icmp slt i32 %37, %7
br i1 %38, label %9, label %39
39: ; preds = %36, %21, %3
%40 = sext i32 %7 to i64
%41 = getelementptr inbounds i8, i8* %1, i64 %40
store i8 0, i8* %41
ret void
}