#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__ isdigit(char);
int strlen(char *);
char *right_trim_session(char *buffer) {
  char *buf_ptr;
  if (!buffer || !*buffer)
    return buffer;
  buf_ptr = buffer + strlen(buffer) - 1;
  while (*buf_ptr && isdigit(*buf_ptr))
    buf_ptr--;
  if (*buf_ptr == '_') {
    *(buf_ptr) = '\0';
  }
  return buffer;
}


x86 assembly (clang, -O0):
<right_trim_session>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1144 <right_trim_session+0x24>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1151 <right_trim_session+0x31>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e1 <right_trim_session+0xc1>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
je     11a1 <right_trim_session+0x81>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11b1 <right_trim_session+0x91>
jmp    11c2 <right_trim_session+0xa2>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1176 <right_trim_session+0x56>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5f,%eax
jne    11d9 <right_trim_session+0xb9>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<right_trim_session>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
test   %rdi,%rdi
je     1161 <right_trim_session+0x41>
cmpb   $0x0,(%r14)
je     1161 <right_trim_session+0x41>
mov    %r14,%rdi
call   1040 <strlen@plt>
movslq %eax,%rbx
add    %r14,%rbx
movzbl -0x1(%rbx),%eax
test   %al,%al
je     1161 <right_trim_session+0x41>
add    $0xffffffffffffffff,%rbx
movsbl %al,%edi
call   1030 <isdigit@plt>
test   %rax,%rax
jne    1140 <right_trim_session+0x20>
cmpb   $0x5f,(%rbx)
jne    1161 <right_trim_session+0x41>
movb   $0x0,(%rbx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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