#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 main(void) {
  char *p;
  p = getenv("HOME");
  if (p == NULL) {
    perror("fail to getenv");
    exit(1);
  }
  printf("$HOME is %s\n", p);
  if (setenv("HOME", "/home/admin", 0) == -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);
  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    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   1050 <printf@plt>
lea    0xe59(%rip),%rdi
lea    0xe73(%rip),%rsi
xor    %edx,%edx
call   1040 <setenv@plt>
cmp    $0xffffffff,%eax
jne    11d4 <main+0x84>
lea    0xe68(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe25(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1205 <main+0xb5>
lea    0xe0f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rsi
lea    0xe2c(%rip),%rdi
call   1050 <printf@plt>
lea    0xde4(%rip),%rdi
lea    0xdfe(%rip),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
cmp    $0xffffffff,%eax
jne    124c <main+0xfc>
lea    0xdf0(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xdad(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    127d <main+0x12d>
lea    0xd97(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rsi
lea    0xdb4(%rip),%rdi
call   1050 <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     11f0 <main+0xa0>
lea    0xea7(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
lea    0xe84(%rip),%rdi
lea    0xe9e(%rip),%rsi
xor    %edx,%edx
call   1040 <setenv@plt>
cmp    $0xffffffff,%eax
je     11f9 <main+0xa9>
lea    0xe6a(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11f0 <main+0xa0>
lea    0xe95(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
lea    0xe4a(%rip),%rdi
lea    0xe64(%rip),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
cmp    $0xffffffff,%eax
je     11f9 <main+0xa9>
lea    0xe2d(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11f0 <main+0xa0>
lea    0xe58(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe0e(%rip),%rdi
jmp    1200 <main+0xb0>
lea    0xe2d(%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 = [12 x i8] c"/home/admin\00"
@.str.4 = [15 x i8] c"fail to setenv\00"
@.str.5 = [14 x i8] c"$HOME 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 @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.3, i64 0, i64 0), i32 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 @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.3, i64 0, i64 0), i32 1)
%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 ([5 x i8], [5 x i8]* @.str, 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 ([14 x i8], [14 x i8]* @.str.5, 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 = [12 x i8] c"/home/admin\00"
@.str.4 = [15 x i8] c"fail to setenv\00"
@.str.5 = [14 x i8] c"$HOME 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 @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.3, i64 0, i64 0), i32 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 @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.3, i64 0, i64 0), i32 1)
%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 ([5 x i8], [5 x i8]* @.str, 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 ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %27)
ret i32 0
}