#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 */
struct rlimit {
  int rlim_cur;
};

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_STACK;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ getrlimit(int /*<<< orphan*/, struct rlimit *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ setrlimit(int /*<<< orphan*/, struct rlimit *);
int snprintf(char *, int, char *, int /*<<< orphan*/);
int main(void) {
  struct rlimit lim;
  getrlimit(RLIMIT_STACK, &lim);
  lim.rlim_cur = 1048576;
  setrlimit(RLIMIT_STACK, &lim);
  char *fmtstr = malloc(4 * 1000000 + 1);
  if (fmtstr == NULL)
    abort();
  char *output = malloc(1000000 + 1);
  if (output == NULL)
    abort();
  for (size_t i = 0; i < 1000000; i++)
    memcpy(fmtstr + 4 * i, "%1$d", 4);
  fmtstr[4 * 1000000] = '\0';
  int ret = snprintf(output, 1000000 + 1, fmtstr, 0);
  if (ret != 1000000)
    abort();
  for (size_t i = 0; i < 1000000; i++)
    if (output[i] != '0')
      abort();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1080 <getrlimit@plt>
movl   $0x100000,-0x8(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1070 <setrlimit@plt>
mov    $0x3d0901,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ba <main+0x5a>
mov    $0x0,%al
call   1030 <abort@plt>
mov    $0xf4241,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11da <main+0x7a>
mov    $0x0,%al
call   1030 <abort@plt>
movq   $0x0,-0x20(%rbp)
cmpq   $0xf4240,-0x20(%rbp)
jae    1221 <main+0xc1>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
lea    0xdfa(%rip),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11e2 <main+0x82>
mov    -0x10(%rbp),%rax
movb   $0x0,0x3d0900(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0xf4241,%esi
xor    %ecx,%ecx
call   1040 <snprintf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xf4240,-0x24(%rbp)
je     1257 <main+0xf7>
mov    $0x0,%al
call   1030 <abort@plt>
movq   $0x0,-0x30(%rbp)
cmpq   $0xf4240,-0x30(%rbp)
jae    129f <main+0x13f>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x30,%eax
je     1289 <main+0x129>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    128e <main+0x12e>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    125f <main+0xff>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e71(%rip),%r14
mov    (%r14),%edi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   1080 <getrlimit@plt>
movl   $0x100000,(%rsp)
mov    (%r14),%edi
mov    %rbx,%rsi
call   1070 <setrlimit@plt>
mov    $0x3d0901,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1261 <main+0x101>
mov    %rax,%r14
mov    $0xf4241,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1261 <main+0x101>
mov    %rax,%r12
xor    %ebx,%ebx
lea    0xe3c(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    (%r14,%rbx,1),%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
add    $0x4,%rbx
cmp    $0x3d0900,%rbx
jne    11d0 <main+0x70>
movb   $0x0,0x3d0900(%r14)
mov    %r12,%rdi
mov    $0xf4241,%esi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1040 <snprintf@plt>
cmp    $0xf4240,%eax
jne    1261 <main+0x101>
mov    $0x4,%eax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmpb   $0x30,-0x4(%r12,%rax,1)
jne    1261 <main+0x101>
cmpb   $0x30,-0x3(%r12,%rax,1)
jne    1261 <main+0x101>
cmpb   $0x30,-0x2(%r12,%rax,1)
jne    1261 <main+0x101>
cmpb   $0x30,-0x1(%r12,%rax,1)
jne    1261 <main+0x101>
cmpb   $0x30,(%r12,%rax,1)
jne    1261 <main+0x101>
add    $0x5,%rax
cmp    $0xf4244,%rax
jne    1220 <main+0xc0>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.rlimit = type { i32 }
@RLIMIT_STACK = global i32 0
@.str = [5 x i8] c"%1$d\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.rlimit
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
store i32 0, i32* %1
%8 = load i32, i32* @RLIMIT_STACK
%9 = call i32 @getrlimit(i32 %8, %struct.rlimit* %2)
%10 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
store i32 1048576, i32* %10
%11 = load i32, i32* @RLIMIT_STACK
%12 = call i32 @setrlimit(i32 %11, %struct.rlimit* %2)
%13 = call i8* @malloc(i32 4000001)
store i8* %13, i8** %3
%14 = load i8*, i8** %3
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 (...) @abort()
unreachable
18: ; preds = %0
%19 = call i8* @malloc(i32 1000001)
store i8* %19, i8** %4
%20 = load i8*, i8** %4
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 (...) @abort()
unreachable
24: ; preds = %18
store i64 0, i64* %5
br label %25
25: ; preds = %34, %24
%26 = load i64, i64* %5
%27 = icmp ult i64 %26, 1000000
br i1 %27, label %28, label %37
28: ; preds = %25
%29 = load i8*, i8** %3
%30 = load i64, i64* %5
%31 = mul i64 4, %30
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = call i32 @memcpy(i8* %32, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
br label %34
34: ; preds = %28
%35 = load i64, i64* %5
%36 = add i64 %35, 1
store i64 %36, i64* %5
br label %25
37: ; preds = %25
%38 = load i8*, i8** %3
%39 = getelementptr inbounds i8, i8* %38, i64 4000000
store i8 0, i8* %39
%40 = load i8*, i8** %4
%41 = load i8*, i8** %3
%42 = call i32 @snprintf(i8* %40, i32 1000001, i8* %41, i32 0)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp ne i32 %43, 1000000
br i1 %44, label %45, label %47
45: ; preds = %37
%46 = call i32 (...) @abort()
unreachable
47: ; preds = %37
store i64 0, i64* %7
br label %48
48: ; preds = %61, %47
%49 = load i64, i64* %7
%50 = icmp ult i64 %49, 1000000
br i1 %50, label %51, label %64
51: ; preds = %48
%52 = load i8*, i8** %4
%53 = load i64, i64* %7
%54 = getelementptr inbounds i8, i8* %52, i64 %53
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp ne i32 %56, 48
br i1 %57, label %58, label %60
58: ; preds = %51
%59 = call i32 (...) @abort()
unreachable
60: ; preds = %51
br label %61
61: ; preds = %60
%62 = load i64, i64* %7
%63 = add i64 %62, 1
store i64 %63, i64* %7
br label %48
64: ; preds = %48
ret i32 0
}

LLVM IR (-O2):
%struct.rlimit = type { i32 }
@RLIMIT_STACK = global i32 0
@.str = [5 x i8] c"%1$d\00"
define i32 @main() {
%1 = alloca %struct.rlimit
%2 = bitcast %struct.rlimit* %1 to i8*
%3 = load i32, i32* @RLIMIT_STACK
%4 = call i32 @getrlimit(i32 %3, %struct.rlimit* %1)
%5 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
store i32 1048576, i32* %5
%6 = load i32, i32* @RLIMIT_STACK
%7 = call i32 @setrlimit(i32 %6, %struct.rlimit* %1)
%8 = call dereferenceable_or_null(4000001) i8* @malloc(i32 4000001)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 (...) @abort()
unreachable
12: ; preds = %0
%13 = call dereferenceable_or_null(1000001) i8* @malloc(i32 1000001)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = call i32 (...) @abort()
unreachable
17: ; preds = %21
%18 = getelementptr inbounds i8, i8* %8, i64 4000000
store i8 0, i8* %18
%19 = call i32 @snprintf(i8* %13, i32 1000001, i8* %8, i32 0)
%20 = icmp eq i32 %19, 1000000
br i1 %20, label %54, label %28
21: ; preds = %12, %21
%22 = phi i64 [ %26, %21 ], [ 0, %12 ]
%23 = shl nuw nsw i64 %22, 2
%24 = getelementptr inbounds i8, i8* %8, i64 %23
%25 = call i32 @memcpy(i8* %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%26 = add nuw nsw i64 %22, 1
%27 = icmp eq i64 %26, 1000000
br i1 %27, label %17, label %21
28: ; preds = %17
%29 = call i32 (...) @abort()
unreachable
30: ; preds = %54
%31 = add nuw nsw i64 %55, 1
%32 = getelementptr inbounds i8, i8* %13, i64 %31
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 48
br i1 %34, label %35, label %59
35: ; preds = %30
%36 = add nuw nsw i64 %55, 2
%37 = getelementptr inbounds i8, i8* %13, i64 %36
%38 = load i8, i8* %37
%39 = icmp eq i8 %38, 48
br i1 %39, label %40, label %59
40: ; preds = %35
%41 = add nuw nsw i64 %55, 3
%42 = getelementptr inbounds i8, i8* %13, i64 %41
%43 = load i8, i8* %42
%44 = icmp eq i8 %43, 48
br i1 %44, label %45, label %59
45: ; preds = %40
%46 = add nuw nsw i64 %55, 4
%47 = getelementptr inbounds i8, i8* %13, i64 %46
%48 = load i8, i8* %47
%49 = icmp eq i8 %48, 48
br i1 %49, label %50, label %59
50: ; preds = %45
%51 = add nuw nsw i64 %55, 5
%52 = icmp eq i64 %51, 1000000
br i1 %52, label %53, label %54
53: ; preds = %50
ret i32 0
54: ; preds = %17, %50
%55 = phi i64 [ %51, %50 ], [ 0, %17 ]
%56 = getelementptr inbounds i8, i8* %13, i64 %55
%57 = load i8, i8* %56
%58 = icmp eq i8 %57, 48
br i1 %58, label %30, label %59
59: ; preds = %45, %40, %35, %30, %54
%60 = call i32 (...) @abort()
unreachable
}