#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 */
size_t Position_to_index(size_t const);
size_t strlen(char *);
void remove_line_ending(char *line) {
  const size_t length = strlen(line);
  size_t end_index = Position_to_index(length);
  while (line[end_index] == '\n' || line[end_index] == '\r') {
    line[end_index] = '\0';
    if (end_index == 0) {
      break;
    }
    end_index -= 1;
  }
}


x86 assembly (clang, -O0):
<remove_line_ending>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <Position_to_index@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x1,%al
cmp    $0xa,%ecx
mov    %al,-0x19(%rbp)
je     1175 <remove_line_ending+0x55>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1185 <remove_line_ending+0x65>
jmp    11b2 <remove_line_ending+0x92>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x18(%rbp)
jne    11a1 <remove_line_ending+0x81>
jmp    11b2 <remove_line_ending+0x92>
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1146 <remove_line_ending+0x26>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remove_line_ending>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %rax,%rdi
call   1040 <Position_to_index@plt>
jmp    114a <remove_line_ending+0x2a>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movb   $0x0,(%rbx,%rax,1)
add    $0xffffffffffffffff,%rax
jae    1158 <remove_line_ending+0x38>
movzbl (%rbx,%rax,1),%ecx
cmp    $0xd,%cl
je     1140 <remove_line_ending+0x20>
cmp    $0xa,%cl
je     1140 <remove_line_ending+0x20>
pop    %rbx
ret

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

LLVM IR (-O2):
define void @remove_line_ending(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = tail call i64 @Position_to_index(i64 %2)
br label %4
4: ; preds = %8, %1
%5 = phi i64 [ %3, %1 ], [ %10, %8 ]
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = load i8, i8* %6
switch i8 %7, label %11 [
i8 10, label %8
i8 13, label %8
]
8: ; preds = %4, %4
store i8 0, i8* %6
%9 = icmp eq i64 %5, 0
%10 = add i64 %5, -1
br i1 %9, label %11, label %4
11: ; preds = %4, %8
ret void
}