#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 cmn_getline(int /*<<< orphan*/, char **);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
scalar_t__ isspace(char);
char *malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int main(void) {
  char *line = NULL;
  char *trimed_line = NULL;
  int retval = 0, i, trimed = 0;
  while ((retval = cmn_getline(stdin, &line)) >= 0) {
    trimed_line = malloc(retval * sizeof(char) + sizeof(char));
    memset(trimed_line, '\0', retval * sizeof(char) + sizeof(char));
    for (i = retval - 1; i >= 0; i--) {
      if ((trimed == 1) || isspace(line[i]) == 0) {
        trimed = 1;
        trimed_line[i] = line[i];
      }
    }
    printf("%s\n", trimed_line);
    free(trimed_line);
    free(line);
    trimed_line = NULL;
    line = NULL;
    trimed = 0;
  }
  if (retval == -1) {
    return 0;
  } else {
    fputs("rtrim: malloc/realloc error\n", stderr);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1090 <cmn_getline@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jl     1291 <main+0x121>
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     1253 <main+0xe3>
cmpl   $0x1,-0x24(%rbp)
je     1223 <main+0xb3>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1070 <isspace@plt>
cmp    $0x0,%rax
jne    1240 <main+0xd0>
movl   $0x1,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1245 <main+0xd5>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    11f4 <main+0x84>
mov    -0x18(%rbp),%rsi
lea    0xda2(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
jmp    119d <main+0x2d>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12a7 <main+0x137>
movl   $0x0,-0x4(%rbp)
jmp    12c3 <main+0x153>
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
lea    0xd4d(%rip),%rdi
call   1050 <fputs@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movq   $0x0,(%rsp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1090 <cmn_getline@plt>
mov    %eax,%r12d
test   %eax,%eax
js     1263 <main+0xf3>
lea    0xe5a(%rip),%r14
mov    %rsp,%r15
jmp    11ec <main+0x7c>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r13,%rsi
call   1040 <printf@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
movq   $0x0,(%rsp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
call   1090 <cmn_getline@plt>
mov    %eax,%r12d
test   %eax,%eax
js     1263 <main+0xf3>
lea    0x1(%r12),%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1060 <memset@plt>
test   %r12d,%r12d
je     11b0 <main+0x40>
mov    %r12d,%ebp
add    $0x1,%rbp
xor    %ecx,%ecx
jmp    1242 <main+0xd2>
nopw   0x0(%rax,%rax,1)
mov    %eax,%ebx
mov    (%rsp),%rax
movzbl (%rax,%rbx,1),%eax
mov    %al,0x0(%r13,%rbx,1)
mov    $0x1,%ecx
add    $0xffffffffffffffff,%rbp
cmp    $0x1,%rbp
jbe    11b0 <main+0x40>
lea    -0x2(%rbp),%rax
cmp    $0x1,%ecx
je     1220 <main+0xb0>
mov    (%rsp),%rcx
mov    %eax,%ebx
movsbl (%rcx,%rbx,1),%edi
call   1070 <isspace@plt>
xor    %ecx,%ecx
test   %rax,%rax
jne    1234 <main+0xc4>
jmp    1222 <main+0xb2>
cmp    $0xffffffff,%r12d
je     1285 <main+0x115>
mov    0x2d68(%rip),%rax
mov    (%rax),%esi
lea    0xd8b(%rip),%rdi
call   1050 <fputs@plt>
mov    $0x1,%eax
jmp    1287 <main+0x117>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [29 x i8] c"rtrim: malloc/realloc error\0A\00"
@stderr = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i8* null, i8** %2
store i8* null, i8** %3
store i32 0, i32* %4
store i32 0, i32* %6
br label %7
7: ; preds = %55, %0
%8 = load i32, i32* @stdin
%9 = call i32 @cmn_getline(i32 %8, i8** %2)
store i32 %9, i32* %4
%10 = icmp sge i32 %9, 0
br i1 %10, label %11, label %62
11: ; preds = %7
%12 = load i32, i32* %4
%13 = sext i32 %12 to i64
%14 = mul i64 %13, 1
%15 = add i64 %14, 1
%16 = trunc i64 %15 to i32
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %3
%18 = load i8*, i8** %3
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = mul i64 %20, 1
%22 = add i64 %21, 1
%23 = trunc i64 %22 to i32
%24 = call i32 @memset(i8* %18, i8 signext 0, i32 %23)
%25 = load i32, i32* %4
%26 = sub nsw i32 %25, 1
store i32 %26, i32* %5
br label %27
27: ; preds = %52, %11
%28 = load i32, i32* %5
%29 = icmp sge i32 %28, 0
br i1 %29, label %30, label %55
30: ; preds = %27
%31 = load i32, i32* %6
%32 = icmp eq i32 %31, 1
br i1 %32, label %41, label %33
33: ; preds = %30
%34 = load i8*, i8** %2
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i8, i8* %37
%39 = call i64 @isspace(i8 signext %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %51
41: ; preds = %33, %30
store i32 1, i32* %6
%42 = load i8*, i8** %2
%43 = load i32, i32* %5
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8, i8* %45
%47 = load i8*, i8** %3
%48 = load i32, i32* %5
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 %46, i8* %50
br label %51
51: ; preds = %41, %33
br label %52
52: ; preds = %51
%53 = load i32, i32* %5
%54 = add nsw i32 %53, -1
store i32 %54, i32* %5
br label %27
55: ; preds = %27
%56 = load i8*, i8** %3
%57 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %56)
%58 = load i8*, i8** %3
%59 = call i32 @free(i8* %58)
%60 = load i8*, i8** %2
%61 = call i32 @free(i8* %60)
store i8* null, i8** %3
store i8* null, i8** %2
store i32 0, i32* %6
br label %7
62: ; preds = %7
%63 = load i32, i32* %4
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %66
65: ; preds = %62
store i32 0, i32* %1
br label %69
66: ; preds = %62
%67 = load i32, i32* @stderr
%68 = call i32 @fputs(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %67)
store i32 1, i32* %1
br label %69
69: ; preds = %66, %65
%70 = load i32, i32* %1
ret i32 %70
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [29 x i8] c"rtrim: malloc/realloc error\0A\00"
@stderr = global i32 0
define i32 @main() {
%1 = alloca i8*
%2 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%3 = load i32, i32* @stdin
%4 = call i32 @cmn_getline(i32 %3, i8** %1)
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %45
6: ; preds = %0, %37
%7 = phi i32 [ %43, %37 ], [ %4, %0 ]
%8 = add nuw i32 %7, 1
%9 = call i8* @malloc(i32 %8)
%10 = call i32 @memset(i8* %9, i8 signext 0, i32 %8)
%11 = icmp eq i32 %7, 0
br i1 %11, label %37, label %12
12: ; preds = %6
%13 = zext i32 %7 to i64
br label %14
14: ; preds = %12, %34
%15 = phi i64 [ %13, %12 ], [ %17, %34 ]
%16 = phi i32 [ 0, %12 ], [ %35, %34 ]
%17 = add nsw i64 %15, -1
%18 = icmp eq i32 %16, 1
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = and i64 %17, 4294967295
br label %28
21: ; preds = %14
%22 = load i8*, i8** %1
%23 = and i64 %17, 4294967295
%24 = getelementptr inbounds i8, i8* %22, i64 %23
%25 = load i8, i8* %24
%26 = call i64 @isspace(i8 signext %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %19, %21
%29 = phi i64 [ %20, %19 ], [ %23, %21 ]
%30 = load i8*, i8** %1
%31 = getelementptr inbounds i8, i8* %30, i64 %29
%32 = load i8, i8* %31
%33 = getelementptr inbounds i8, i8* %9, i64 %29
store i8 %32, i8* %33
br label %34
34: ; preds = %21, %28
%35 = phi i32 [ 1, %28 ], [ 0, %21 ]
%36 = icmp ugt i64 %15, 1
br i1 %36, label %14, label %37
37: ; preds = %34, %6
%38 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %9)
%39 = call i32 @free(i8* %9)
%40 = load i8*, i8** %1
%41 = call i32 @free(i8* %40)
store i8* null, i8** %1
%42 = load i32, i32* @stdin
%43 = call i32 @cmn_getline(i32 %42, i8** %1)
%44 = icmp sgt i32 %43, -1
br i1 %44, label %6, label %45
45: ; preds = %37, %0
%46 = phi i32 [ %4, %0 ], [ %43, %37 ]
%47 = icmp eq i32 %46, -1
br i1 %47, label %51, label %48
48: ; preds = %45
%49 = load i32, i32* @stderr
%50 = call i32 @fputs(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %45, %48
%52 = phi i32 [ 1, %48 ], [ 0, %45 ]
ret i32 %52
}