#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);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ printf(char *, char *, ...);
scalar_t__ putenv(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int main(int argc, char *argv[]) {
  char *var, *value;
  if (argc == 1 || argc > 3) {
    fprintf(stderr, "usage: environ var [value]\n");
    exit(1);
  }
  var = argv[1];
  value = getenv(var);
  if (value)
    printf("Variable %s has value %s\n", var, value);
  else
    printf("Variable %s has no value\n", var);
  if (argc == 3) {
    char *string;
    value = argv[2];
    string = malloc(strlen(var) + strlen(value) + 2);
    if (!string) {
      fprintf(stderr, "out of memory\n");
      exit(1);
    }
    strcpy(string, var);
    strcat(string, "=");
    strcat(string, value);
    printf("Calling putenv with: %s\n", string);
    if (putenv(string) != 0) {
      fprintf(stderr, "putenv failed\n");
      free(string);
      exit(1);
    }
    value = getenv(var);
    if (value)
      printf("New value of %s is %s\n", var, value);
    else
      printf("New value of %s is null??\n", var);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
je     11ca <main+0x2a>
cmpl   $0x3,-0x8(%rbp)
jle    11e9 <main+0x49>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1228 <main+0x88>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    123a <main+0x9a>
mov    -0x18(%rbp),%rsi
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
cmpl   $0x3,-0x8(%rbp)
jne    1365 <main+0x1c5>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12a4 <main+0x104>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
lea    0xdbb(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x28(%rbp),%rdi
lea    0xda3(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10b0 <strcat@plt>
mov    -0x28(%rbp),%rsi
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1080 <putenv@plt>
cmp    $0x0,%rax
je     131b <main+0x17b>
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
lea    0xd77(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     134e <main+0x1ae>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xd47(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1360 <main+0x1c0>
mov    -0x18(%rbp),%rsi
lea    0xd47(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1365 <main+0x1c5>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x1,%edi
je     12b2 <main+0x112>
mov    %edi,%ebp
cmp    $0x4,%edi
jge    12b2 <main+0x112>
mov    %rsi,%rbx
mov    0x8(%rsi),%r14
mov    %r14,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11ea <main+0x4a>
lea    0xe49(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
cmp    $0x3,%ebp
je     1204 <main+0x64>
jmp    12ab <main+0x10b>
lea    0xe45(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
cmp    $0x3,%ebp
jne    12ab <main+0x10b>
mov    0x10(%rbx),%rbp
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1060 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   10a0 <malloc@plt>
test   %rax,%rax
je     12c4 <main+0x124>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1050 <strcpy@plt>
lea    0xe19(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <strcat@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10b0 <strcat@plt>
lea    0xe01(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1080 <putenv@plt>
test   %rax,%rax
jne    12e3 <main+0x143>
mov    %r14,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     129a <main+0xfa>
lea    0xdfe(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
jmp    12ab <main+0x10b>
lea    0xdff(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
lea    0xd3e(%rip),%rsi
jmp    12d4 <main+0x134>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
lea    0xd7c(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
lea    0xd87(%rip),%rsi
call   1090 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"usage: environ var [value]\0A\00"
@.str.1 = [26 x i8] c"Variable %s has value %s\0A\00"
@.str.2 = [26 x i8] c"Variable %s has no value\0A\00"
@.str.3 = [15 x i8] c"out of memory\0A\00"
@.str.4 = [2 x i8] c"=\00"
@.str.5 = [25 x i8] c"Calling putenv with: %s\0A\00"
@.str.6 = [15 x i8] c"putenv failed\0A\00"
@.str.7 = [23 x i8] c"New value of %s is %s\0A\00"
@.str.8 = [27 x i8] c"New value of %s is null??\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 1
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 3
br i1 %13, label %14, label %18
14: ; preds = %11, %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = call i8* @getenv(i8* %22)
store i8* %23, i8** %7
%24 = load i8*, i8** %7
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %30
26: ; preds = %18
%27 = load i8*, i8** %6
%28 = load i8*, i8** %7
%29 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* %28)
br label %33
30: ; preds = %18
%31 = load i8*, i8** %6
%32 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %30, %26
%34 = load i32, i32* %4
%35 = icmp eq i32 %34, 3
br i1 %35, label %36, label %86
36: ; preds = %33
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 2
%39 = load i8*, i8** %38
store i8* %39, i8** %7
%40 = load i8*, i8** %6
%41 = call i64 @strlen(i8* %40)
%42 = load i8*, i8** %7
%43 = call i64 @strlen(i8* %42)
%44 = add nsw i64 %41, %43
%45 = add nsw i64 %44, 2
%46 = call i8* @malloc(i64 %45)
store i8* %46, i8** %8
%47 = load i8*, i8** %8
%48 = icmp ne i8* %47, null
br i1 %48, label %53, label %49
49: ; preds = %36
%50 = load i32, i32* @stderr
%51 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %36
%54 = load i8*, i8** %8
%55 = load i8*, i8** %6
%56 = call i32 @strcpy(i8* %54, i8* %55)
%57 = load i8*, i8** %8
%58 = call i32 @strcat(i8* %57, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%59 = load i8*, i8** %8
%60 = load i8*, i8** %7
%61 = call i32 @strcat(i8* %59, i8* %60)
%62 = load i8*, i8** %8
%63 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %62)
%64 = load i8*, i8** %8
%65 = call i64 @putenv(i8* %64)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %73
67: ; preds = %53
%68 = load i32, i32* @stderr
%69 = call i32 @fprintf(i32 %68, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%70 = load i8*, i8** %8
%71 = call i32 @free(i8* %70)
%72 = call i32 @exit(i32 1)
unreachable
73: ; preds = %53
%74 = load i8*, i8** %6
%75 = call i8* @getenv(i8* %74)
store i8* %75, i8** %7
%76 = load i8*, i8** %7
%77 = icmp ne i8* %76, null
br i1 %77, label %78, label %82
78: ; preds = %73
%79 = load i8*, i8** %6
%80 = load i8*, i8** %7
%81 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i8* %79, i8* %80)
br label %85
82: ; preds = %73
%83 = load i8*, i8** %6
%84 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0), i8* %83)
br label %85
85: ; preds = %82, %78
br label %86
86: ; preds = %85, %33
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"usage: environ var [value]\0A\00"
@.str.1 = [26 x i8] c"Variable %s has value %s\0A\00"
@.str.2 = [26 x i8] c"Variable %s has no value\0A\00"
@.str.3 = [15 x i8] c"out of memory\0A\00"
@.str.4 = [2 x i8] c"=\00"
@.str.5 = [25 x i8] c"Calling putenv with: %s\0A\00"
@.str.6 = [15 x i8] c"putenv failed\0A\00"
@.str.7 = [23 x i8] c"New value of %s is %s\0A\00"
@.str.8 = [27 x i8] c"New value of %s is null??\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 1
%4 = icmp sgt i32 %0, 3
%5 = or i1 %3, %4
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i8* @getenv(i8* %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %10
%16 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %12, i8* %13)
br label %19
17: ; preds = %10
%18 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %12)
br label %19
19: ; preds = %17, %15
%20 = icmp eq i32 %0, 3
br i1 %20, label %21, label %53
21: ; preds = %19
%22 = getelementptr inbounds i8*, i8** %1, i64 2
%23 = load i8*, i8** %22
%24 = tail call i64 @strlen(i8* dereferenceable(1) %12)
%25 = tail call i64 @strlen(i8* dereferenceable(1) %23)
%26 = add i64 %24, 2
%27 = add i64 %26, %25
%28 = tail call i8* @malloc(i64 %27)
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %34
30: ; preds = %21
%31 = load i32, i32* @stderr
%32 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %21
%35 = tail call i32 @strcpy(i8* %28, i8* %12)
%36 = tail call i32 @strcat(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%37 = tail call i32 @strcat(i8* %28, i8* %23)
%38 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %28)
%39 = tail call i64 @putenv(i8* %28)
%40 = icmp eq i64 %39, 0
br i1 %40, label %46, label %41
41: ; preds = %34
%42 = load i32, i32* @stderr
%43 = tail call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%44 = tail call i32 @free(i8* %28)
%45 = tail call i32 @exit(i32 1)
unreachable
46: ; preds = %34
%47 = tail call i8* @getenv(i8* %12)
%48 = icmp eq i8* %47, null
br i1 %48, label %51, label %49
49: ; preds = %46
%50 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i8* %12, i8* %47)
br label %53
51: ; preds = %46
%52 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0), i8* %12)
br label %53
53: ; preds = %49, %51, %19
ret i32 0
}