#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 */
unsigned char getch();
int /*<<< orphan*/ pchar(char);
void scans(unsigned int length, char *outstr) {
  unsigned char temp_char = '\0';
  int index = 0;
  for (index = 0; index < length - 1;) {
    temp_char = getch();
    if (temp_char != 0) {
      outstr[index] = temp_char;
      pchar(outstr[index]);
      if (outstr[index] == '\n') {
        outstr[index] = 0;
        break;
      }
      index++;
      if (index == length - 1)
        pchar('\n');
    }
  }
  outstr[index + 1] = 0;
}


x86 assembly (clang, -O0):
<scans>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x0,-0x11(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jae    11dc <scans+0xbc>
mov    $0x0,%al
call   1040 <getch@plt>
mov    %al,-0x11(%rbp)
movzbl -0x11(%rbp),%eax
cmp    $0x0,%eax
je     11d7 <scans+0xb7>
mov    -0x11(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <pchar@plt>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11ae <scans+0x8e>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11dc <scans+0xbc>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11d2 <scans+0xb2>
mov    $0xa,%edi
call   1030 <pchar@plt>
jmp    11d7 <scans+0xb7>
jmp    1141 <scans+0x21>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scans>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
add    $0xffffffff,%r15d
jne    1145 <scans+0x25>
jmp    1182 <scans+0x62>
nopw   0x0(%rax,%rax,1)
cmp    %r15d,%ebp
jae    1182 <scans+0x62>
xor    %eax,%eax
call   1040 <getch@plt>
test   %al,%al
je     1140 <scans+0x20>
movslq %ebp,%rbx
mov    %al,(%r14,%rbx,1)
movsbl %al,%edi
call   1030 <pchar@plt>
cmpb   $0xa,(%r14,%rbx,1)
je     117d <scans+0x5d>
add    $0x1,%ebp
cmp    %r15d,%ebp
jne    1140 <scans+0x20>
mov    $0xa,%edi
call   1030 <pchar@plt>
mov    %r15d,%ebp
jmp    1182 <scans+0x62>
movb   $0x0,(%r14,%rbx,1)
movslq %ebp,%rax
movb   $0x0,0x1(%rax,%r14,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @scans(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i8 0, i8* %5
store i32 0, i32* %6
store i32 0, i32* %6
br label %7
7: ; preds = %51, %2
%8 = load i32, i32* %6
%9 = load i32, i32* %3
%10 = sub i32 %9, 1
%11 = icmp ult i32 %8, %10
br i1 %11, label %12, label %52
12: ; preds = %7
%13 = call zeroext i8 (...) @getch()
store i8 %13, i8* %5
%14 = load i8, i8* %5
%15 = zext i8 %14 to i32
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %51
17: ; preds = %12
%18 = load i8, i8* %5
%19 = load i8*, i8** %4
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
store i8 %18, i8* %22
%23 = load i8*, i8** %4
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = call i32 @pchar(i8 signext %27)
%29 = load i8*, i8** %4
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 10
br i1 %35, label %36, label %41
36: ; preds = %17
%37 = load i8*, i8** %4
%38 = load i32, i32* %6
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
br label %52
41: ; preds = %17
%42 = load i32, i32* %6
%43 = add nsw i32 %42, 1
store i32 %43, i32* %6
%44 = load i32, i32* %6
%45 = load i32, i32* %3
%46 = sub i32 %45, 1
%47 = icmp eq i32 %44, %46
br i1 %47, label %48, label %50
48: ; preds = %41
%49 = call i32 @pchar(i8 signext 10)
br label %50
50: ; preds = %48, %41
br label %51
51: ; preds = %50, %12
br label %7
52: ; preds = %36, %7
%53 = load i8*, i8** %4
%54 = load i32, i32* %6
%55 = add nsw i32 %54, 1
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %53, i64 %56
store i8 0, i8* %57
ret void
}

LLVM IR (-O2):
define void @scans(i32 %0, i8* nocapture %1) {
%3 = add i32 %0, -1
%4 = icmp eq i32 %3, 0
br i1 %4, label %25, label %5
5: ; preds = %2, %22
%6 = phi i32 [ %23, %22 ], [ 0, %2 ]
%7 = tail call zeroext i8 (...) @getch()
%8 = icmp eq i8 %7, 0
br i1 %8, label %22, label %9
9: ; preds = %5
%10 = sext i32 %6 to i64
%11 = getelementptr inbounds i8, i8* %1, i64 %10
store i8 %7, i8* %11
%12 = tail call i32 @pchar(i8 signext %7)
%13 = load i8, i8* %11
%14 = icmp eq i8 %13, 10
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = getelementptr inbounds i8, i8* %1, i64 %10
store i8 0, i8* %16
br label %25
17: ; preds = %9
%18 = add nuw nsw i32 %6, 1
%19 = icmp eq i32 %18, %3
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @pchar(i8 signext 10)
br label %25
22: ; preds = %17, %5
%23 = phi i32 [ %18, %17 ], [ %6, %5 ]
%24 = icmp ult i32 %23, %3
br i1 %24, label %5, label %25
25: ; preds = %22, %20, %2, %15
%26 = phi i32 [ %6, %15 ], [ 0, %2 ], [ %3, %20 ], [ %23, %22 ]
%27 = add nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %1, i64 %28
store i8 0, i8* %29
ret void
}