#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 *malloc(int);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *stripNewline(const char *str) {
  char *noNewlineStr = malloc(strlen(str) * sizeof(char));
  strcpy(noNewlineStr, str);
  size_t len = strlen(noNewlineStr);
  if (len > 0 && noNewlineStr[len - 1] == '\n')
    noNewlineStr[--len] = '\0';
  return noNewlineStr;
}


x86 assembly (clang, -O0):
<stripNewline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cltq
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11ae <stripNewline+0x7e>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11ae <stripNewline+0x7e>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x18(%rbp)
movb   $0x0,-0x1(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stripNewline>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
je     116b <stripNewline+0x3b>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    116b <stripNewline+0x3b>
movb   $0x0,-0x1(%rbx,%rax,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @stripNewline(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 @strlen(i8* %5)
%7 = sext i32 %6 to i64
%8 = mul i64 %7, 1
%9 = trunc i64 %8 to i32
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %3
%11 = load i8*, i8** %3
%12 = load i8*, i8** %2
%13 = call i32 @strcpy(i8* %11, i8* %12)
%14 = load i8*, i8** %3
%15 = call i32 @strlen(i8* %14)
%16 = sext i32 %15 to i64
store i64 %16, i64* %4
%17 = load i64, i64* %4
%18 = icmp ugt i64 %17, 0
br i1 %18, label %19, label %32
19: ; preds = %1
%20 = load i8*, i8** %3
%21 = load i64, i64* %4
%22 = sub i64 %21, 1
%23 = getelementptr inbounds i8, i8* %20, 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 %32
27: ; preds = %19
%28 = load i8*, i8** %3
%29 = load i64, i64* %4
%30 = add i64 %29, -1
store i64 %30, i64* %4
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 0, i8* %31
br label %32
32: ; preds = %27, %19, %1
%33 = load i8*, i8** %3
ret i8* %33
}

LLVM IR (-O2):
define i8* @stripNewline(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = tail call i8* @malloc(i32 %2)
%4 = tail call i32 @strcpy(i8* %3, i8* %0)
%5 = tail call i32 @strlen(i8* %3)
%6 = icmp eq i32 %5, 0
br i1 %6, label %14, label %7
7: ; preds = %1
%8 = sext i32 %5 to i64
%9 = add nsw i64 %8, -1
%10 = getelementptr inbounds i8, i8* %3, i64 %9
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 10
br i1 %12, label %13, label %14
13: ; preds = %7
store i8 0, i8* %10
br label %14
14: ; preds = %13, %7, %1
ret i8* %3
}