#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 const *, char const *);
char *getenv(char const *);
int putenv(char *);
int nsetenv(const char *name, const char *value, int overwrite) {
  if (!overwrite) {
    const char *envval = getenv(name);
    if (envval != NULL)
      return 0;
  }
  char *buf;
  asprintf(&buf, "%s=%s", name, value);
  return putenv(buf);
}


x86 assembly (clang, -O0):
<nsetenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1176 <nsetenv+0x46>
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1171 <nsetenv+0x41>
movl   $0x0,-0x4(%rbp)
jmp    119a <nsetenv+0x6a>
jmp    1176 <nsetenv+0x46>
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x30(%rbp),%rdi
lea    0xe77(%rip),%rsi
call   1050 <asprintf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <putenv@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nsetenv>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
test   %edx,%edx
jne    1150 <nsetenv+0x20>
mov    %rbx,%rdi
call   1030 <getenv@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    116e <nsetenv+0x3e>
lea    0xea9(%rip),%rsi
mov    %rsp,%rdi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1050 <asprintf@plt>
mov    (%rsp),%rdi
call   1040 <putenv@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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