#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 *);
void remove_ending_nl(char *str) {
  int length = strlen(str);
  if (length == 0) {
    return;
  }
  while ((str[length] == 10) || (str[length] == 13)) {
    str[length] = 0;
    --length;
    if (length == 0) {
      break;
    }
  }
  if ((str[0] == 10) || (str[0] == 13)) {
    str[0] = 0;
  }
}


x86 assembly (clang, -O0):
<remove_ending_nl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1137 <remove_ending_nl+0x27>
jmp    11cb <remove_ending_nl+0xbb>
jmp    113c <remove_ending_nl+0x2c>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x1,%al
cmp    $0xa,%ecx
mov    %al,-0xd(%rbp)
je     116b <remove_ending_nl+0x5b>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
sete   %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    117b <remove_ending_nl+0x6b>
jmp    11a4 <remove_ending_nl+0x94>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    119f <remove_ending_nl+0x8f>
jmp    11a4 <remove_ending_nl+0x94>
jmp    113c <remove_ending_nl+0x2c>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     11c4 <remove_ending_nl+0xb4>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xd,%eax
jne    11cb <remove_ending_nl+0xbb>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remove_ending_nl>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
test   %eax,%eax
je     1159 <remove_ending_nl+0x49>
movslq %eax,%rcx
add    %r14,%rcx
neg    %eax
xor    %edx,%edx
jmp    113c <remove_ending_nl+0x2c>
nopl   0x0(%rax)
movb   $0x0,(%rcx,%rdx,1)
add    $0xffffffffffffffff,%rdx
cmp    %edx,%eax
je     114a <remove_ending_nl+0x3a>
movzbl (%rcx,%rdx,1),%ebx
cmp    $0xd,%bl
je     1130 <remove_ending_nl+0x20>
cmp    $0xa,%bl
je     1130 <remove_ending_nl+0x20>
mov    (%r14),%al
cmp    $0xd,%al
je     1155 <remove_ending_nl+0x45>
cmp    $0xa,%al
jne    1159 <remove_ending_nl+0x49>
movb   $0x0,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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