#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 /*<<< orphan*/ buf;

/* Variables and functions */
int atoi(char *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  char buf[256];
  int i = argv[1] ? atoi(argv[1]) : 999999;
  while (--i > 0) {
    sprintf(buf, "%d", i);
    setenv("VAR", buf, 1);
  }
  printf("Check size of [heap] mapping:\n");
  freopen("/proc/self/maps", "r", stdin);
  while (fgets(buf, sizeof(buf), stdin))
    fputs(buf, stdout);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     11b0 <main+0x40>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x118(%rbp)
jmp    11c0 <main+0x50>
mov    $0xf423f,%eax
mov    %eax,-0x118(%rbp)
jmp    11c0 <main+0x50>
mov    -0x118(%rbp),%eax
mov    %eax,-0x114(%rbp)
mov    -0x114(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x114(%rbp)
cmp    $0x0,%eax
jle    121a <main+0xaa>
lea    -0x110(%rbp),%rdi
mov    -0x114(%rbp),%edx
lea    0xe08(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x110(%rbp),%rsi
lea    0xdf8(%rip),%rdi
mov    $0x1,%edx
call   1030 <setenv@plt>
jmp    11cc <main+0x5c>
lea    0xde6(%rip),%rdi
call   1040 <printf@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
lea    0xdf0(%rip),%rdi
lea    0xdf9(%rip),%rsi
call   1060 <freopen@plt>
lea    -0x110(%rbp),%rdi
mov    0x2d98(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1280 <main+0x110>
lea    -0x110(%rbp),%rdi
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
jmp    1242 <main+0xd2>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    0x8(%rsi),%rdi
test   %rdi,%rdi
je     1195 <main+0x25>
call   1080 <atoi@plt>
mov    %eax,%ebx
cmp    $0x2,%eax
jge    119a <main+0x2a>
jmp    11d6 <main+0x66>
mov    $0xf423f,%ebx
add    $0x1,%ebx
lea    0xe5c(%rip),%r14
mov    %rsp,%r15
lea    0xe55(%rip),%r12
xchg   %ax,%ax
lea    -0x2(%rbx),%edx
mov    %r15,%rdi
mov    %r14,%rsi
call   1090 <sprintf@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
add    $0xffffffff,%ebx
cmp    $0x2,%ebx
ja     11b0 <main+0x40>
lea    0xe3c(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dff(%rip),%r14
mov    (%r14),%edx
lea    0xe14(%rip),%rdi
lea    0xe1d(%rip),%rsi
call   1060 <freopen@plt>
mov    (%r14),%edx
mov    %rsp,%rdi
mov    $0x100,%esi
call   1070 <fgets@plt>
test   %rax,%rax
je     1240 <main+0xd0>
mov    0x2dbd(%rip),%r15
mov    %rsp,%rbx
xchg   %ax,%ax
mov    (%r15),%esi
mov    %rbx,%rdi
call   1050 <fputs@plt>
mov    (%r14),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1070 <fgets@plt>
test   %rax,%rax
jne    1220 <main+0xb0>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [4 x i8] c"VAR\00"
@.str.2 = [31 x i8] c"Check size of [heap] mapping:\0A\00"
@.str.3 = [16 x i8] c"/proc/self/maps\00"
@.str.4 = [2 x i8] c"r\00"
@stdin = global i32 0
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [256 x i8]
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i8**, i8*** %5
%9 = getelementptr inbounds i8*, i8** %8, i64 1
%10 = load i8*, i8** %9
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @atoi(i8* %15)
br label %18
17: ; preds = %2
br label %18
18: ; preds = %17, %12
%19 = phi i32 [ %16, %12 ], [ 999999, %17 ]
store i32 %19, i32* %7
br label %20
20: ; preds = %24, %18
%21 = load i32, i32* %7
%22 = add nsw i32 %21, -1
store i32 %22, i32* %7
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%26 = load i32, i32* %7
%27 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%29 = call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %28, i32 1)
br label %20
30: ; preds = %20
%31 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* @stdin
%33 = call i32 @freopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %39, %30
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%36 = load i32, i32* @stdin
%37 = call i64 @fgets(i8* %35, i32 256, i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %34
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%41 = load i32, i32* @stdout
%42 = call i32 @fputs(i8* %40, i32 %41)
br label %34
43: ; preds = %34
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [4 x i8] c"VAR\00"
@.str.3 = [16 x i8] c"/proc/self/maps\00"
@.str.4 = [2 x i8] c"r\00"
@stdin = global i32 0
@stdout = global i32 0
@str = [30 x i8] c"Check size of [heap] mapping:\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = icmp eq i8* %6, null
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = tail call i32 @atoi(i8* %6)
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %19
11: ; preds = %2, %8
%12 = phi i32 [ 999999, %2 ], [ %9, %8 ]
br label %13
13: ; preds = %11, %13
%14 = phi i32 [ %15, %13 ], [ %12, %11 ]
%15 = add nsw i32 %14, -1
%16 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %4, i32 1)
%18 = icmp ugt i32 %14, 2
br i1 %18, label %13, label %19
19: ; preds = %13, %8
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%21 = load i32, i32* @stdin
%22 = call i32 @freopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%23 = load i32, i32* @stdin
%24 = call i64 @fgets(i8* %4, i32 256, i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %19, %26
%27 = load i32, i32* @stdout
%28 = call i32 @fputs(i8* %4, i32 %27)
%29 = load i32, i32* @stdin
%30 = call i64 @fgets(i8* %4, i32 256, i32 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %26
32: ; preds = %26, %19
ret i32 0
}