#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 */
char *getenv(char *);
scalar_t__ getpid();
int /*<<< orphan*/ putenv(int /*<<< orphan*/);
int /*<<< orphan*/ rmarg(char **, char *);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int, ...);
int sscanf(char *, char *, unsigned int *, int *, char *);
int /*<<< orphan*/ strdup(char *);
int strlen(char *);
int chkenv(int argc) {
  char buff[512];
  unsigned mask, m;
  int l, a, c;
  char *string;
  extern char **environ;
  mask = (unsigned)chkenv;
  mask ^= (unsigned)getpid() * ~mask;
  sprintf(buff, "x%x", mask);
  string = getenv(buff);
  l = strlen(buff);
  if (!string) {
    sprintf(&buff[l], "=%u %d", mask, argc);
    putenv(strdup(buff));
    return 0;
  }
  c = sscanf(string, "%u %d%c", &m, &a, buff);
  if (c == 2 && m == mask) {
    rmarg(environ, &string[-l - 1]);
    return 1 + (argc - a);
  }
  return -1;
}


x86 assembly (clang, -O0):
<chkenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e43(%rip),%rax
mov    %eax,-0x214(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x214(%rbp),%ecx
xor    $0xffffffff,%ecx
imul   %ecx,%eax
xor    -0x214(%rbp),%eax
mov    %eax,-0x214(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x214(%rbp),%edx
lea    0xe32(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
lea    -0x210(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x230(%rbp)
lea    -0x210(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x21c(%rbp)
cmpq   $0x0,-0x230(%rbp)
jne    124f <chkenv+0xcf>
movslq -0x21c(%rbp),%rax
lea    -0x210(%rbp),%rdi
add    %rax,%rdi
mov    -0x214(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xddb(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
lea    -0x210(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %eax,%edi
call   1060 <putenv@plt>
movl   $0x0,-0x4(%rbp)
jmp    12dd <chkenv+0x15d>
mov    -0x230(%rbp),%rdi
lea    -0x210(%rbp),%r8
lea    0xda7(%rip),%rsi
lea    -0x218(%rbp),%rdx
lea    -0x220(%rbp),%rcx
call   1070 <sscanf@plt>
mov    %eax,-0x224(%rbp)
cmpl   $0x2,-0x224(%rbp)
jne    12d6 <chkenv+0x156>
mov    -0x218(%rbp),%eax
cmp    -0x214(%rbp),%eax
jne    12d6 <chkenv+0x156>
mov    0x2d45(%rip),%rax
mov    (%rax),%rdi
mov    -0x230(%rbp),%rsi
xor    %eax,%eax
sub    -0x21c(%rbp),%eax
sub    $0x1,%eax
cltq
add    %rax,%rsi
call   1080 <rmarg@plt>
mov    -0x8(%rbp),%eax
sub    -0x220(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    12dd <chkenv+0x15d>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chkenv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %edi,%r14d
xor    %r15d,%r15d
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0x2e34(%rip),%ecx
mov    %ecx,%ebx
not    %ebx
imul   %eax,%ebx
xor    %ecx,%ebx
lea    0xe4c(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %ebx,%edx
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    %rbp,%rdi
call   1030 <getenv@plt>
mov    %rax,%r13
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %eax,%r12d
test   %r13,%r13
je     1238 <chkenv+0xb8>
lea    0xe24(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %r13,%rdi
call   1070 <sscanf@plt>
mov    $0xffffffff,%r15d
cmp    $0x2,%eax
jne    1267 <chkenv+0xe7>
cmp    %ebx,0xc(%rsp)
jne    1267 <chkenv+0xe7>
mov    0x2dd2(%rip),%rax
mov    (%rax),%rdi
not    %r12d
movslq %r12d,%rax
add    %rax,%r13
mov    %r13,%rsi
call   1080 <rmarg@plt>
sub    0x8(%rsp),%r14d
add    $0x1,%r14d
mov    %r14d,%r15d
jmp    1267 <chkenv+0xe7>
movslq %r12d,%rax
lea    (%rsp,%rax,1),%rdi
add    $0x10,%rdi
lea    0xdba(%rip),%rsi
mov    %ebx,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1090 <sprintf@plt>
lea    0x10(%rsp),%rdi
call   10a0 <strdup@plt>
mov    %eax,%edi
call   1060 <putenv@plt>
mov    %r15d,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"x%x\00"
@.str.1 = [7 x i8] c"=%u %d\00"
@.str.2 = [8 x i8] c"%u %d%c\00"
@environ = external global i8**
define i32 @chkenv(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [512 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %3
store i32 ptrtoint (i32 (i32)* @chkenv to i32), i32* %5
%11 = call i64 (...) @getpid()
%12 = trunc i64 %11 to i32
%13 = load i32, i32* %5
%14 = xor i32 %13, -1
%15 = mul i32 %12, %14
%16 = load i32, i32* %5
%17 = xor i32 %16, %15
store i32 %17, i32* %5
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%19 = load i32, i32* %5
%20 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%22 = call i8* @getenv(i8* %21)
store i8* %22, i8** %10
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%24 = call i32 @strlen(i8* %23)
store i32 %24, i32* %7
%25 = load i8*, i8** %10
%26 = icmp ne i8* %25, null
br i1 %26, label %37, label %27
27: ; preds = %1
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %29
%31 = load i32, i32* %5
%32 = load i32, i32* %3
%33 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %31, i32 %32)
%34 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%35 = call i32 @strdup(i8* %34)
%36 = call i32 @putenv(i32 %35)
store i32 0, i32* %2
br label %61
37: ; preds = %1
%38 = load i8*, i8** %10
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%40 = call i32 @sscanf(i8* %38, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32* %6, i32* %8, i8* %39)
store i32 %40, i32* %9
%41 = load i32, i32* %9
%42 = icmp eq i32 %41, 2
br i1 %42, label %43, label %60
43: ; preds = %37
%44 = load i32, i32* %6
%45 = load i32, i32* %5
%46 = icmp eq i32 %44, %45
br i1 %46, label %47, label %60
47: ; preds = %43
%48 = load i8**, i8*** @environ
%49 = load i8*, i8** %10
%50 = load i32, i32* %7
%51 = sub nsw i32 0, %50
%52 = sub nsw i32 %51, 1
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %49, i64 %53
%55 = call i32 @rmarg(i8** %48, i8* %54)
%56 = load i32, i32* %3
%57 = load i32, i32* %8
%58 = sub nsw i32 %56, %57
%59 = add nsw i32 1, %58
store i32 %59, i32* %2
br label %61
60: ; preds = %43, %37
store i32 -1, i32* %2
br label %61
61: ; preds = %60, %47, %27
%62 = load i32, i32* %2
ret i32 %62
}

LLVM IR (-O2):
@.str = [4 x i8] c"x%x\00"
@.str.1 = [7 x i8] c"=%u %d\00"
@.str.2 = [8 x i8] c"%u %d%c\00"
@environ = external global i8**
define i32 @chkenv(i32 %0) {
%2 = alloca [512 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = tail call i64 (...) @getpid()
%9 = trunc i64 %8 to i32
%10 = mul i32 %9, xor (i32 ptrtoint (i32 (i32)* @chkenv to i32), i32 -1)
%11 = xor i32 %10, ptrtoint (i32 (i32)* @chkenv to i32)
%12 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = call i8* @getenv(i8* %5)
%14 = call i32 @strlen(i8* %5)
%15 = icmp eq i8* %13, null
br i1 %15, label %16, label %22
16: ; preds = %1
%17 = sext i32 %14 to i64
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 %17
%19 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %18, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %0)
%20 = call i32 @strdup(i8* %5)
%21 = call i32 @putenv(i32 %20)
br label %37
22: ; preds = %1
%23 = call i32 @sscanf(i8* %13, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32* %3, i32* %4, i8* %5)
%24 = icmp eq i32 %23, 2
%25 = load i32, i32* %3
%26 = icmp eq i32 %25, %11
%27 = select i1 %24, i1 %26, i1 false
br i1 %27, label %28, label %37
28: ; preds = %22
%29 = load i8**, i8*** @environ
%30 = xor i32 %14, -1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %13, i64 %31
%33 = call i32 @rmarg(i8** %29, i8* %32)
%34 = load i32, i32* %4
%35 = add i32 %0, 1
%36 = sub i32 %35, %34
br label %37
37: ; preds = %22, %28, %16
%38 = phi i32 [ %36, %28 ], [ 0, %16 ], [ -1, %22 ]
ret i32 %38
}