#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 */
char *sld_string_allocate(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *sld_string_chop(char *string) {
  char *chopped_string = NULL;
  int length = strlen(string);
  chopped_string = sld_string_allocate(string);
  strcpy(chopped_string, string);
  if (length == 0 || length == 1)
    chopped_string = "";
  else {
    if (chopped_string[length - 1] == '\n' &&
        chopped_string[length - 2] == '\r')
      chopped_string[length - 2] = '\0';
    else
      chopped_string[length - 1] = '\0';
  }
  return chopped_string;
}


x86 assembly (clang, -O0):
<sld_string_chop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <sld_string_allocate@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
cmpl   $0x0,-0x14(%rbp)
je     117e <sld_string_chop+0x4e>
cmpl   $0x1,-0x14(%rbp)
jne    118e <sld_string_chop+0x5e>
lea    0xe7b(%rip),%rax
mov    %rax,-0x10(%rbp)
jmp    11ee <sld_string_chop+0xbe>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11d8 <sld_string_chop+0xa8>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    11d8 <sld_string_chop+0xa8>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11e9 <sld_string_chop+0xb9>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11ee <sld_string_chop+0xbe>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sld_string_chop>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbp
call   1050 <strlen@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1040 <sld_string_allocate@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
cmp    $0x2,%r14d
jae    1164 <sld_string_chop+0x34>
lea    0xe9e(%rip),%rbx
jmp    1188 <sld_string_chop+0x58>
movslq %r14d,%rax
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    117e <sld_string_chop+0x4e>
cmpb   $0xd,-0x2(%rax,%rbx,1)
jne    117e <sld_string_chop+0x4e>
add    %rbx,%rax
add    $0xfffffffffffffffe,%rax
jmp    1185 <sld_string_chop+0x55>
add    %rbx,%rax
add    $0xffffffffffffffff,%rax
movb   $0x0,(%rax)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @sld_string_chop(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = tail call i8* @sld_string_allocate(i8* %0)
%4 = tail call i32 @strcpy(i8* %3, i8* %0)
%5 = icmp ult i32 %2, 2
br i1 %5, label %21, label %6
6: ; preds = %1
%7 = add nsw i32 %2, -1
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %3, i64 %8
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 10
br i1 %11, label %12, label %18
12: ; preds = %6
%13 = add nsw i32 %2, -2
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %3, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 13
br i1 %17, label %19, label %18
18: ; preds = %12, %6
br label %19
19: ; preds = %12, %18
%20 = phi i8* [ %9, %18 ], [ %15, %12 ]
store i8 0, i8* %20
br label %21
21: ; preds = %19, %1
%22 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %1 ], [ %3, %19 ]
ret i8* %22
}