char *stack_string(char *q) {
  static char aside[4096];
  static char *p = aside;
  char *from = q;
  char *to = p;
  if (to > (aside + 4096 - 120))
    p = to = aside;
  while ((*to++ = *from++)) {
    if (to > (aside + 4096 - 4)) {
      p = to = aside;
      from = q;
    }
  }
  from = p;
  p = to;
  return from;
}


x86 assembly (clang, -O0):
<stack_string>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    0x2f09(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0x2f1e(%rip),%rax
add    $0x1000,%rax
add    $0xffffffffffffff88,%rax
cmp    %rax,-0x18(%rbp)
jbe    114f <stack_string+0x4f>
lea    0x2f03(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0x2ef8(%rip),%rax
mov    %rax,0x2ed1(%rip)
jmp    1154 <stack_string+0x54>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    -0x18(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %al,(%rcx)
cmp    $0x0,%al
je     11bf <stack_string+0xbf>
lea    0x2ebb(%rip),%rax
add    $0x1000,%rax
add    $0xfffffffffffffffc,%rax
cmp    %rax,-0x18(%rbp)
jbe    11ba <stack_string+0xba>
lea    0x2ea0(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0x2e95(%rip),%rax
mov    %rax,0x2e6e(%rip)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1154 <stack_string+0x54>
mov    0x2e5a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,0x2e4b(%rip)
mov    -0x10(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<stack_string>:
mov    0x2f19(%rip),%rax
lea    0x2f32(%rip),%r9
lea    0x3eb3(%rip),%rcx
cmp    %rcx,%rax
cmova  %r9,%rax
mov    (%rdi),%sil
lea    0x1(%rax),%rcx
mov    %sil,(%rax)
test   %sil,%sil
je     1160 <stack_string+0x60>
lea    0x3f0a(%rip),%r8
mov    %rdi,%rsi
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%rsi
cmp    %r8,%rcx
cmova  %r9,%rax
cmova  %rdi,%rsi
cmova  %r9,%rcx
movzbl (%rsi),%edx
mov    %dl,(%rcx)
add    $0x1,%rcx
test   %dl,%dl
jne    1140 <stack_string+0x40>
mov    %rcx,0x2eb9(%rip)
ret

LLVM IR (-O0):
@stack_string.aside = internal global [4096 x i8] zeroinitializer
@stack_string.p = internal global i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @stack_string.aside, i32 0, i32 0)
define i8* @stack_string(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
store i8* %5, i8** %3
%6 = load i8*, i8** @stack_string.p
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = icmp ugt i8* %7, getelementptr inbounds ([4096 x i8], [4096 x i8]* @stack_string.aside, i64 1, i64 -120)
br i1 %8, label %9, label %10
9: ; preds = %1
store i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @stack_string.aside, i64 0, i64 0), i8** %4
store i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @stack_string.aside, i64 0, i64 0), i8** @stack_string.p
br label %10
10: ; preds = %9, %1
br label %11
11: ; preds = %23, %10
%12 = load i8*, i8** %3
%13 = getelementptr inbounds i8, i8* %12, i32 1
store i8* %13, i8** %3
%14 = load i8, i8* %12
%15 = load i8*, i8** %4
%16 = getelementptr inbounds i8, i8* %15, i32 1
store i8* %16, i8** %4
store i8 %14, i8* %15
%17 = icmp ne i8 %14, 0
br i1 %17, label %18, label %24
18: ; preds = %11
%19 = load i8*, i8** %4
%20 = icmp ugt i8* %19, getelementptr inbounds ([4096 x i8], [4096 x i8]* @stack_string.aside, i64 1, i64 -4)
br i1 %20, label %21, label %23
21: ; preds = %18
store i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @stack_string.aside, i64 0, i64 0), i8** %4
store i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @stack_string.aside, i64 0, i64 0), i8** @stack_string.p
%22 = load i8*, i8** %2
store i8* %22, i8** %3
br label %23
23: ; preds = %21, %18
br label %11
24: ; preds = %11
%25 = load i8*, i8** @stack_string.p
store i8* %25, i8** %3
%26 = load i8*, i8** %4
store i8* %26, i8** @stack_string.p
%27 = load i8*, i8** %3
ret i8* %27
}

LLVM IR (-O2):
@stack_string.aside = internal global [4096 x i8] zeroinitializer
@stack_string.p = internal unnamed_addr global i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @stack_string.aside, i64 0, i64 0)
define i8* @stack_string(i8* nocapture %0) {
%2 = load i8*, i8** @stack_string.p
%3 = icmp ugt i8* %2, getelementptr inbounds ([4096 x i8], [4096 x i8]* @stack_string.aside, i64 0, i64 3976)
%4 = select i1 %3, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @stack_string.aside, i64 0, i64 0), i8* %2
%5 = load i8, i8* %0
%6 = getelementptr inbounds i8, i8* %4, i64 1
store i8 %5, i8* %4
%7 = icmp eq i8 %5, 0
br i1 %7, label %20, label %8
8: ; preds = %1, %8
%9 = phi i8* [ %14, %8 ], [ %4, %1 ]
%10 = phi i8* [ %18, %8 ], [ %6, %1 ]
%11 = phi i8* [ %15, %8 ], [ %0, %1 ]
%12 = getelementptr inbounds i8, i8* %11, i64 1
%13 = icmp ugt i8* %10, getelementptr inbounds ([4096 x i8], [4096 x i8]* @stack_string.aside, i64 0, i64 4092)
%14 = select i1 %13, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @stack_string.aside, i64 0, i64 0), i8* %9
%15 = select i1 %13, i8* %0, i8* %12
%16 = select i1 %13, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @stack_string.aside, i64 0, i64 0), i8* %10
%17 = load i8, i8* %15
%18 = getelementptr inbounds i8, i8* %16, i64 1
store i8 %17, i8* %16
%19 = icmp eq i8 %17, 0
br i1 %19, label %20, label %8
20: ; preds = %8, %1
%21 = phi i8* [ %4, %1 ], [ %14, %8 ]
%22 = phi i8* [ %6, %1 ], [ %18, %8 ]
store i8* %22, i8** @stack_string.p
ret i8* %21
}