#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 */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
char getchar();
int /*<<< orphan*/ stdin;
int getstr(char *str, int maxlen) {
  char c;
  int len = 0;
  while ((c = getchar()) != '\n' && len < maxlen - 1) {
    str[len++] = c;
  }
  str[len] = '\0';
  fflush(stdin);
  return len;
}


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)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,-0xd(%rbp)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x15(%rbp)
je     1162 <getstr+0x42>
mov    -0x14(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
setl   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1172 <getstr+0x52>
jmp    118f <getstr+0x6f>
mov    -0xd(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1136 <getstr+0x16>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getstr>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%r15d
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%al
je     1168 <getstr+0x48>
cmp    $0x2,%r15d
jl     1168 <getstr+0x48>
add    $0xffffffff,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%al
je     1168 <getstr+0x48>
cmp    %r15,%rbx
jb     1150 <getstr+0x30>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stdin = global i32 0
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
br label %7
7: ; preds = %18, %2
%8 = call signext i8 (...) @getchar()
store i8 %8, i8* %5
%9 = sext i8 %8 to i32
%10 = icmp ne i32 %9, 10
br i1 %10, label %11, label %16
11: ; preds = %7
%12 = load i32, i32* %6
%13 = load i32, i32* %4
%14 = sub nsw i32 %13, 1
%15 = icmp slt i32 %12, %14
br label %16
16: ; preds = %11, %7
%17 = phi i1 [ false, %7 ], [ %15, %11 ]
br i1 %17, label %18, label %25
18: ; preds = %16
%19 = load i8, i8* %5
%20 = load i8*, i8** %3
%21 = load i32, i32* %6
%22 = add nsw i32 %21, 1
store i32 %22, i32* %6
%23 = sext i32 %21 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
store i8 %19, i8* %24
br label %7
25: ; preds = %16
%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
%30 = load i32, i32* @stdin
%31 = call i32 @fflush(i32 %30)
%32 = load i32, i32* %6
ret i32 %32
}

LLVM IR (-O2):
@stdin = global i32 0
define i32 @getstr(i8* nocapture writeonly %0, i32 %1) {
%3 = tail call signext i8 (...) @getchar()
%4 = icmp ne i8 %3, 10
%5 = icmp sgt i32 %1, 1
%6 = and i1 %4, %5
br i1 %6, label %7, label %21
7: ; preds = %2
%8 = add nsw i32 %1, -1
%9 = zext i32 %8 to i64
br label %10
10: ; preds = %7, %10
%11 = phi i64 [ 0, %7 ], [ %13, %10 ]
%12 = phi i8 [ %3, %7 ], [ %15, %10 ]
%13 = add nuw nsw i64 %11, 1
%14 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 %12, i8* %14
%15 = tail call signext i8 (...) @getchar()
%16 = icmp ne i8 %15, 10
%17 = icmp ult i64 %13, %9
%18 = select i1 %16, i1 %17, i1 false
br i1 %18, label %10, label %19
19: ; preds = %10
%20 = trunc i64 %13 to i32
br label %21
21: ; preds = %19, %2
%22 = phi i32 [ 0, %2 ], [ %20, %19 ]
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %0, i64 %23
store i8 0, i8* %24
%25 = load i32, i32* @stdin
%26 = tail call i32 @fflush(i32 %25)
ret i32 %22
}