#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 /*<<< orphan*/ cleanFortranString(char *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, char *, int);
char *mallocNCopyMcfio(char *string, int length) {
  char *str;
  if (string == NULL)
    return NULL;
  if (length < 0) {
    fprintf(
        stderr,
        "hs: Error. Character argument has negative length, setting to null\n");
    length = 0;
  }
  str = malloc(length + 1);
  if (length > 0)
    strncpy(str, string, length);
  str[length] = '\0';
  cleanFortranString(str, length);
  return str;
}


x86 assembly (clang, -O0):
<mallocNCopyMcfio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1167 <mallocNCopyMcfio+0x27>
movq   $0x0,-0x8(%rbp)
jmp    11d6 <mallocNCopyMcfio+0x96>
cmpl   $0x0,-0x14(%rbp)
jge    118d <mallocNCopyMcfio+0x4d>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
lea    0xe7f(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11b6 <mallocNCopyMcfio+0x76>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <cleanFortranString@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mallocNCopyMcfio>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1171 <mallocNCopyMcfio+0x31>
mov    %esi,%ebx
test   %esi,%esi
js     1176 <mallocNCopyMcfio+0x36>
mov    %rdi,%r15
lea    0x1(%rbx),%edi
call   1050 <malloc@plt>
mov    %rax,%r14
test   %ebx,%ebx
je     1198 <mallocNCopyMcfio+0x58>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
jmp    119a <mallocNCopyMcfio+0x5a>
xor    %r14d,%r14d
jmp    11ab <mallocNCopyMcfio+0x6b>
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
lea    0xe7a(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
xor    %ebx,%ebx
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rdi
mov    %ebx,%esi
call   1060 <cleanFortranString@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [68 x i8] c"hs: Error. Character argument has negative length, setting to null\0A\00"
define i8* @mallocNCopyMcfio(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %2
store i8* null, i8** %3
br label %36
10: ; preds = %2
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %5
br label %16
16: ; preds = %13, %10
%17 = load i32, i32* %5
%18 = add nsw i32 %17, 1
%19 = call i8* @malloc(i32 %18)
store i8* %19, i8** %6
%20 = load i32, i32* %5
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %16
%23 = load i8*, i8** %6
%24 = load i8*, i8** %4
%25 = load i32, i32* %5
%26 = call i32 @strncpy(i8* %23, i8* %24, i32 %25)
br label %27
27: ; preds = %22, %16
%28 = load i8*, i8** %6
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 0, i8* %31
%32 = load i8*, i8** %6
%33 = load i32, i32* %5
%34 = call i32 @cleanFortranString(i8* %32, i32 %33)
%35 = load i8*, i8** %6
store i8* %35, i8** %3
br label %36
36: ; preds = %27, %9
%37 = load i8*, i8** %3
ret i8* %37
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [68 x i8] c"hs: Error. Character argument has negative length, setting to null\0A\00"
define i8* @mallocNCopyMcfio(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %22, label %4
4: ; preds = %2
%5 = icmp slt i32 %1, 0
br i1 %5, label %6, label %10
6: ; preds = %4
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str, i64 0, i64 0))
%9 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
br label %16
10: ; preds = %4
%11 = add nuw nsw i32 %1, 1
%12 = tail call i8* @malloc(i32 %11)
%13 = icmp eq i32 %1, 0
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = tail call i32 @strncpy(i8* %12, i8* %0, i32 %1)
br label %16
16: ; preds = %6, %14, %10
%17 = phi i8* [ %9, %6 ], [ %12, %14 ], [ %12, %10 ]
%18 = phi i32 [ 0, %6 ], [ %1, %14 ], [ 0, %10 ]
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
store i8 0, i8* %20
%21 = tail call i32 @cleanFortranString(i8* %17, i32 %18)
br label %22
22: ; preds = %2, %16
%23 = phi i8* [ %17, %16 ], [ null, %2 ]
ret i8* %23
}