#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();
void gets(char *s, int n) {
  if (!s)
    return;
  n--;
  s[n] = 0;
  while (n--) {
    *s = getc();
    if (*s == '\r') {
      *s = 0;
      return;
    }
    s++;
  }
}


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    112f <gets+0x1f>
jmp    1194 <gets+0x84>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
cmp    $0x0,%eax
je     1194 <gets+0x84>
mov    $0x0,%al
call   1030 <getc@plt>
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xd,%eax
jne    1183 <gets+0x73>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
jmp    1194 <gets+0x84>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1144 <gets+0x34>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     115f <gets+0x4f>
mov    %rdi,%r15
movslq %esi,%rax
movb   $0x0,-0x1(%rdi,%rax,1)
add    $0xffffffffffffffff,%rax
test   %eax,%eax
je     115f <gets+0x4f>
mov    %eax,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <getc@plt>
mov    %al,(%r15,%rbx,1)
cmp    $0xd,%al
je     115a <gets+0x4a>
add    $0x1,%rbx
cmp    %ebx,%r14d
jne    1140 <gets+0x30>
jmp    115f <gets+0x4f>
movb   $0x0,(%r15,%rbx,1)
pop    %rbx
pop    %r14
pop    %r15
ret

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

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