#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__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
char *realloc(char *, scalar_t__);
scalar_t__ strlen(char *);
char *sfix(char *fstr, int size) {
  char *cstr = (char *)malloc(sizeof(char) * size);
  memcpy(cstr, fstr, size);
  char *end = cstr + size - 1;
  while (*(--end) == ' ')
    ;
  end++;
  *end = '\0';
  cstr = realloc(cstr, strlen(cstr) + 1);
  return cstr;
}


x86 assembly (clang, -O0):
<sfix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x20(%rbp)
movsbl -0x1(%rax),%eax
cmp    $0x20,%eax
jne    11a6 <sfix+0x66>
jmp    1185 <sfix+0x45>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sfix>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
movslq %esi,%rbx
mov    %ebx,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
mov    %r15,%rax
add    %rbx,%rax
nopl   0x0(%rax,%rax,1)
cmpb   $0x20,-0x2(%rax)
lea    -0x1(%rax),%rax
je     1170 <sfix+0x30>
movb   $0x0,(%rax)
mov    %r15,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%rsi
mov    %r15,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <realloc@plt>

LLVM IR (-O0):
define i8* @sfix(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = sext i32 %7 to i64
%9 = mul i64 1, %8
%10 = trunc i64 %9 to i32
%11 = call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = load i8*, i8** %3
%15 = load i32, i32* %4
%16 = call i32 @memcpy(i8* %13, i8* %14, i32 %15)
%17 = load i8*, i8** %5
%18 = load i32, i32* %4
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = getelementptr inbounds i8, i8* %20, i64 -1
store i8* %21, i8** %6
br label %22
22: ; preds = %28, %2
%23 = load i8*, i8** %6
%24 = getelementptr inbounds i8, i8* %23, i32 -1
store i8* %24, i8** %6
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 32
br i1 %27, label %28, label %29
28: ; preds = %22
br label %22
29: ; preds = %22
%30 = load i8*, i8** %6
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %6
%32 = load i8*, i8** %6
store i8 0, i8* %32
%33 = load i8*, i8** %5
%34 = load i8*, i8** %5
%35 = call i64 @strlen(i8* %34)
%36 = add nsw i64 %35, 1
%37 = call i8* @realloc(i8* %33, i64 %36)
store i8* %37, i8** %5
%38 = load i8*, i8** %5
ret i8* %38
}

LLVM IR (-O2):
define noalias i8* @sfix(i8* %0, i32 %1) {
%3 = sext i32 %1 to i64
%4 = tail call i64 @malloc(i32 %1)
%5 = inttoptr i64 %4 to i8*
%6 = tail call i32 @memcpy(i8* %5, i8* %0, i32 %1)
%7 = getelementptr inbounds i8, i8* %5, i64 %3
%8 = getelementptr inbounds i8, i8* %7, i64 -1
br label %9
9: ; preds = %9, %2
%10 = phi i8* [ %8, %2 ], [ %11, %9 ]
%11 = getelementptr inbounds i8, i8* %10, i64 -1
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 32
br i1 %13, label %9, label %14
14: ; preds = %9
store i8 0, i8* %10
%15 = tail call i64 @strlen(i8* dereferenceable(1) %5)
%16 = add nsw i64 %15, 1
%17 = tail call i8* @realloc(i8* %5, i64 %16)
ret i8* %17
}