#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 /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int strlen(char *);
char *chomp(char *str) {
  char *p;
  if (!str || strlen(str) < 1) {
    errno = EINVAL;
    return NULL;
  }
  p = str + strlen(str) - 1;
  while (*p == '\n' || *p == '\r')
    *p-- = 0;
  return str;
}


x86 assembly (clang, -O0):
<chomp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1139 <chomp+0x29>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x1,%eax
jge    1158 <chomp+0x48>
mov    0x2e98(%rip),%rax
mov    (%rax),%ecx
mov    0x2e97(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11d1 <chomp+0xc1>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <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
mov    $0x1,%al
cmp    $0xa,%ecx
mov    %al,-0x21(%rbp)
je     11a2 <chomp+0x92>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xd,%eax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11b2 <chomp+0xa2>
jmp    11c9 <chomp+0xb9>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x0,(%rax)
jmp    117d <chomp+0x6d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chomp>:
push   %rbx
test   %rdi,%rdi
je     1156 <chomp+0x46>
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %eax,%eax
jle    1156 <chomp+0x46>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
add    %rbx,%rax
add    $0xffffffffffffffff,%rax
jmp    1147 <chomp+0x37>
cs nopw 0x0(%rax,%rax,1)
nop
movb   $0x0,(%rax)
add    $0xffffffffffffffff,%rax
movzbl (%rax),%ecx
cmp    $0xd,%cl
je     1140 <chomp+0x30>
cmp    $0xa,%cl
je     1140 <chomp+0x30>
jmp    116a <chomp+0x5a>
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    0x2e7a(%rip),%rcx
mov    %eax,(%rcx)
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
define i8* @chomp(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 = call i32 @strlen(i8* %8)
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %7, %1
%12 = load i32, i32* @EINVAL
store i32 %12, i32* @errno
store i8* null, i8** %2
br label %37
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 eq i32 %23, 10
br i1 %24, label %30, label %25
25: ; preds = %20
%26 = load i8*, i8** %4
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 13
br label %30
30: ; preds = %25, %20
%31 = phi i1 [ true, %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
store i8 0, i8* %33
br label %20
35: ; preds = %30
%36 = load i8*, i8** %3
store i8* %36, i8** %2
br label %37
37: ; preds = %35, %11
%38 = load i8*, i8** %2
ret i8* %38
}

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