#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(unsigned int);
int /*<<< orphan*/ strncpy(char *, char const *, unsigned int);
char *f77char2str(const char *fstr, unsigned int fstrlen) {
  char *s = malloc(fstrlen + 1);
  if (s) {
    int i;
    strncpy(s, fstr, fstrlen);
    s[fstrlen] = '\0';
    for (i = fstrlen - 1; i >= 0; i--) {
      if (s[i] == ' ') {
        s[i] = '\0';
      } else {
        break;
      }
    }
  }
  return s;
}


x86 assembly (clang, -O0):
<f77char2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ba <f77char2str+0x9a>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11b5 <f77char2str+0x95>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
jne    119d <f77char2str+0x7d>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11a2 <f77char2str+0x82>
jmp    11b5 <f77char2str+0x95>
jmp    11a7 <f77char2str+0x87>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    116d <f77char2str+0x4d>
jmp    11ba <f77char2str+0x9a>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<f77char2str>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r15
lea    0x1(%rbx),%edi
call   1040 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     1173 <f77char2str+0x53>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
add    $0xffffffff,%ebx
js     1173 <f77char2str+0x53>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%ebx
cmpb   $0x20,(%r14,%rbx,1)
jne    1173 <f77char2str+0x53>
movb   $0x0,(%r14,%rbx,1)
add    $0xffffffff,%ebx
jns    1160 <f77char2str+0x40>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @f77char2str(i8* %0, i32 %1) {
%3 = add i32 %1, 1
%4 = tail call i8* @malloc(i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %21, label %6
6: ; preds = %2
%7 = tail call i32 @strncpy(i8* %4, i8* %0, i32 %1)
%8 = zext i32 %1 to i64
%9 = getelementptr inbounds i8, i8* %4, i64 %8
store i8 0, i8* %9
%10 = add i32 %1, -1
%11 = icmp sgt i32 %10, -1
br i1 %11, label %12, label %21
12: ; preds = %6, %18
%13 = phi i32 [ %19, %18 ], [ %10, %6 ]
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %4, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 32
br i1 %17, label %18, label %21
18: ; preds = %12
store i8 0, i8* %15
%19 = add i32 %13, -1
%20 = icmp sgt i32 %19, -1
br i1 %20, label %12, label %21
21: ; preds = %12, %18, %6, %2
ret i8* %4
}