#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char *fgets(char *, int, int /*<<< orphan*/ *);
size_t strlen(char *);
char *readline(char *line, int n, FILE *stream) {
  if (line == NULL || stream == NULL) {
    return NULL;
  }
  static char *rs;
  rs = fgets(line, n, stream);
  if (strlen(line) >= 1) {
    line[strlen(line) - 1] = line[strlen(line)];
  }
  return rs;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1149 <readline+0x29>
cmpq   $0x0,-0x20(%rbp)
jne    1156 <readline+0x36>
movq   $0x0,-0x8(%rbp)
jmp    11cb <readline+0xab>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1040 <fgets@plt>
mov    %rax,0x2ecb(%rip)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x1,%rax
jb     11c0 <readline+0xa0>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
mov    (%rax,%rcx,1),%al
mov    %al,-0x29(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x29(%rbp),%dl
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
sub    $0x1,%rcx
mov    %dl,(%rax,%rcx,1)
mov    0x2e71(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %rdi,%rdi
je     1157 <readline+0x37>
test   %rdx,%rdx
je     1157 <readline+0x37>
mov    %rdi,%rbx
call   1040 <fgets@plt>
mov    %rax,%r14
mov    %rax,0x2ef5(%rip)
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
je     1157 <readline+0x37>
mov    (%rbx,%rax,1),%cl
mov    %cl,-0x1(%rbx,%rax,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@readline.rs = internal unnamed_addr global i8* null
define i8* @readline(i8* %0, i32 %1, i32* %2) {
%4 = icmp eq i8* %0, null
%5 = icmp eq i32* %2, null
%6 = or i1 %4, %5
br i1 %6, label %16, label %7
7: ; preds = %3
%8 = tail call i8* @fgets(i8* %0, i32 %1, i32* %2)
store i8* %8, i8** @readline.rs
%9 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%10 = icmp eq i64 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %7
%12 = getelementptr inbounds i8, i8* %0, i64 %9
%13 = load i8, i8* %12
%14 = add i64 %9, -1
%15 = getelementptr inbounds i8, i8* %0, i64 %14
store i8 %13, i8* %15
br label %16
16: ; preds = %7, %11, %3
%17 = phi i8* [ null, %3 ], [ %8, %11 ], [ %8, %7 ]
ret i8* %17
}