#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__ iscntrl(char);
int strlen(char *);
int chomp(char *string) {
  int position;
  for (position = strlen(string); position >= 0; position--) {
    if (iscntrl(string[position])) {
      string[position] = '\0';
    } else {
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<chomp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1186 <chomp+0x66>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <iscntrl@plt>
cmp    $0x0,%rax
je     116e <chomp+0x4e>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1173 <chomp+0x53>
jmp    1186 <chomp+0x66>
jmp    1178 <chomp+0x58>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1138 <chomp+0x18>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chomp>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
test   %eax,%eax
js     115c <chomp+0x3c>
mov    %eax,%ebx
add    $0x1,%rbx
cs nopw 0x0(%rax,%rax,1)
movsbl -0x1(%r14,%rbx,1),%edi
call   1030 <iscntrl@plt>
test   %rax,%rax
je     115c <chomp+0x3c>
movb   $0x0,-0x1(%r14,%rbx,1)
add    $0xffffffffffffffff,%rbx
jg     1140 <chomp+0x20>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @chomp(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
br label %6
6: ; preds = %24, %1
%7 = load i32, i32* %3
%8 = icmp sge i32 %7, 0
br i1 %8, label %9, label %27
9: ; preds = %6
%10 = load i8*, i8** %2
%11 = load i32, i32* %3
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
%14 = load i8, i8* %13
%15 = call i64 @iscntrl(i8 signext %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %9
%18 = load i8*, i8** %2
%19 = load i32, i32* %3
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 0, i8* %21
br label %23
22: ; preds = %9
br label %27
23: ; preds = %17
br label %24
24: ; preds = %23
%25 = load i32, i32* %3
%26 = add nsw i32 %25, -1
store i32 %26, i32* %3
br label %6
27: ; preds = %22, %6
ret i32 0
}

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