#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);
char *getenv(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int putenv(char *);
int main(void) {
  char *p;
  p = getenv("HOME");
  if (p == NULL) {
    perror("fail to getenv");
    exit(1);
  }
  printf("$HOME is %s\n", p);
  if (putenv("HOME=/home/admin") == -1) {
    perror("fail to putenv");
    exit(1);
  }
  p = getenv("HOME");
  if (p == NULL) {
    perror("fail to getenv");
    exit(1);
  }
  printf("$HOME is %s \n", p);
  if (putenv("ADMIN=hello") == -1) {
    perror("fail to putenv");
    exit(1);
  }
  p = getenv("ADMIN");
  if (p == NULL) {
    perror("fail to getenv");
    exit(1);
  }
  printf("$ADMIN is %s \n", p);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1190 <main+0x40>
lea    0xe84(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rsi
lea    0xe79(%rip),%rdi
call   1040 <printf@plt>
lea    0xe7a(%rip),%rdi
call   1050 <putenv@plt>
cmp    $0xffffffff,%eax
jne    11cb <main+0x7b>
lea    0xe76(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe2e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11fc <main+0xac>
lea    0xe18(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rsi
lea    0xe3a(%rip),%rdi
call   1040 <printf@plt>
lea    0xe3c(%rip),%rdi
call   1050 <putenv@plt>
cmp    $0xffffffff,%eax
jne    1237 <main+0xe7>
lea    0xe0a(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe1d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1268 <main+0x118>
lea    0xdac(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rsi
lea    0xdee(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xea8(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11d7 <main+0x87>
lea    0xeab(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
lea    0xea9(%rip),%rdi
call   1050 <putenv@plt>
cmp    $0xffffffff,%eax
je     11e0 <main+0x90>
lea    0xe77(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11d7 <main+0x87>
lea    0xea7(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
lea    0xea6(%rip),%rdi
call   1050 <putenv@plt>
cmp    $0xffffffff,%eax
je     11e0 <main+0x90>
lea    0xea1(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11d7 <main+0x87>
lea    0xe96(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe27(%rip),%rdi
jmp    11e7 <main+0x97>
lea    0xe4b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [15 x i8] c"fail to getenv\00"
@.str.2 = [13 x i8] c"$HOME is %s\0A\00"
@.str.3 = [17 x i8] c"HOME=/home/admin\00"
@.str.4 = [15 x i8] c"fail to putenv\00"
@.str.5 = [14 x i8] c"$HOME is %s \0A\00"
@.str.6 = [12 x i8] c"ADMIN=hello\00"
@.str.7 = [6 x i8] c"ADMIN\00"
@.str.8 = [15 x i8] c"$ADMIN is %s \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
store i32 0, i32* %1
%3 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %3, i8** %2
%4 = load i8*, i8** %2
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i8*, i8** %2
%11 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %10)
%12 = call i32 @putenv(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %9
%18 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %18, i8** %2
%19 = load i8*, i8** %2
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %17
%25 = load i8*, i8** %2
%26 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %25)
%27 = call i32 @putenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %24
%33 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
store i8* %33, i8** %2
%34 = load i8*, i8** %2
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %32
%40 = load i8*, i8** %2
%41 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i8* %40)
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [15 x i8] c"fail to getenv\00"
@.str.2 = [13 x i8] c"$HOME is %s\0A\00"
@.str.3 = [17 x i8] c"HOME=/home/admin\00"
@.str.4 = [15 x i8] c"fail to putenv\00"
@.str.5 = [14 x i8] c"$HOME is %s \0A\00"
@.str.6 = [12 x i8] c"ADMIN=hello\00"
@.str.7 = [6 x i8] c"ADMIN\00"
@.str.8 = [15 x i8] c"$ADMIN is %s \0A\00"
define i32 @main() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%8 = tail call i32 @putenv(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %6
%14 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %14)
%21 = tail call i32 @putenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %19
%27 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %26
%33 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i8* %27)
ret i32 0
}