#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*/ asprintf(char **, char *, char *, char *);
int /*<<< orphan*/ putenv(char *);
int update_environment(char *name, char *value, int set) {
  char *env_string = NULL;
  if (name == NULL)
    return -1;
  if (set == 1) {
    asprintf(&env_string, "%s=%s", name, (value == NULL) ? "" : value);
    if (env_string)
      putenv(env_string);
  } else {
  }
  return 0;
}


x86 assembly (clang, -O0):
<update_environment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1152 <update_environment+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c4 <update_environment+0xa4>
cmpl   $0x1,-0x1c(%rbp)
jne    11b8 <update_environment+0x98>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117f <update_environment+0x5f>
lea    0xe8f(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    1187 <update_environment+0x67>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
lea    -0x28(%rbp),%rdi
lea    0xe66(%rip),%rsi
call   1040 <asprintf@plt>
cmpq   $0x0,-0x28(%rbp)
je     11b3 <update_environment+0x93>
mov    -0x28(%rbp),%rdi
call   1030 <putenv@plt>
jmp    11bd <update_environment+0x9d>
jmp    11bd <update_environment+0x9d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<update_environment>:
push   %rbx
sub    $0x10,%rsp
movq   $0x0,0x8(%rsp)
test   %rdi,%rdi
je     1170 <update_environment+0x50>
xor    %ebx,%ebx
cmp    $0x1,%edx
jne    1175 <update_environment+0x55>
mov    %rdi,%rax
test   %rsi,%rsi
lea    0xebe(%rip),%rcx
cmovne %rsi,%rcx
lea    0xeae(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %rax,%rdx
call   1040 <asprintf@plt>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     1175 <update_environment+0x55>
call   1030 <putenv@plt>
jmp    1175 <update_environment+0x55>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s=%s\00"
@.str.1 = [1 x i8] zeroinitializer
define i32 @update_environment(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8* null, i8** %8
%9 = load i8*, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %3
store i32 -1, i32* %4
br label %33
12: ; preds = %3
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %31
15: ; preds = %12
%16 = load i8*, i8** %5
%17 = load i8*, i8** %6
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %20
19: ; preds = %15
br label %22
20: ; preds = %15
%21 = load i8*, i8** %6
br label %22
22: ; preds = %20, %19
%23 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %19 ], [ %21, %20 ]
%24 = call i32 @asprintf(i8** %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %23)
%25 = load i8*, i8** %8
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i8*, i8** %8
%29 = call i32 @putenv(i8* %28)
br label %30
30: ; preds = %27, %22
br label %32
31: ; preds = %12
br label %32
32: ; preds = %31, %30
store i32 0, i32* %4
br label %33
33: ; preds = %32, %11
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s=%s\00"
@.str.1 = [1 x i8] zeroinitializer
define i32 @update_environment(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%6 = icmp eq i8* %0, null
br i1 %6, label %17, label %7
7: ; preds = %3
%8 = icmp eq i32 %2, 1
br i1 %8, label %9, label %17
9: ; preds = %7
%10 = icmp eq i8* %1, null
%11 = select i1 %10, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* %1
%12 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %11)
%13 = load i8*, i8** %4
%14 = icmp eq i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %9
%16 = call i32 @putenv(i8* %13)
br label %17
17: ; preds = %15, %9, %7, %3
%18 = phi i32 [ -1, %3 ], [ 0, %7 ], [ 0, %9 ], [ 0, %15 ]
ret i32 %18
}