#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 strip_trailing_zeros(char *buffer) {
  int length;
  int i;
  length = strlen(buffer);
  for (i = length - 1; i >= 0; i--) {
    if (buffer[i] != '0')
      break;
  }
  if (i < 0) {
    buffer[1] = '\0';
  } else {
    buffer[i + 1] = '\0';
  }
  return;
}


x86 assembly (clang, -O0):
<strip_trailing_zeros>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     1168 <strip_trailing_zeros+0x58>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x30,%eax
je     1155 <strip_trailing_zeros+0x45>
jmp    1168 <strip_trailing_zeros+0x58>
jmp    115a <strip_trailing_zeros+0x4a>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1131 <strip_trailing_zeros+0x21>
cmpl   $0x0,-0x10(%rbp)
jge    117f <strip_trailing_zeros+0x6f>
mov    -0x8(%rbp),%rax
movb   $0x0,0x1(%rax)
jmp    1190 <strip_trailing_zeros+0x80>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strip_trailing_zeros>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%ecx
nopl   0x0(%rax,%rax,1)
test   %ecx,%ecx
jle    1138 <strip_trailing_zeros+0x28>
lea    -0x1(%rcx),%rax
cmpb   $0x30,-0x1(%rbx,%rcx,1)
mov    %rax,%rcx
je     1120 <strip_trailing_zeros+0x10>
add    $0x1,%rax
jmp    113d <strip_trailing_zeros+0x2d>
mov    $0x1,%eax
movb   $0x0,(%rbx,%rax,1)
pop    %rbx
ret

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

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