#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_trailing_newline(char *s) {
  int i;
  i = strlen(s) - 1;
  while (s[i] == '\n' && i >= 0)
    s[i--] = 0;
}


x86 assembly (clang, -O0):
<remove_trailing_newline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0xd(%rbp)
jne    114f <remove_trailing_newline+0x3f>
cmpl   $0x0,-0xc(%rbp)
setge  %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    115f <remove_trailing_newline+0x4f>
jmp    117a <remove_trailing_newline+0x6a>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %ecx,%edx
add    $0xffffffff,%edx
mov    %edx,-0xc(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    112b <remove_trailing_newline+0x1b>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remove_trailing_newline>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
movslq %eax,%rcx
cmpb   $0xa,-0x1(%rcx,%rbx,1)
jne    115a <remove_trailing_newline+0x4a>
test   %eax,%eax
jle    115a <remove_trailing_newline+0x4a>
add    %rbx,%rcx
add    $0xffffffffffffffff,%rcx
mov    %eax,%eax
add    $0xfffffffffffffffe,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movb   $0x0,(%rcx)
cmpb   $0xa,(%rbx,%rax,1)
jne    115a <remove_trailing_newline+0x4a>
lea    (%rbx,%rax,1),%rcx
lea    -0x1(%rax),%rdx
cmp    $0x1,%rax
mov    %rdx,%rax
jne    1140 <remove_trailing_newline+0x30>
pop    %rbx
ret

LLVM IR (-O0):
define void @remove_trailing_newline(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i32 @strlen(i8* %4)
%6 = sub nsw i32 %5, 1
store i32 %6, i32* %3
br label %7
7: ; preds = %20, %1
%8 = load i8*, i8** %2
%9 = load i32, i32* %3
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %8, i64 %10
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 10
br i1 %14, label %15, label %18
15: ; preds = %7
%16 = load i32, i32* %3
%17 = icmp sge i32 %16, 0
br label %18
18: ; preds = %15, %7
%19 = phi i1 [ false, %7 ], [ %17, %15 ]
br i1 %19, label %20, label %26
20: ; preds = %18
%21 = load i8*, i8** %2
%22 = load i32, i32* %3
%23 = add nsw i32 %22, -1
store i32 %23, i32* %3
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
store i8 0, i8* %25
br label %7
26: ; preds = %18
ret void
}

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