#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*/ fprintf(int /*<<< orphan*/, char *, char *);
char *malloc(int);
int /*<<< orphan*/ stderr;
int strlen(char *);
char *newstring(char *text, int len) {
  char *s, *s2;
  if (text == NULL)
    return NULL;
  if (len < 0) {
    fprintf(stderr, "warning: negative length string '%s'\n", text);
    len = 0;
  }
  if (len == 0)
    len = strlen(text);
  s = malloc(len + 1);
  if (s == NULL) {
    fprintf(stderr, "can't alloc memory for string '%s'\n", text);
    return NULL;
  }
  s2 = s;
  while ((*s++ = *text++))
    ;
  return s2;
}


x86 assembly (clang, -O0):
<newstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1157 <newstring+0x27>
movq   $0x0,-0x8(%rbp)
jmp    1216 <newstring+0xe6>
cmpl   $0x0,-0x14(%rbp)
jge    1181 <newstring+0x51>
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe8b(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1197 <newstring+0x67>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d7 <newstring+0xa7>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe61(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1216 <newstring+0xe6>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %al,(%rcx)
cmp    $0x0,%al
je     120e <newstring+0xde>
jmp    11df <newstring+0xaf>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<newstring>:
push   %rbx
test   %rdi,%rdi
je     11a9 <newstring+0x79>
mov    %rdi,%rbx
test   %esi,%esi
js     1141 <newstring+0x11>
jne    1163 <newstring+0x33>
jmp    1159 <newstring+0x29>
mov    0x2e98(%rip),%rax
mov    (%rax),%edi
lea    0xeaf(%rip),%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%esi
add    $0x1,%esi
mov    %esi,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1191 <newstring+0x61>
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl (%rbx,%rcx,1),%edx
mov    %dl,(%rax,%rcx,1)
add    $0x1,%rcx
test   %dl,%dl
jne    1180 <newstring+0x50>
pop    %rbx
ret
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0xe85(%rip),%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [38 x i8] c"warning: negative length string '%s'\0A\00"
@.str.1 = [36 x i8] c"can't alloc memory for string '%s'\0A\00"
define i8* @newstring(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %2
store i8* null, i8** %3
br label %46
11: ; preds = %2
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %4
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 0, i32* %5
br label %18
18: ; preds = %14, %11
%19 = load i32, i32* %5
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i8*, i8** %4
%23 = call i32 @strlen(i8* %22)
store i32 %23, i32* %5
br label %24
24: ; preds = %21, %18
%25 = load i32, i32* %5
%26 = add nsw i32 %25, 1
%27 = call i8* @malloc(i32 %26)
store i8* %27, i8** %6
%28 = load i8*, i8** %6
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %34
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %4
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %32)
store i8* null, i8** %3
br label %46
34: ; preds = %24
%35 = load i8*, i8** %6
store i8* %35, i8** %7
br label %36
36: ; preds = %43, %34
%37 = load i8*, i8** %4
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %4
%39 = load i8, i8* %37
%40 = load i8*, i8** %6
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %6
store i8 %39, i8* %40
%42 = icmp ne i8 %39, 0
br i1 %42, label %43, label %44
43: ; preds = %36
br label %36
44: ; preds = %36
%45 = load i8*, i8** %7
store i8* %45, i8** %3
br label %46
46: ; preds = %44, %30, %10
%47 = load i8*, i8** %3
ret i8* %47
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [38 x i8] c"warning: negative length string '%s'\0A\00"
@.str.1 = [36 x i8] c"can't alloc memory for string '%s'\0A\00"
define noalias i8* @newstring(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %28, label %4
4: ; preds = %2
%5 = icmp slt i32 %1, 0
br i1 %5, label %6, label %9
6: ; preds = %4
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %11
9: ; preds = %4
%10 = icmp eq i32 %1, 0
br i1 %10, label %11, label %13
11: ; preds = %6, %9
%12 = tail call i32 @strlen(i8* %0)
br label %13
13: ; preds = %11, %9
%14 = phi i32 [ %12, %11 ], [ %1, %9 ]
%15 = add nsw i32 %14, 1
%16 = tail call i8* @malloc(i32 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %28
21: ; preds = %13, %21
%22 = phi i8* [ %24, %21 ], [ %0, %13 ]
%23 = phi i8* [ %26, %21 ], [ %16, %13 ]
%24 = getelementptr inbounds i8, i8* %22, i64 1
%25 = load i8, i8* %22
%26 = getelementptr inbounds i8, i8* %23, i64 1
store i8 %25, i8* %23
%27 = icmp eq i8 %25, 0
br i1 %27, label %28, label %21
28: ; preds = %21, %2, %18
%29 = phi i8* [ null, %18 ], [ null, %2 ], [ %16, %21 ]
ret i8* %29
}