#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *getenv(char *);
int /*<<< orphan*/ printf(char *, char *);
int setenv(char *, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int unsetenv(char *);
int main(int argc, char **argv) {
  printf("Initially, FOO = %s\n", getenv("FOO"));
  if (setenv("FOO", "one", 0) == -1) {
    fprintf(stderr, "setenv: %s\n", strerror(errno));
    return 1;
  }
  printf("After update without overwrite, FOO = %s\n", getenv("FOO"));
  if (setenv("FOO", "two", 1) == -1) {
    fprintf(stderr, "setenv: %s\n", strerror(errno));
    return 1;
  }
  printf("After update with overwrite, FOO = %s\n", getenv("FOO"));
  if (unsetenv("FOO") == -1) {
    fprintf(stderr, "unsetenv: %s\n", strerror(errno));
    return 1;
  }
  printf("After unset, FOO = %s\n", getenv("FOO"));
  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)
lea    0xe98(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rsi
lea    0xe74(%rip),%rdi
call   1050 <printf@plt>
lea    0xe7d(%rip),%rdi
lea    0xe7a(%rip),%rsi
xor    %edx,%edx
call   1040 <setenv@plt>
cmp    $0xffffffff,%eax
jne    11e7 <main+0x87>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rdx
lea    0xe9e(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e5 <main+0x185>
lea    0xe27(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rsi
lea    0xe20(%rip),%rdi
call   1050 <printf@plt>
lea    0xe0c(%rip),%rdi
lea    0xe37(%rip),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
cmp    $0xffffffff,%eax
jne    125b <main+0xfb>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe2a(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e5 <main+0x185>
lea    0xdb3(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rsi
lea    0xdda(%rip),%rdi
call   1050 <printf@plt>
lea    0xd98(%rip),%rdi
call   1070 <unsetenv@plt>
cmp    $0xffffffff,%eax
jne    12c3 <main+0x163>
mov    0x2d4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xdc0(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e5 <main+0x185>
lea    0xd4b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rsi
lea    0xda7(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xeaa(%rip),%rbx
mov    %rbx,%rdi
call   1030 <getenv@plt>
lea    0xe86(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
lea    0xe90(%rip),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1040 <setenv@plt>
cmp    $0xffffffff,%eax
je     1222 <main+0xc2>
lea    0xe72(%rip),%r14
mov    %r14,%rdi
call   1030 <getenv@plt>
lea    0xe6b(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
lea    0xe86(%rip),%rsi
mov    $0x1,%ebx
mov    %r14,%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
cmp    $0xffffffff,%eax
je     1242 <main+0xe2>
lea    0xe36(%rip),%rbx
mov    %rbx,%rdi
call   1030 <getenv@plt>
lea    0xe5d(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1070 <unsetenv@plt>
cmp    $0xffffffff,%eax
je     126c <main+0x10c>
lea    0xe0b(%rip),%rdi
call   1030 <getenv@plt>
lea    0xe6a(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
xor    %ebx,%ebx
jmp    1299 <main+0x139>
mov    0x2db7(%rip),%rax
mov    (%rax),%ebx
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe34(%rip),%rsi
jmp    128a <main+0x12a>
mov    0x2d97(%rip),%rax
mov    (%rax),%ebp
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe14(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
jmp    1299 <main+0x139>
mov    0x2d6d(%rip),%rax
mov    (%rax),%ebx
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xde8(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"Initially, FOO = %s\0A\00"
@.str.1 = [4 x i8] c"FOO\00"
@.str.2 = [4 x i8] c"one\00"
@stderr = global i32 0
@.str.3 = [12 x i8] c"setenv: %s\0A\00"
@errno = global i32 0
@.str.4 = [42 x i8] c"After update without overwrite, FOO = %s\0A\00"
@.str.5 = [4 x i8] c"two\00"
@.str.6 = [39 x i8] c"After update with overwrite, FOO = %s\0A\00"
@.str.7 = [14 x i8] c"unsetenv: %s\0A\00"
@.str.8 = [23 x i8] c"After unset, FOO = %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%6 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%7 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %13)
store i32 1, i32* %3
br label %38
15: ; preds = %2
%16 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %16)
%18 = call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 1)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %25
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %23)
store i32 1, i32* %3
br label %38
25: ; preds = %15
%26 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i8* %26)
%28 = call i32 @unsetenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %25
%31 = load i32, i32* @stderr
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i8* %33)
store i32 1, i32* %3
br label %38
35: ; preds = %25
%36 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0), i8* %36)
store i32 0, i32* %3
br label %38
38: ; preds = %35, %30, %20, %10
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [21 x i8] c"Initially, FOO = %s\0A\00"
@.str.1 = [4 x i8] c"FOO\00"
@.str.2 = [4 x i8] c"one\00"
@stderr = global i32 0
@.str.3 = [12 x i8] c"setenv: %s\0A\00"
@errno = global i32 0
@.str.4 = [42 x i8] c"After update without overwrite, FOO = %s\0A\00"
@.str.5 = [4 x i8] c"two\00"
@.str.6 = [39 x i8] c"After update with overwrite, FOO = %s\0A\00"
@.str.7 = [14 x i8] c"unsetenv: %s\0A\00"
@.str.8 = [23 x i8] c"After unset, FOO = %s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %3)
%5 = tail call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %10)
br label %35
12: ; preds = %2
%13 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %13)
%15 = tail call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 1)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %22
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %20)
br label %35
22: ; preds = %12
%23 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%24 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i8* %23)
%25 = tail call i32 @unsetenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %32
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = tail call i8* @strerror(i32 %29)
%31 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i8* %30)
br label %35
32: ; preds = %22
%33 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%34 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %32, %27, %17, %7
%36 = phi i32 [ 1, %7 ], [ 1, %17 ], [ 1, %27 ], [ 0, %32 ]
ret i32 %36
}