#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*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *copy_to_scratch(char *s, int len) {
  static char *buf = NULL;
  static int buflen = 0;
  if (len > buflen) {
    if (buf)
      free(buf);
    buflen = (len < 40) ? 80 : (len * 2);
    buf = (char *)malloc(buflen + 1);
  }
  if (len > 0)
    strncpy(buf, s, len);
  else
    len = 0;
  buf[len] = '\0';
  return (buf);
}


x86 assembly (clang, -O0):
<copy_to_scratch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    0x2f00(%rip),%eax
jle    11a6 <copy_to_scratch+0x76>
cmpq   $0x0,0x2eea(%rip)
je     1168 <copy_to_scratch+0x38>
mov    0x2edd(%rip),%rdi
call   1030 <free@plt>
cmpl   $0x28,-0xc(%rbp)
jge    117f <copy_to_scratch+0x4f>
mov    $0x50,%eax
mov    %eax,-0x10(%rbp)
jmp    1188 <copy_to_scratch+0x58>
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,0x2eb7(%rip)
mov    0x2eb1(%rip),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,0x2e9a(%rip)
cmpl   $0x0,-0xc(%rbp)
jle    11c8 <copy_to_scratch+0x98>
mov    0x2e89(%rip),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <strncpy@plt>
jmp    11cf <copy_to_scratch+0x9f>
movl   $0x0,-0xc(%rbp)
mov    0x2e6a(%rip),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2e5b(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_to_scratch>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
cmp    %esi,0x2f09(%rip)
jge    1175 <copy_to_scratch+0x45>
mov    0x2ef8(%rip),%rdi
test   %rdi,%rdi
je     1152 <copy_to_scratch+0x22>
call   1030 <free@plt>
lea    (%rbx,%rbx,1),%eax
cmp    $0x28,%ebx
mov    $0x50,%edi
cmovge %eax,%edi
mov    %edi,0x2ee2(%rip)
or     $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,0x2ecb(%rip)
test   %ebx,%ebx
jle    118e <copy_to_scratch+0x5e>
mov    0x2ec0(%rip),%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <strncpy@plt>
mov    %ebx,%ecx
jmp    1190 <copy_to_scratch+0x60>
xor    %ecx,%ecx
mov    0x2ea9(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@copy_to_scratch.buf = internal global i8* null
@copy_to_scratch.buflen = internal global i32 0
define i8* @copy_to_scratch(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%5 = load i32, i32* %4
%6 = load i32, i32* @copy_to_scratch.buflen
%7 = icmp sgt i32 %5, %6
br i1 %7, label %8, label %27
8: ; preds = %2
%9 = load i8*, i8** @copy_to_scratch.buf
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = load i8*, i8** @copy_to_scratch.buf
%13 = call i32 @free(i8* %12)
br label %14
14: ; preds = %11, %8
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 40
br i1 %16, label %17, label %18
17: ; preds = %14
br label %21
18: ; preds = %14
%19 = load i32, i32* %4
%20 = mul nsw i32 %19, 2
br label %21
21: ; preds = %18, %17
%22 = phi i32 [ 80, %17 ], [ %20, %18 ]
store i32 %22, i32* @copy_to_scratch.buflen
%23 = load i32, i32* @copy_to_scratch.buflen
%24 = add nsw i32 %23, 1
%25 = call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** @copy_to_scratch.buf
br label %27
27: ; preds = %21, %2
%28 = load i32, i32* %4
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i8*, i8** @copy_to_scratch.buf
%32 = load i8*, i8** %3
%33 = load i32, i32* %4
%34 = call i32 @strncpy(i8* %31, i8* %32, i32 %33)
br label %36
35: ; preds = %27
store i32 0, i32* %4
br label %36
36: ; preds = %35, %30
%37 = load i8*, i8** @copy_to_scratch.buf
%38 = load i32, i32* %4
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** @copy_to_scratch.buf
ret i8* %41
}

LLVM IR (-O2):
@copy_to_scratch.buf = internal unnamed_addr global i8* null
@copy_to_scratch.buflen = internal unnamed_addr global i32 0
define i8* @copy_to_scratch(i8* %0, i32 %1) {
%3 = load i32, i32* @copy_to_scratch.buflen
%4 = icmp slt i32 %3, %1
br i1 %4, label %5, label %17
5: ; preds = %2
%6 = load i8*, i8** @copy_to_scratch.buf
%7 = icmp eq i8* %6, null
br i1 %7, label %10, label %8
8: ; preds = %5
%9 = tail call i32 @free(i8* %6)
br label %10
10: ; preds = %8, %5
%11 = icmp slt i32 %1, 40
%12 = shl nsw i32 %1, 1
%13 = select i1 %11, i32 80, i32 %12
store i32 %13, i32* @copy_to_scratch.buflen
%14 = or i32 %13, 1
%15 = tail call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** @copy_to_scratch.buf
br label %17
17: ; preds = %10, %2
%18 = icmp sgt i32 %1, 0
br i1 %18, label %19, label %23
19: ; preds = %17
%20 = load i8*, i8** @copy_to_scratch.buf
%21 = tail call i32 @strncpy(i8* %20, i8* %0, i32 %1)
%22 = zext i32 %1 to i64
br label %23
23: ; preds = %17, %19
%24 = phi i64 [ %22, %19 ], [ 0, %17 ]
%25 = load i8*, i8** @copy_to_scratch.buf
%26 = getelementptr inbounds i8, i8* %25, i64 %24
store i8 0, i8* %26
ret i8* %25
}