#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 ERR;
char getch();
void gets_(char *str, int len) {
  char c;
  char *p;
  int char_count;
  p = str;
  char_count = 0;
  while (1) {
    c = getch();
    if (c != ERR)
      if (c == '\n') {
        *p = 0;
        break;
      } else {
        *p++ = c;
        if (char_count++ >= len)
          break;
      }
  }
}


x86 assembly (clang, -O0):
<gets_>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getch@plt>
mov    %al,-0xd(%rbp)
movsbl -0xd(%rbp),%eax
mov    0x2ea5(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     119e <gets_+0x8e>
movsbl -0xd(%rbp),%eax
cmp    $0xa,%eax
jne    1167 <gets_+0x57>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    11a3 <gets_+0x93>
mov    -0xd(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x1c(%rbp)
cmp    -0xc(%rbp),%eax
jl     1194 <gets_+0x84>
jmp    11a3 <gets_+0x93>
jmp    1199 <gets_+0x89>
jmp    119e <gets_+0x8e>
jmp    112e <gets_+0x1e>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets_>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2ec4(%rip),%rbp
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <getch@plt>
cmp    0x0(%rbp),%al
je     1130 <gets_+0x20>
cmp    $0xa,%al
je     1154 <gets_+0x44>
mov    %al,(%rbx)
add    $0x1,%rbx
lea    0x1(%r15),%eax
cmp    %r14d,%r15d
mov    %eax,%r15d
jl     1130 <gets_+0x20>
jmp    1157 <gets_+0x47>
movb   $0x0,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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