#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 long, ...);
int sscanf(char *, char *, unsigned long *, int *, char *);
int /*<<< orphan*/ strdup(char *);
int strlen(char *);
int chkenv(int argc) {
  char buff[512];
  unsigned long mask, m;
  int l, a, c;
  char *string;
  extern char **environ;
  mask = (unsigned long)&chkenv;
  mask ^= (unsigned long)getpid() * ~mask;
  sprintf(buff, "x%lx", mask);
  string = getenv(buff);
  l = strlen(buff);
  if (!string) {
    sprintf(&buff[l], "=%lu %d", mask, argc);
    putenv(strdup(buff));
    return 0;
  }
  c = sscanf(string, "%lu %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    $0x240,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e43(%rip),%rax
mov    %rax,-0x218(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x218(%rbp),%rcx
xor    $0xffffffffffffffff,%rcx
imul   %rcx,%rax
xor    -0x218(%rbp),%rax
mov    %rax,-0x218(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x218(%rbp),%rdx
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
lea    -0x210(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x238(%rbp)
lea    -0x210(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x224(%rbp)
cmpq   $0x0,-0x238(%rbp)
jne    1257 <chkenv+0xd7>
movslq -0x224(%rbp),%rax
lea    -0x210(%rbp),%rdi
add    %rax,%rdi
mov    -0x218(%rbp),%rdx
mov    -0x8(%rbp),%ecx
lea    0xdd4(%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    12e7 <chkenv+0x167>
mov    -0x238(%rbp),%rdi
lea    -0x210(%rbp),%r8
lea    0xda1(%rip),%rsi
lea    -0x220(%rbp),%rdx
lea    -0x228(%rbp),%rcx
call   1070 <sscanf@plt>
mov    %eax,-0x22c(%rbp)
cmpl   $0x2,-0x22c(%rbp)
jne    12e0 <chkenv+0x160>
mov    -0x220(%rbp),%rax
cmp    -0x218(%rbp),%rax
jne    12e0 <chkenv+0x160>
mov    0x2d3b(%rip),%rax
mov    (%rax),%rdi
mov    -0x238(%rbp),%rsi
xor    %eax,%eax
sub    -0x224(%rbp),%eax
sub    $0x1,%eax
cltq
add    %rax,%rsi
call   1080 <rmarg@plt>
mov    -0x8(%rbp),%eax
sub    -0x228(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    12e7 <chkenv+0x167>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%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    0x2e33(%rip),%rcx
mov    %rcx,%rbx
not    %rbx
imul   %rax,%rbx
xor    %rcx,%rbx
lea    0xe47(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %rbx,%rdx
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     123f <chkenv+0xbf>
lea    0xe20(%rip),%rsi
lea    0x8(%rsp),%rdx
lea    0x4(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %r13,%rdi
call   1070 <sscanf@plt>
mov    $0xffffffff,%r15d
cmp    $0x2,%eax
jne    126f <chkenv+0xef>
cmp    %rbx,0x8(%rsp)
jne    126f <chkenv+0xef>
mov    0x2dcb(%rip),%rax
mov    (%rax),%rdi
not    %r12d
movslq %r12d,%rax
add    %rax,%r13
mov    %r13,%rsi
call   1080 <rmarg@plt>
sub    0x4(%rsp),%r14d
add    $0x1,%r14d
mov    %r14d,%r15d
jmp    126f <chkenv+0xef>
movslq %r12d,%rax
lea    (%rsp,%rax,1),%rdi
add    $0x10,%rdi
lea    0xdb4(%rip),%rsi
mov    %rbx,%rdx
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 = [5 x i8] c"x%lx\00"
@.str.1 = [8 x i8] c"=%lu %d\00"
@.str.2 = [9 x i8] c"%lu %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 i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %3
store i64 ptrtoint (i32 (i32)* @chkenv to i64), i64* %5
%11 = call i64 (...) @getpid()
%12 = load i64, i64* %5
%13 = xor i64 %12, -1
%14 = mul i64 %11, %13
%15 = load i64, i64* %5
%16 = xor i64 %15, %14
store i64 %16, i64* %5
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%18 = load i64, i64* %5
%19 = call i32 (i8*, i8*, i64, ...) @sprintf(i8* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %18)
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%21 = call i8* @getenv(i8* %20)
store i8* %21, i8** %10
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%23 = call i32 @strlen(i8* %22)
store i32 %23, i32* %7
%24 = load i8*, i8** %10
%25 = icmp ne i8* %24, null
br i1 %25, label %36, label %26
26: ; preds = %1
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %28
%30 = load i64, i64* %5
%31 = load i32, i32* %3
%32 = call i32 (i8*, i8*, i64, ...) @sprintf(i8* %29, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i64 %30, i32 %31)
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%34 = call i32 @strdup(i8* %33)
%35 = call i32 @putenv(i32 %34)
store i32 0, i32* %2
br label %60
36: ; preds = %1
%37 = load i8*, i8** %10
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%39 = call i32 @sscanf(i8* %37, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i64* %6, i32* %8, i8* %38)
store i32 %39, i32* %9
%40 = load i32, i32* %9
%41 = icmp eq i32 %40, 2
br i1 %41, label %42, label %59
42: ; preds = %36
%43 = load i64, i64* %6
%44 = load i64, i64* %5
%45 = icmp eq i64 %43, %44
br i1 %45, label %46, label %59
46: ; preds = %42
%47 = load i8**, i8*** @environ
%48 = load i8*, i8** %10
%49 = load i32, i32* %7
%50 = sub nsw i32 0, %49
%51 = sub nsw i32 %50, 1
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %48, i64 %52
%54 = call i32 @rmarg(i8** %47, i8* %53)
%55 = load i32, i32* %3
%56 = load i32, i32* %8
%57 = sub nsw i32 %55, %56
%58 = add nsw i32 1, %57
store i32 %58, i32* %2
br label %60
59: ; preds = %42, %36
store i32 -1, i32* %2
br label %60
60: ; preds = %59, %46, %26
%61 = load i32, i32* %2
ret i32 %61
}

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