#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 /*<<< orphan*/ exit(int /*<<< orphan*/);
char *getenv(char *);
int /*<<< orphan*/ oops();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putenv(char *);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ show_env();
int main(int argc, char *argv[]) {
  char *val;
  printf("test environment variables\n");
  printf("show env\n");
  show_env();
  printf("setenv-1\n");
  setenv("PATH", "/boot", 1);
  show_env();
  printf("setenv-2\n");
  setenv("PATH", "/boot:/bin", 1);
  show_env();
  printf("setenv-3\n");
  setenv("PATH", "/boot", 0);
  show_env();
  printf("setenv-4\n");
  setenv("PATH", "/boot", 0);
  show_env();
  printf("getenv-1\n");
  val = getenv("TMP");
  if (val != NULL)
    oops();
  printf("getenv-2\n");
  val = getenv("PATH");
  if (val == NULL)
    oops();
  printf("PATH=%s\n", val);
  printf("putenv-1\n");
  putenv("TMP=/tmp");
  show_env();
  printf("putenv-2\n");
  putenv("ABC=/abc");
  show_env();
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x0,%al
call   1060 <show_env@plt>
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xe72(%rip),%rdi
lea    0xe70(%rip),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    $0x0,%al
call   1060 <show_env@plt>
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xe45(%rip),%rdi
lea    0xe53(%rip),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    $0x0,%al
call   1060 <show_env@plt>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xe18(%rip),%rdi
lea    0xe16(%rip),%rsi
xor    %edx,%edx
call   1040 <setenv@plt>
mov    $0x0,%al
call   1060 <show_env@plt>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xdee(%rip),%rdi
lea    0xdec(%rip),%rsi
xor    %edx,%edx
call   1040 <setenv@plt>
mov    $0x0,%al
call   1060 <show_env@plt>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xe02(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1287 <main+0x117>
mov    $0x0,%al
call   1050 <oops@plt>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xd94(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12b7 <main+0x147>
mov    $0x0,%al
call   1050 <oops@plt>
mov    -0x18(%rbp),%rsi
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xdb1(%rip),%rdi
call   1080 <putenv@plt>
mov    $0x0,%al
call   1060 <show_env@plt>
lea    0xda7(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xda3(%rip),%rdi
call   1080 <putenv@plt>
mov    $0x0,%al
call   1060 <show_env@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xeaa(%rip),%rdi
call   1040 <puts@plt>
lea    0xeb9(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1070 <show_env@plt>
lea    0xeaf(%rip),%rdi
call   1040 <puts@plt>
lea    0xe4a(%rip),%rbx
lea    0xe48(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
xor    %eax,%eax
call   1070 <show_env@plt>
lea    0xe87(%rip),%rdi
call   1040 <puts@plt>
lea    0xe24(%rip),%rsi
mov    %rbx,%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
xor    %eax,%eax
call   1070 <show_env@plt>
lea    0xe69(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <setenv@plt>
xor    %eax,%eax
call   1070 <show_env@plt>
lea    0xe52(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <setenv@plt>
xor    %eax,%eax
call   1070 <show_env@plt>
lea    0xe3b(%rip),%rdi
call   1040 <puts@plt>
lea    0xdc8(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     125f <main+0xdf>
xor    %eax,%eax
call   1060 <oops@plt>
lea    0xe20(%rip),%rdi
call   1040 <puts@plt>
lea    0xd8e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1286 <main+0x106>
xor    %eax,%eax
call   1060 <oops@plt>
lea    0xd8d(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xdf1(%rip),%rdi
call   1040 <puts@plt>
lea    0xd79(%rip),%rdi
call   1090 <putenv@plt>
xor    %eax,%eax
call   1070 <show_env@plt>
lea    0xddb(%rip),%rdi
call   1040 <puts@plt>
lea    0xd63(%rip),%rdi
call   1090 <putenv@plt>
xor    %eax,%eax
call   1070 <show_env@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"test environment variables\0A\00"
@.str.1 = [10 x i8] c"show env\0A\00"
@.str.2 = [10 x i8] c"setenv-1\0A\00"
@.str.3 = [5 x i8] c"PATH\00"
@.str.4 = [6 x i8] c"/boot\00"
@.str.5 = [10 x i8] c"setenv-2\0A\00"
@.str.6 = [11 x i8] c"/boot:/bin\00"
@.str.7 = [10 x i8] c"setenv-3\0A\00"
@.str.8 = [10 x i8] c"setenv-4\0A\00"
@.str.9 = [10 x i8] c"getenv-1\0A\00"
@.str.10 = [4 x i8] c"TMP\00"
@.str.11 = [10 x i8] c"getenv-2\0A\00"
@.str.12 = [9 x i8] c"PATH=%s\0A\00"
@.str.13 = [10 x i8] c"putenv-1\0A\00"
@.str.14 = [9 x i8] c"TMP=/tmp\00"
@.str.15 = [10 x i8] c"putenv-2\0A\00"
@.str.16 = [9 x i8] c"ABC=/abc\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 (...) @show_env()
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 1)
%12 = call i32 (...) @show_env()
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%14 = call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 1)
%15 = call i32 (...) @show_env()
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%17 = call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 0)
%18 = call i32 (...) @show_env()
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
%20 = call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 0)
%21 = call i32 (...) @show_env()
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0))
%23 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.10, i64 0, i64 0))
store i8* %23, i8** %6
%24 = load i8*, i8** %6
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %2
%27 = call i32 (...) @oops()
br label %28
28: ; preds = %26, %2
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.11, i64 0, i64 0))
%30 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i8* %30, i8** %6
%31 = load i8*, i8** %6
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 (...) @oops()
br label %35
35: ; preds = %33, %28
%36 = load i8*, i8** %6
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.12, i64 0, i64 0), i8* %36)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.13, i64 0, i64 0))
%39 = call i32 @putenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.14, i64 0, i64 0))
%40 = call i32 (...) @show_env()
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.15, i64 0, i64 0))
%42 = call i32 @putenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.16, i64 0, i64 0))
%43 = call i32 (...) @show_env()
%44 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.3 = [5 x i8] c"PATH\00"
@.str.4 = [6 x i8] c"/boot\00"
@.str.6 = [11 x i8] c"/boot:/bin\00"
@.str.10 = [4 x i8] c"TMP\00"
@.str.12 = [9 x i8] c"PATH=%s\0A\00"
@.str.14 = [9 x i8] c"TMP=/tmp\00"
@.str.16 = [9 x i8] c"ABC=/abc\00"
@str = [27 x i8] c"test environment variables\00"
@str.17 = [9 x i8] c"show env\00"
@str.18 = [9 x i8] c"setenv-1\00"
@str.19 = [9 x i8] c"setenv-2\00"
@str.20 = [9 x i8] c"setenv-3\00"
@str.21 = [9 x i8] c"setenv-4\00"
@str.22 = [9 x i8] c"getenv-1\00"
@str.23 = [9 x i8] c"getenv-2\00"
@str.24 = [9 x i8] c"putenv-1\00"
@str.25 = [9 x i8] c"putenv-2\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.17, i64 0, i64 0))
%5 = tail call i32 (...) @show_env()
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.18, i64 0, i64 0))
%7 = tail call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 1)
%8 = tail call i32 (...) @show_env()
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.19, i64 0, i64 0))
%10 = tail call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 1)
%11 = tail call i32 (...) @show_env()
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.20, i64 0, i64 0))
%13 = tail call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 0)
%14 = tail call i32 (...) @show_env()
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.21, i64 0, i64 0))
%16 = tail call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 0)
%17 = tail call i32 (...) @show_env()
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.22, i64 0, i64 0))
%19 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.10, i64 0, i64 0))
%20 = icmp eq i8* %19, null
br i1 %20, label %23, label %21
21: ; preds = %2
%22 = tail call i32 (...) @oops()
br label %23
23: ; preds = %21, %2
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.23, i64 0, i64 0))
%25 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = tail call i32 (...) @oops()
br label %29
29: ; preds = %27, %23
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.12, i64 0, i64 0), i8* %25)
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.24, i64 0, i64 0))
%32 = tail call i32 @putenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.14, i64 0, i64 0))
%33 = tail call i32 (...) @show_env()
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.25, i64 0, i64 0))
%35 = tail call i32 @putenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.16, i64 0, i64 0))
%36 = tail call i32 (...) @show_env()
%37 = tail call i32 @exit(i32 0)
unreachable
}