#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 clearenv();
int /*<<< orphan*/ echo_env(char const *);
int /*<<< orphan*/ err_sys(char *, ...);
int /*<<< orphan*/ printf(char *, ...);
int putenv(char *);
int setenv(char const *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int unsetenv(char const *);
int main(int argc, char *argv[]) {
  if (argc < 3)
    err_sys("Usage: %s env value\n", argv[0]);
  char const *key = argv[1];
  echo_env(key);
  char *newval = argv[2];
  char buf[256] = {0};
  sprintf(buf, "%s=%s", key, newval);
  int ret = putenv(buf);
  if (ret != 0)
    err_sys("putenv");
  echo_env(key);
  strcat(buf, " (deleted)");
  echo_env(key);
  ret = unsetenv(key);
  if (ret != 0)
    printf("unsetenv %s failed\n", key);
  echo_env(key);
  ret = unsetenv(key);
  if (ret != 0)
    printf("unsetenv %s failed\n", key);
  echo_env(key);
  ret = setenv(key, newval, 0);
  if (ret != 0)
    printf("setenv %s failed\n", key);
  echo_env(key);
  ret = setenv(key, "1", 0);
  if (ret != 0)
    printf("setenv %s failed\n", key);
  echo_env(key);
  ret = setenv(key, "2", 1);
  if (ret != 0)
    printf("setenv %s failed\n", key);
  echo_env(key);
  ret = clearenv();
  if (ret != 0)
    printf("clearenv failed\n");
  extern char **environ;
  printf("environ = %p\n", environ);
  echo_env(key);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11d8 <main+0x38>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1030 <err_sys@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <echo_env@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1080 <memset@plt>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xdf3(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x120(%rbp),%rdi
call   1090 <putenv@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
je     1254 <main+0xb4>
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1030 <err_sys@plt>
mov    -0x18(%rbp),%rdi
call   1060 <echo_env@plt>
lea    -0x120(%rbp),%rdi
lea    0xdb7(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x18(%rbp),%rdi
call   1060 <echo_env@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <unsetenv@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
je     12a7 <main+0x107>
mov    -0x18(%rbp),%rsi
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <echo_env@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <unsetenv@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
je     12de <main+0x13e>
mov    -0x18(%rbp),%rsi
lea    0xd56(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <echo_env@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1040 <setenv@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
je     131b <main+0x17b>
mov    -0x18(%rbp),%rsi
lea    0xd1b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <echo_env@plt>
mov    -0x18(%rbp),%rdi
lea    0xd12(%rip),%rsi
xor    %edx,%edx
call   1040 <setenv@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
je     135b <main+0x1bb>
mov    -0x18(%rbp),%rsi
lea    0xcdb(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <echo_env@plt>
mov    -0x18(%rbp),%rdi
lea    0xcd4(%rip),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
je     139e <main+0x1fe>
mov    -0x18(%rbp),%rsi
lea    0xc98(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <echo_env@plt>
mov    $0x0,%al
call   1050 <clearenv@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
je     13cf <main+0x22f>
lea    0xc7d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2c12(%rip),%rax
mov    (%rax),%rsi
lea    0xc76(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <echo_env@plt>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jg     11c5 <main+0x25>
mov    (%r14),%rsi
lea    0xe42(%rip),%rdi
xor    %eax,%eax
call   1040 <err_sys@plt>
mov    0x8(%r14),%rbx
mov    %rbx,%rdi
call   1070 <echo_env@plt>
mov    0x10(%r14),%r14
xorps  %xmm0,%xmm0
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xdcf(%rip),%rsi
mov    %rsp,%r15
mov    %r15,%rdi
mov    %rbx,%rdx
mov    %r14,%rcx
call   10c0 <sprintf@plt>
mov    %r15,%rdi
call   1090 <putenv@plt>
test   %eax,%eax
je     1271 <main+0xd1>
lea    0xdb1(%rip),%rdi
xor    %eax,%eax
call   1040 <err_sys@plt>
mov    %rbx,%rdi
call   1070 <echo_env@plt>
lea    0xda2(%rip),%rsi
mov    %rsp,%rdi
call   10b0 <strcat@plt>
mov    %rbx,%rdi
call   1070 <echo_env@plt>
mov    %rbx,%rdi
call   10a0 <unsetenv@plt>
test   %eax,%eax
je     12ad <main+0x10d>
lea    0xd8a(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rdi
call   1070 <echo_env@plt>
mov    %rbx,%rdi
call   10a0 <unsetenv@plt>
test   %eax,%eax
je     12d2 <main+0x132>
lea    0xd65(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rdi
call   1070 <echo_env@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <setenv@plt>
test   %eax,%eax
je     12fc <main+0x15c>
lea    0xd3d(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rdi
call   1070 <echo_env@plt>
lea    0xd36(%rip),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1050 <setenv@plt>
test   %eax,%eax
je     132a <main+0x18a>
lea    0xd0f(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rdi
call   1070 <echo_env@plt>
lea    0xd0a(%rip),%rsi
mov    %rbx,%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
test   %eax,%eax
je     135b <main+0x1bb>
lea    0xcde(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rdi
call   1070 <echo_env@plt>
xor    %eax,%eax
call   1060 <clearenv@plt>
test   %eax,%eax
je     137a <main+0x1da>
lea    0xcde(%rip),%rdi
call   1030 <puts@plt>
mov    0x2c67(%rip),%rax
mov    (%rax),%rsi
lea    0xcba(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rdi
call   1070 <echo_env@plt>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [21 x i8] c"Usage: %s env value\0A\00"
@.str.1 = [6 x i8] c"%s=%s\00"
@.str.2 = [7 x i8] c"putenv\00"
@.str.3 = [11 x i8] c" (deleted)\00"
@.str.4 = [20 x i8] c"unsetenv %s failed\0A\00"
@.str.5 = [18 x i8] c"setenv %s failed\0A\00"
@.str.6 = [2 x i8] c"1\00"
@.str.7 = [2 x i8] c"2\00"
@.str.8 = [17 x i8] c"clearenv failed\0A\00"
@.str.9 = [14 x i8] c"environ = %p\0A\00"
@environ = external global i8**
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [256 x i8]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %12, %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = call i32 @echo_env(i8* %21)
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
store i8* %25, i8** %7
%26 = bitcast [256 x i8]* %8 to i8*
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%28 = load i8*, i8** %6
%29 = load i8*, i8** %7
%30 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %28, i8* %29)
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%32 = call i32 @putenv(i8* %31)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %17
%36 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %17
%38 = load i8*, i8** %6
%39 = call i32 @echo_env(i8* %38)
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%41 = call i32 @strcat(i8* %40, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%42 = load i8*, i8** %6
%43 = call i32 @echo_env(i8* %42)
%44 = load i8*, i8** %6
%45 = call i32 @unsetenv(i8* %44)
store i32 %45, i32* %9
%46 = load i32, i32* %9
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %37
%49 = load i8*, i8** %6
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %49)
br label %51
51: ; preds = %48, %37
%52 = load i8*, i8** %6
%53 = call i32 @echo_env(i8* %52)
%54 = load i8*, i8** %6
%55 = call i32 @unsetenv(i8* %54)
store i32 %55, i32* %9
%56 = load i32, i32* %9
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %51
%59 = load i8*, i8** %6
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %59)
br label %61
61: ; preds = %58, %51
%62 = load i8*, i8** %6
%63 = call i32 @echo_env(i8* %62)
%64 = load i8*, i8** %6
%65 = load i8*, i8** %7
%66 = call i32 @setenv(i8* %64, i8* %65, i32 0)
store i32 %66, i32* %9
%67 = load i32, i32* %9
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %61
%70 = load i8*, i8** %6
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %70)
br label %72
72: ; preds = %69, %61
%73 = load i8*, i8** %6
%74 = call i32 @echo_env(i8* %73)
%75 = load i8*, i8** %6
%76 = call i32 @setenv(i8* %75, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 0)
store i32 %76, i32* %9
%77 = load i32, i32* %9
%78 = icmp ne i32 %77, 0
br i1 %78, label %79, label %82
79: ; preds = %72
%80 = load i8*, i8** %6
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %80)
br label %82
82: ; preds = %79, %72
%83 = load i8*, i8** %6
%84 = call i32 @echo_env(i8* %83)
%85 = load i8*, i8** %6
%86 = call i32 @setenv(i8* %85, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 1)
store i32 %86, i32* %9
%87 = load i32, i32* %9
%88 = icmp ne i32 %87, 0
br i1 %88, label %89, label %92
89: ; preds = %82
%90 = load i8*, i8** %6
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %90)
br label %92
92: ; preds = %89, %82
%93 = load i8*, i8** %6
%94 = call i32 @echo_env(i8* %93)
%95 = call i32 (...) @clearenv()
store i32 %95, i32* %9
%96 = load i32, i32* %9
%97 = icmp ne i32 %96, 0
br i1 %97, label %98, label %100
98: ; preds = %92
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
br label %100
100: ; preds = %98, %92
%101 = load i8**, i8*** @environ
%102 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0), i8** %101)
%103 = load i8*, i8** %6
%104 = call i32 @echo_env(i8* %103)
ret i32 0
}

LLVM IR (-O2):
@.str = [21 x i8] c"Usage: %s env value\0A\00"
@.str.1 = [6 x i8] c"%s=%s\00"
@.str.2 = [7 x i8] c"putenv\00"
@.str.3 = [11 x i8] c" (deleted)\00"
@.str.4 = [20 x i8] c"unsetenv %s failed\0A\00"
@.str.5 = [18 x i8] c"setenv %s failed\0A\00"
@.str.6 = [2 x i8] c"1\00"
@.str.7 = [2 x i8] c"2\00"
@.str.9 = [14 x i8] c"environ = %p\0A\00"
@environ = external global i8**
@str = [16 x i8] c"clearenv failed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = icmp slt i32 %0, 3
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %8
8: ; preds = %5, %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @echo_env(i8* %10)
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%15 = call i32 @sprintf(i8* %14, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %10, i8* %13)
%16 = call i32 @putenv(i8* %14)
%17 = icmp eq i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %8
%19 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %8
%21 = call i32 @echo_env(i8* %10)
%22 = call i32 @strcat(i8* %14, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @echo_env(i8* %10)
%24 = call i32 @unsetenv(i8* %10)
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %20
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %10)
br label %28
28: ; preds = %26, %20
%29 = call i32 @echo_env(i8* %10)
%30 = call i32 @unsetenv(i8* %10)
%31 = icmp eq i32 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %28
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %10)
br label %34
34: ; preds = %32, %28
%35 = call i32 @echo_env(i8* %10)
%36 = call i32 @setenv(i8* %10, i8* %13, i32 0)
%37 = icmp eq i32 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %34
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %10)
br label %40
40: ; preds = %38, %34
%41 = call i32 @echo_env(i8* %10)
%42 = call i32 @setenv(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 0)
%43 = icmp eq i32 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %40
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %10)
br label %46
46: ; preds = %44, %40
%47 = call i32 @echo_env(i8* %10)
%48 = call i32 @setenv(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 1)
%49 = icmp eq i32 %48, 0
br i1 %49, label %52, label %50
50: ; preds = %46
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %10)
br label %52
52: ; preds = %50, %46
%53 = call i32 @echo_env(i8* %10)
%54 = call i32 (...) @clearenv()
%55 = icmp eq i32 %54, 0
br i1 %55, label %58, label %56
56: ; preds = %52
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %58
58: ; preds = %56, %52
%59 = load i8**, i8*** @environ
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0), i8** %59)
%61 = call i32 @echo_env(i8* %10)
ret i32 0
}