#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 atoi(char *);
int /*<<< orphan*/ errx(int, char *, int);
char *getenv(char *);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
void check_make_level(void) {
  char *value = getenv("__MKLVL__");
  int level = (value == NULL) ? 0 : atoi(value);
  if (level < 0) {
    errx(2, "Invalid value for recursion level (%d).", level);
  } else if (level > 500) {
    errx(2, "Max recursion level (%d) exceeded.", 500);
  } else {
    char new_value[32];
    sprintf(new_value, "%d", level + 1);
    setenv("__MKLVL__", new_value, 1);
  }
}


x86 assembly (clang, -O0):
<check_make_level>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    0xea1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    117d <check_make_level+0x2d>
xor    %eax,%eax
mov    %eax,-0x34(%rbp)
jmp    1189 <check_make_level+0x39>
mov    -0x8(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11b2 <check_make_level+0x62>
mov    -0xc(%rbp),%edx
mov    $0x2,%edi
lea    0xe62(%rip),%rsi
call   1050 <errx@plt>
jmp    120a <check_make_level+0xba>
cmpl   $0x1f4,-0xc(%rbp)
jle    11da <check_make_level+0x8a>
mov    $0x2,%edi
lea    0xe67(%rip),%rsi
mov    $0x1f4,%edx
call   1050 <errx@plt>
jmp    1205 <check_make_level+0xb5>
lea    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%edx
add    $0x1,%edx
lea    0xe6a(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x30(%rbp),%rsi
lea    0xe05(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
jmp    120a <check_make_level+0xba>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_make_level>:
push   %rbx
sub    $0x20,%rsp
lea    0xea4(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     118f <check_make_level+0x3f>
mov    %rax,%rdi
call   1060 <atoi@plt>
mov    %eax,%edx
test   %eax,%eax
js     11c0 <check_make_level+0x70>
cmp    $0x1f5,%edx
jb     1191 <check_make_level+0x41>
lea    0xeaf(%rip),%rsi
mov    $0x2,%edi
mov    $0x1f4,%edx
jmp    11cc <check_make_level+0x7c>
xor    %edx,%edx
add    $0x1,%edx
lea    0xeba(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
lea    0xe53(%rip),%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe43(%rip),%rsi
mov    $0x2,%edi
add    $0x20,%rsp
pop    %rbx
jmp    1050 <errx@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"__MKLVL__\00"
@.str.1 = [40 x i8] c"Invalid value for recursion level (%d).\00"
@.str.2 = [35 x i8] c"Max recursion level (%d) exceeded.\00"
@.str.3 = [3 x i8] c"%d\00"
define void @check_make_level() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca [32 x i8]
%4 = call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %1
%5 = load i8*, i8** %1
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %8
7: ; preds = %0
br label %11
8: ; preds = %0
%9 = load i8*, i8** %1
%10 = call i32 @atoi(i8* %9)
br label %11
11: ; preds = %8, %7
%12 = phi i32 [ 0, %7 ], [ %10, %8 ]
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* %2
%17 = call i32 @errx(i32 2, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %16)
br label %31
18: ; preds = %11
%19 = load i32, i32* %2
%20 = icmp sgt i32 %19, 500
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @errx(i32 2, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 500)
br label %30
23: ; preds = %18
%24 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%25 = load i32, i32* %2
%26 = add nsw i32 %25, 1
%27 = call i32 @sprintf(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%29 = call i32 @setenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %28, i32 1)
br label %30
30: ; preds = %23, %21
br label %31
31: ; preds = %30, %15
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"__MKLVL__\00"
@.str.1 = [40 x i8] c"Invalid value for recursion level (%d).\00"
@.str.2 = [35 x i8] c"Max recursion level (%d) exceeded.\00"
@.str.3 = [3 x i8] c"%d\00"
define void @check_make_level() {
%1 = alloca [32 x i8]
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %13, label %4
4: ; preds = %0
%5 = tail call i32 @atoi(i8* %2)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @errx(i32 2, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %5)
br label %19
9: ; preds = %4
%10 = icmp ugt i32 %5, 500
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = tail call i32 @errx(i32 2, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 500)
br label %19
13: ; preds = %0, %9
%14 = phi i32 [ %5, %9 ], [ 0, %0 ]
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%16 = add nuw nsw i32 %14, 1
%17 = call i32 @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = call i32 @setenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %15, i32 1)
br label %19
19: ; preds = %11, %13, %7
ret void
}