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


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1173 <gets+0x63>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getc@plt>
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    116e <gets+0x5e>
jmp    1173 <gets+0x63>
jmp    1126 <gets+0x16>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x10(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
add    $0xffffffff,%esi
mov    $0xffffffff,%eax
cmovns %esi,%eax
mov    %eax,0x4(%rsp)
movslq %eax,%r13
mov    $0xffffffffffffffff,%rbx
movabs $0xffffffff00000000,%rbp
ff ff ff
movabs $0x100000000,%r12
nopl   0x0(%rax,%rax,1)
cmp    %rbx,%r13
je     117b <gets+0x6b>
lea    0x1(%rbx),%r14
xor    %eax,%eax
call   1030 <getc@plt>
mov    %al,0x1(%r15,%rbx,1)
add    %r12,%rbp
mov    %r14,%rbx
cmp    $0xa,%al
jne    1150 <gets+0x40>
sar    $0x20,%rbp
mov    %rbp,%r13
mov    %r14d,%eax
jmp    117f <gets+0x6f>
mov    0x4(%rsp),%eax
add    $0x1,%eax
movb   $0x0,(%r15,%r13,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @gets(i8* nocapture writeonly %0, i32 %1) {
%3 = add i32 %1, -1
%4 = call i32 @llvm.smax.i32(i32 %3, i32 -1)
%5 = sext i32 %4 to i64
br label %6
6: ; preds = %9, %2
%7 = phi i64 [ %10, %9 ], [ -1, %2 ]
%8 = icmp eq i64 %7, %5
br i1 %8, label %18, label %9
9: ; preds = %6
%10 = add nsw i64 %7, 1
%11 = tail call signext i8 (...) @getc()
%12 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 %11, i8* %12
%13 = icmp eq i8 %11, 10
br i1 %13, label %14, label %6
14: ; preds = %9
%15 = trunc i64 %10 to i32
%16 = shl i64 %10, 32
%17 = ashr exact i64 %16, 32
br label %18
18: ; preds = %6, %14
%19 = phi i64 [ %17, %14 ], [ %5, %6 ]
%20 = phi i32 [ %15, %14 ], [ %4, %6 ]
%21 = add nsw i32 %20, 1
%22 = getelementptr inbounds i8, i8* %0, i64 %19
store i8 0, i8* %22
ret i32 %21
}