#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 */
typedef int u_int;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *savestr(register const char *str) {
  register u_int size;
  register char *p;
  static char *strptr = NULL;
  static u_int strsize = 0;
  size = strlen(str) + 1;
  if (size > strsize) {
    strsize = 1024;
    if (strsize < size)
      strsize = size;
    strptr = (char *)malloc(strsize);
    if (strptr == NULL) {
      fprintf(stderr, "savestr: malloc\n");
      exit(1);
    }
  }
  (void)strcpy(strptr, str);
  p = strptr;
  strptr += size;
  strsize -= size;
  return (p);
}


x86 assembly (clang, -O0):
<savestr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    0x2ee4(%rip),%eax
jle    11e0 <savestr+0x90>
movl   $0x400,0x2ed4(%rip)
mov    0x2ece(%rip),%eax
cmp    -0xc(%rbp),%eax
jge    119c <savestr+0x4c>
mov    -0xc(%rbp),%eax
mov    %eax,0x2ebc(%rip)
mov    0x2eb6(%rip),%edi
call   1060 <malloc@plt>
mov    %rax,0x2ea2(%rip)
cmpq   $0x0,0x2e9a(%rip)
jne    11db <savestr+0x8b>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11e0 <savestr+0x90>
mov    0x2e69(%rip),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2e59(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%ecx
mov    0x2e4b(%rip),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x2e3e(%rip)
mov    -0xc(%rbp),%ecx
mov    0x2e3d(%rip),%eax
sub    %ecx,%eax
mov    %eax,0x2e35(%rip)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<savestr>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <strlen@plt>
lea    0x1(%rax),%ebp
cmp    0x2ef4(%rip),%eax
jge    116f <savestr+0x1f>
mov    0x2ee3(%rip),%rdi
jmp    1196 <savestr+0x46>
cmp    $0x400,%eax
mov    $0x400,%edi
cmovge %ebp,%edi
mov    %edi,0x2ed6(%rip)
call   1060 <malloc@plt>
mov    %rax,%rdi
mov    %rax,0x2ebf(%rip)
test   %rax,%rax
je     11c0 <savestr+0x70>
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x2eab(%rip),%rax
movslq %ebp,%rcx
lea    (%rax,%rcx,1),%rdx
mov    %rdx,0x2e9d(%rip)
sub    %ecx,0x2e9f(%rip)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@savestr.strptr = internal global i8* null
@savestr.strsize = internal global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"savestr: malloc\0A\00"
define i8* @savestr(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 @strlen(i8* %5)
%7 = add nsw i32 %6, 1
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = load i32, i32* @savestr.strsize
%10 = icmp sgt i32 %8, %9
br i1 %10, label %11, label %28
11: ; preds = %1
store i32 1024, i32* @savestr.strsize
%12 = load i32, i32* @savestr.strsize
%13 = load i32, i32* %3
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = load i32, i32* %3
store i32 %16, i32* @savestr.strsize
br label %17
17: ; preds = %15, %11
%18 = load i32, i32* @savestr.strsize
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** @savestr.strptr
%21 = load i8*, i8** @savestr.strptr
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %17
br label %28
28: ; preds = %27, %1
%29 = load i8*, i8** @savestr.strptr
%30 = load i8*, i8** %2
%31 = call i32 @strcpy(i8* %29, i8* %30)
%32 = load i8*, i8** @savestr.strptr
store i8* %32, i8** %4
%33 = load i32, i32* %3
%34 = load i8*, i8** @savestr.strptr
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8* %36, i8** @savestr.strptr
%37 = load i32, i32* %3
%38 = load i32, i32* @savestr.strsize
%39 = sub nsw i32 %38, %37
store i32 %39, i32* @savestr.strsize
%40 = load i8*, i8** %4
ret i8* %40
}

LLVM IR (-O2):
@savestr.strptr = internal unnamed_addr global i8* null
@savestr.strsize = internal unnamed_addr global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"savestr: malloc\0A\00"
define i8* @savestr(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = load i32, i32* @savestr.strsize
%5 = icmp slt i32 %2, %4
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = load i8*, i8** @savestr.strptr
br label %18
8: ; preds = %1
%9 = icmp slt i32 %2, 1024
%10 = select i1 %9, i32 1024, i32 %3
store i32 %10, i32* @savestr.strsize
%11 = tail call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** @savestr.strptr
%13 = icmp eq i64 %11, 0
br i1 %13, label %14, label %18
14: ; preds = %8
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %6, %8
%19 = phi i8* [ %7, %6 ], [ %12, %8 ]
%20 = tail call i32 @strcpy(i8* %19, i8* %0)
%21 = load i8*, i8** @savestr.strptr
%22 = sext i32 %3 to i64
%23 = getelementptr inbounds i8, i8* %21, i64 %22
store i8* %23, i8** @savestr.strptr
%24 = load i32, i32* @savestr.strsize
%25 = sub nsw i32 %24, %3
store i32 %25, i32* @savestr.strsize
ret i8* %21
}