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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a0 <main+0x40>
lea    0xe74(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rsi
lea    0xe69(%rip),%rdi
call   1050 <printf@plt>
lea    0xe49(%rip),%rdi
call   1070 <unsetenv@plt>
cmp    $0xffffffff,%eax
jne    11db <main+0x7b>
lea    0xe55(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe1e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1206 <main+0xa6>
mov    -0x10(%rbp),%rsi
lea    0xe31(%rip),%rdi
call   1050 <printf@plt>
lea    0xdf3(%rip),%rdi
lea    0xe2c(%rip),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
cmp    $0xffffffff,%eax
jne    123d <main+0xdd>
lea    0xe1e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdbc(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    126e <main+0x10e>
lea    0xda6(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rsi
lea    0xdb9(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xe98(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11f6 <main+0x96>
lea    0xe97(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
lea    0xe74(%rip),%rdi
call   1070 <unsetenv@plt>
cmp    $0xffffffff,%eax
je     11ff <main+0x9f>
lea    0xe63(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
jne    11b5 <main+0x55>
lea    0xe84(%rip),%rdi
xor    %esi,%esi
call   1050 <printf@plt>
lea    0xe44(%rip),%rdi
lea    0xe7d(%rip),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
cmp    $0xffffffff,%eax
je     1208 <main+0xa8>
lea    0xe27(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11f6 <main+0x96>
lea    0xe48(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe08(%rip),%rdi
jmp    120f <main+0xaf>
lea    0xe1b(%rip),%rdi
jmp    120f <main+0xaf>
lea    0xe3d(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <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"fail to unsetenv\00"
@.str.4 = [14 x i8] c"$HOME is %s \0A\00"
@.str.5 = [12 x i8] c"/home/admin\00"
@.str.6 = [15 x i8] c"fail to setenv\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 @unsetenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, 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 ([17 x i8], [17 x i8]* @.str.3, 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 = load i8*, i8** %2
%23 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %21, %17
%25 = call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 1)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %24
%31 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %31, i8** %2
%32 = load i8*, i8** %2
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %30
%38 = load i8*, i8** %2
%39 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %38)
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"fail to unsetenv\00"
@.str.4 = [14 x i8] c"$HOME is %s \0A\00"
@.str.5 = [12 x i8] c"/home/admin\00"
@.str.6 = [15 x i8] c"fail to setenv\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 @unsetenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, 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 ([17 x i8], [17 x i8]* @.str.3, 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 %18
16: ; preds = %13
%17 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* null)
br label %18
18: ; preds = %16, %13
%19 = tail call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 1)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %18
%25 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %24
%31 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %25)
ret i32 0
}