#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();
char *__gets(char *s, int max_size) {
  int i;
  for (i = 0; (s[i] = __getc()) != '\n' && i < max_size - 1; ++i) {
  }
  s[i] = 0;
  return s;
}


x86 assembly (clang, -O0):
<__gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1030 <__getc@plt>
mov    -0x8(%rbp),%rcx
movslq -0x10(%rbp),%rdx
mov    %al,(%rcx,%rdx,1)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x11(%rbp)
je     115a <__gets+0x4a>
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
setl   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    116a <__gets+0x5a>
jmp    117d <__gets+0x6d>
jmp    116f <__gets+0x5f>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1126 <__gets+0x16>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__gets>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
add    $0xffffffff,%esi
movslq %esi,%r15
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <__getc@plt>
mov    %al,(%r14,%rbx,1)
lea    0x1(%rbx),%rcx
cmp    $0xa,%al
je     113b <__gets+0x2b>
cmp    %r15,%rbx
mov    %rcx,%rbx
jl     1120 <__gets+0x10>
movb   $0x0,-0x1(%r14,%rcx,1)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @__gets(i8* returned writeonly %0, i32 %1) {
%3 = add nsw i32 %1, -1
%4 = sext i32 %3 to i64
br label %5
5: ; preds = %5, %2
%6 = phi i64 [ %12, %5 ], [ 0, %2 ]
%7 = tail call signext i8 (...) @__getc()
%8 = getelementptr inbounds i8, i8* %0, i64 %6
store i8 %7, i8* %8
%9 = icmp ne i8 %7, 10
%10 = icmp slt i64 %6, %4
%11 = select i1 %9, i1 %10, i1 false
%12 = add nuw nsw i64 %6, 1
br i1 %11, label %5, label %13
13: ; preds = %5
%14 = and i64 %6, 4294967295
%15 = getelementptr inbounds i8, i8* %0, i64 %14
store i8 0, i8* %15
ret i8* %0
}