#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 strlen(char *);
char *rtrim_lines(char *str) {
  char *end;
  int len = strlen(str);
  while (*str && len) {
    end = str + len - 1;
    if (*end == '\r' || *end == '\n') {
      *end = '\0';
    } else {
      break;
    }
    len = strlen(str);
  }
  return str;
}


x86 assembly (clang, -O0):
<rtrim_lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x15(%rbp)
je     1147 <rtrim_lines+0x37>
cmpl   $0x0,-0x14(%rbp)
setne  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1157 <rtrim_lines+0x47>
jmp    11ac <rtrim_lines+0x9c>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xd,%eax
je     118a <rtrim_lines+0x7a>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    1196 <rtrim_lines+0x86>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    119b <rtrim_lines+0x8b>
jmp    11ac <rtrim_lines+0x9c>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
jmp    1128 <rtrim_lines+0x18>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtrim_lines>:
push   %rbx
mov    %rdi,%rbx
jmp    1128 <rtrim_lines+0x18>
cs nopw 0x0(%rax,%rax,1)
movb   $0x0,-0x1(%rbx,%rax,1)
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmpb   $0x0,(%rbx)
je     1147 <rtrim_lines+0x37>
test   %eax,%eax
je     1147 <rtrim_lines+0x37>
cltq
movzbl -0x1(%rbx,%rax,1),%ecx
cmp    $0xd,%cl
je     1120 <rtrim_lines+0x10>
cmp    $0xa,%cl
je     1120 <rtrim_lines+0x10>
mov    %rbx,%rax
pop    %rbx
ret

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

LLVM IR (-O2):
define i8* @rtrim_lines(i8* returned %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = load i8, i8* %0
%4 = icmp ne i8 %3, 0
%5 = icmp ne i32 %2, 0
%6 = select i1 %4, i1 %5, i1 false
br i1 %6, label %7, label %20
7: ; preds = %1
%8 = getelementptr inbounds i8, i8* %0, i64 -1
br label %9
9: ; preds = %7, %14
%10 = phi i32 [ %15, %14 ], [ %2, %7 ]
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %8, i64 %11
%13 = load i8, i8* %12
switch i8 %13, label %20 [
i8 13, label %14
i8 10, label %14
]
14: ; preds = %9, %9
store i8 0, i8* %12
%15 = tail call i32 @strlen(i8* %0)
%16 = load i8, i8* %0
%17 = icmp ne i8 %16, 0
%18 = icmp ne i32 %15, 0
%19 = select i1 %17, i1 %18, i1 false
br i1 %19, label %9, label %20
20: ; preds = %14, %9, %1
ret i8* %0
}