#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 */
char *getenv(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setenv(char *, char *, int /*<<< orphan*/);
int main(int argc, char *args[]) {
  extern char **environ;
  int i = 0;
  char *env;
  while ((env = environ[i]) != NULL) {
    printf("%s\n", env);
    i++;
  }
  printf("===============================\n");
  env = getenv("myhome");
  printf("myhome = %s\n", env);
  setenv("myhome", "hello world", 0);
  env = getenv("myhome");
  printf("myhome = %s\n", env);
  return 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)
movl   $0x0,-0x14(%rbp)
mov    0x2e94(%rip),%rax
mov    (%rax),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     118d <main+0x5d>
mov    -0x20(%rbp),%rsi
lea    0xeb9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    114d <main+0x1d>
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe7f(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe5d(%rip),%rdi
lea    0xe6a(%rip),%rsi
xor    %edx,%edx
call   1040 <setenv@plt>
lea    0xe48(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e9d(%rip),%r14
mov    (%r14),%rax
mov    (%rax),%rdi
test   %rdi,%rdi
je     1175 <main+0x35>
mov    $0x8,%ebx
nopl   0x0(%rax,%rax,1)
call   1040 <puts@plt>
mov    (%r14),%rax
mov    (%rax,%rbx,1),%rdi
add    $0x8,%rbx
test   %rdi,%rdi
jne    1160 <main+0x20>
lea    0xea4(%rip),%rdi
call   1040 <puts@plt>
lea    0xe78(%rip),%rbx
mov    %rbx,%rdi
call   1030 <getenv@plt>
lea    0xe70(%rip),%r14
mov    %r14,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe69(%rip),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1050 <setenv@plt>
mov    %rbx,%rdi
call   1030 <getenv@plt>
mov    %r14,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@environ = external global i8**
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [33 x i8] c"===============================\0A\00"
@.str.2 = [7 x i8] c"myhome\00"
@.str.3 = [13 x i8] c"myhome = %s\0A\00"
@.str.4 = [12 x i8] c"hello world\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
br label %8
8: ; preds = %15, %2
%9 = load i8**, i8*** @environ
%10 = load i32, i32* %6
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8*, i8** %9, i64 %11
%13 = load i8*, i8** %12
store i8* %13, i8** %7
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %20
15: ; preds = %8
%16 = load i8*, i8** %7
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* %6
%19 = add nsw i32 %18, 1
store i32 %19, i32* %6
br label %8
20: ; preds = %8
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%22 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %23)
%25 = call i32 @setenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 0)
%26 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i8* %26, i8** %7
%27 = load i8*, i8** %7
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %27)
ret i32 0
}

LLVM IR (-O2):
@environ = external global i8**
@.str.2 = [7 x i8] c"myhome\00"
@.str.3 = [13 x i8] c"myhome = %s\0A\00"
@.str.4 = [12 x i8] c"hello world\00"
@str = [32 x i8] c"===============================\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i8**, i8*** @environ
%4 = load i8*, i8** %3
%5 = icmp eq i8* %4, null
br i1 %5, label %15, label %6
6: ; preds = %2, %6
%7 = phi i64 [ %10, %6 ], [ 0, %2 ]
%8 = phi i8* [ %13, %6 ], [ %4, %2 ]
%9 = tail call i32 @puts(i8* %8)
%10 = add nuw i64 %7, 1
%11 = load i8**, i8*** @environ
%12 = getelementptr inbounds i8*, i8** %11, i64 %10
%13 = load i8*, i8** %12
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %6
15: ; preds = %6, %2
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%17 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %17)
%19 = tail call i32 @setenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 0)
%20 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %20)
ret i32 0
}