#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();
void read_str(char *str, size_t max_len) {
  char c;
  int i = 0;
  str[max_len] = 0;
  while ((c = getchar()) != '\n') {
    if (i >= max_len)
      continue;
    str[i++] = c;
    str[i] = 0;
  }
}


x86 assembly (clang, -O0):
<read_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%eax
cmp    $0xa,%eax
je     1185 <read_str+0x75>
movslq -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jb     115c <read_str+0x4c>
jmp    1133 <read_str+0x23>
mov    -0x11(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1133 <read_str+0x23>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_str>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rax
xor    %ebx,%ebx
movb   $0x0,(%r14,%rax,1)
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%al
je     114d <read_str+0x3d>
cmp    %r15,%rbx
jae    1130 <read_str+0x20>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %rbx,%rax
jmp    1120 <read_str+0x10>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @read_str(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8
%6 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
store i32 0, i32* %6
%7 = load i8*, i8** %3
%8 = load i64, i64* %4
%9 = getelementptr inbounds i8, i8* %7, i64 %8
store i8 0, i8* %9
br label %10
10: ; preds = %20, %19, %2
%11 = call signext i8 (...) @getchar()
store i8 %11, i8* %5
%12 = sext i8 %11 to i32
%13 = icmp ne i32 %12, 10
br i1 %13, label %14, label %31
14: ; preds = %10
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = load i64, i64* %4
%18 = icmp uge i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %14
br label %10
20: ; preds = %14
%21 = load i8, i8* %5
%22 = load i8*, i8** %3
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
store i32 %24, i32* %6
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
store i8 %21, i8* %26
%27 = load i8*, i8** %3
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 0, i8* %30
br label %10
31: ; preds = %10
ret void
}

LLVM IR (-O2):
define void @read_str(i8* nocapture writeonly %0, i64 %1) {
br label %3
3: ; preds = %12, %2
%4 = phi i64 [ %13, %12 ], [ %1, %2 ]
%5 = phi i64 [ %13, %12 ], [ 0, %2 ]
%6 = getelementptr inbounds i8, i8* %0, i64 %4
store i8 0, i8* %6
%7 = icmp ult i64 %5, %1
br label %8
8: ; preds = %3, %11
%9 = tail call signext i8 (...) @getchar()
%10 = icmp eq i8 %9, 10
br i1 %10, label %15, label %11
11: ; preds = %8
br i1 %7, label %12, label %8
12: ; preds = %11
%13 = add nuw i64 %5, 1
%14 = getelementptr inbounds i8, i8* %0, i64 %5
store i8 %9, i8* %14
br label %3
15: ; preds = %8
ret void
}