#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 */
scalar_t__ strlen(char *);
char *trim_line_ending(char *buffer) {
  if (*buffer != '\0') {
    int last;
    for (last = (int)strlen(buffer) - 1;
         last >= 0 && (buffer[last] == '\n' || buffer[last] == '\r'); --last) {
      buffer[last] = '\0';
    }
  }
  return buffer;
}


x86 assembly (clang, -O0):
<trim_line_ending>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11ae <trim_line_ending+0x9e>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0xd(%rbp)
jl     117f <trim_line_ending+0x6f>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x1,%al
cmp    $0xa,%ecx
mov    %al,-0xe(%rbp)
je     1179 <trim_line_ending+0x69>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
sete   %al
mov    %al,-0xe(%rbp)
mov    -0xe(%rbp),%al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    118f <trim_line_ending+0x7f>
jmp    11a9 <trim_line_ending+0x99>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    113b <trim_line_ending+0x2b>
jmp    11ae <trim_line_ending+0x9e>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim_line_ending>:
push   %rbx
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     114f <trim_line_ending+0x3f>
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    114f <trim_line_ending+0x3f>
mov    %eax,%eax
add    $0x1,%rax
jmp    113e <trim_line_ending+0x2e>
nopl   (%rax)
movb   $0x0,(%rbx,%rcx,1)
add    $0xffffffffffffffff,%rax
cmp    $0x1,%rax
jbe    114f <trim_line_ending+0x3f>
lea    -0x2(%rax),%ecx
movzbl (%rbx,%rcx,1),%edx
cmp    $0xd,%dl
je     1130 <trim_line_ending+0x20>
cmp    $0xa,%dl
je     1130 <trim_line_ending+0x20>
mov    %rbx,%rax
pop    %rbx
ret

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

LLVM IR (-O2):
define i8* @trim_line_ending(i8* returned %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %18, label %4
4: ; preds = %1
%5 = tail call i64 @strlen(i8* %0)
%6 = trunc i64 %5 to i32
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %18
8: ; preds = %4
%9 = and i64 %5, 4294967295
br label %10
10: ; preds = %8, %16
%11 = phi i64 [ %9, %8 ], [ %12, %16 ]
%12 = add nsw i64 %11, -1
%13 = and i64 %12, 4294967295
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
switch i8 %15, label %18 [
i8 10, label %16
i8 13, label %16
]
16: ; preds = %10, %10
store i8 0, i8* %14
%17 = icmp ugt i64 %11, 1
br i1 %17, label %10, label %18
18: ; preds = %10, %16, %4, %1
ret i8* %0
}