#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 getStr(char *str, int len) {
  char ch;
  int i = 0;
  for (i = 0; i < len - 1; i++) {
    ch = getchar();
    if (ch == '\n' || ch == 0 || ch == -1) {
      str[i] = 0;
      break;
    }
    str[i] = ch;
  }
  return i + 1;
}


x86 assembly (clang, -O0):
<getStr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    119c <getStr+0x8c>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,-0xd(%rbp)
movsbl -0xd(%rbp),%eax
cmp    $0xa,%eax
je     116f <getStr+0x5f>
movsbl -0xd(%rbp),%eax
cmp    $0x0,%eax
je     116f <getStr+0x5f>
movsbl -0xd(%rbp),%eax
cmp    $0xffffffff,%eax
jne    1180 <getStr+0x70>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    119c <getStr+0x8c>
mov    -0xd(%rbp),%dl
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    112d <getStr+0x1d>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getStr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %ebp,%ebp
cmp    $0x2,%esi
jl     116d <getStr+0x5d>
mov    %esi,%r14d
mov    %rdi,%rbx
add    $0xffffffff,%r14d
mov    %r14d,%r15d
mov    $0x803,%r12d
xor    %ebp,%ebp
jmp    114c <getStr+0x3c>
cs nopw 0x0(%rax,%rax,1)
mov    %al,(%rbx,%rbp,1)
add    $0x1,%rbp
cmp    %rbp,%r15
je     116a <getStr+0x5a>
xor    %eax,%eax
call   1030 <getchar@plt>
lea    0x1(%rax),%ecx
cmp    $0xb,%cl
ja     1140 <getStr+0x30>
movzbl %cl,%ecx
bt     %rcx,%r12
jae    1140 <getStr+0x30>
movb   $0x0,(%rbx,%rbp,1)
jmp    116d <getStr+0x5d>
mov    %r14d,%ebp
add    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @getStr(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %6
store i32 0, i32* %6
br label %7
7: ; preds = %36, %2
%8 = load i32, i32* %6
%9 = load i32, i32* %4
%10 = sub nsw i32 %9, 1
%11 = icmp slt i32 %8, %10
br i1 %11, label %12, label %39
12: ; preds = %7
%13 = call signext i8 (...) @getchar()
store i8 %13, i8* %5
%14 = load i8, i8* %5
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 10
br i1 %16, label %25, label %17
17: ; preds = %12
%18 = load i8, i8* %5
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %17
%22 = load i8, i8* %5
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %21, %17, %12
%26 = load i8*, i8** %3
%27 = load i32, i32* %6
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
br label %39
30: ; preds = %21
%31 = load i8, i8* %5
%32 = load i8*, i8** %3
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 %31, i8* %35
br label %36
36: ; preds = %30
%37 = load i32, i32* %6
%38 = add nsw i32 %37, 1
store i32 %38, i32* %6
br label %7
39: ; preds = %25, %7
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
ret i32 %41
}

LLVM IR (-O2):
define i32 @getStr(i8* nocapture writeonly %0, i32 %1) {
%3 = add i32 %1, -1
%4 = icmp sgt i32 %1, 1
br i1 %4, label %5, label %18
5: ; preds = %2
%6 = zext i32 %3 to i64
br label %7
7: ; preds = %5, %14
%8 = phi i64 [ 0, %5 ], [ %16, %14 ]
%9 = tail call signext i8 (...) @getchar()
switch i8 %9, label %14 [
i8 -1, label %10
i8 10, label %10
i8 0, label %10
]
10: ; preds = %7, %7, %7
%11 = trunc i64 %8 to i32
%12 = and i64 %8, 4294967295
%13 = getelementptr inbounds i8, i8* %0, i64 %12
store i8 0, i8* %13
br label %18
14: ; preds = %7
%15 = getelementptr inbounds i8, i8* %0, i64 %8
store i8 %9, i8* %15
%16 = add nuw nsw i64 %8, 1
%17 = icmp eq i64 %16, %6
br i1 %17, label %18, label %7
18: ; preds = %14, %2, %10
%19 = phi i32 [ %11, %10 ], [ 0, %2 ], [ %3, %14 ]
%20 = add nuw nsw i32 %19, 1
ret i32 %20
}