#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 */
int /*<<< orphan*/ INT_MAX;
char *fgets(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
size_t strlen(char *);
char *gets(char *s) {
  char *ret = fgets(s, INT_MAX, stdin);
  size_t len = ret ? strlen(s) : 0;
  if (len && s[len - 1] == '\n') {
    s[len - 1] = '\0';
  }
  return ret;
}


x86 assembly (clang, -O0):
<gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2ea9(%rip),%rax
mov    (%rax),%esi
mov    0x2ea8(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1168 <gets+0x48>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
jmp    1173 <gets+0x53>
xor    %eax,%eax
mov    %rax,-0x20(%rbp)
jmp    1173 <gets+0x53>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11af <gets+0x8f>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11af <gets+0x8f>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gets>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2eb2(%rip),%rax
mov    (%rax),%esi
mov    0x2eb1(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1161 <gets+0x41>
mov    %r14,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
je     1161 <gets+0x41>
cmpb   $0xa,-0x1(%rax,%r14,1)
jne    1161 <gets+0x41>
movb   $0x0,-0x1(%r14,%rax,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@INT_MAX = global i32 0
@stdin = global i32 0
define i8* @gets(i8* %0) {
%2 = load i32, i32* @INT_MAX
%3 = load i32, i32* @stdin
%4 = tail call i8* @fgets(i8* %0, i32 %2, i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %15, label %6
6: ; preds = %1
%7 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %6
%10 = add i64 %7, -1
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 10
br i1 %13, label %14, label %15
14: ; preds = %9
store i8 0, i8* %11
br label %15
15: ; preds = %1, %14, %9, %6
ret i8* %4
}