#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 *string_trim_right(char *buffer, int length) {
  int str_len;
  if (!buffer || !*buffer)
    return (char *)0;
  str_len = strlen(buffer);
  if (str_len < length)
    return buffer;
  buffer[str_len - length] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<string_trim_right>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1137 <string_trim_right+0x27>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1144 <string_trim_right+0x34>
movq   $0x0,-0x8(%rbp)
jmp    1182 <string_trim_right+0x72>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1169 <string_trim_right+0x59>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1182 <string_trim_right+0x72>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    -0x14(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_trim_right>:
push   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
je     1136 <string_trim_right+0x26>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     1136 <string_trim_right+0x26>
mov    %esi,%ebp
mov    %rbx,%rdi
call   1030 <strlen@plt>
sub    %ebp,%eax
jl     1138 <string_trim_right+0x28>
cltq
movb   $0x0,(%rbx,%rax,1)
jmp    1138 <string_trim_right+0x28>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i8* @string_trim_right(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i8*, i8** %4
%11 = load i8, i8* %10
%12 = icmp ne i8 %11, 0
br i1 %12, label %14, label %13
13: ; preds = %9, %2
store i8* null, i8** %3
br label %30
14: ; preds = %9
%15 = load i8*, i8** %4
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = load i32, i32* %5
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = load i8*, i8** %4
store i8* %21, i8** %3
br label %30
22: ; preds = %14
%23 = load i8*, i8** %4
%24 = load i32, i32* %6
%25 = load i32, i32* %5
%26 = sub nsw i32 %24, %25
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %23, i64 %27
store i8 0, i8* %28
%29 = load i8*, i8** %4
store i8* %29, i8** %3
br label %30
30: ; preds = %22, %20, %13
%31 = load i8*, i8** %3
ret i8* %31
}

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