#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*/ ENOMEM;
int asprintf(char **, char *, char const *, char const *);
int /*<<< orphan*/ errno;
scalar_t__ getenv(char const *);
int putenv(char *);
int newsetenv(const char *name, const char *value, int overwrite) {
  char *buf;
  int ret;
  if (!overwrite && getenv(name)) {
    return 0;
  }
  if (asprintf(&buf, "%s=%s", name, value) == -1) {
    errno = ENOMEM;
    return -1;
  }
  ret = putenv(buf);
  return ret;
}


x86 assembly (clang, -O0):
<newsetenv>:
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    116c <newsetenv+0x3c>
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     116c <newsetenv+0x3c>
movl   $0x0,-0x4(%rbp)
jmp    11bd <newsetenv+0x8d>
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x28(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1050 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    11ab <newsetenv+0x7b>
mov    0x2e4c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11bd <newsetenv+0x8d>
mov    -0x28(%rbp),%rdi
call   1040 <putenv@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<newsetenv>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
test   %edx,%edx
je     1163 <newsetenv+0x33>
lea    0xebb(%rip),%rsi
mov    %rsp,%rdi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1050 <asprintf@plt>
cmp    $0xffffffff,%eax
je     1177 <newsetenv+0x47>
mov    (%rsp),%rdi
call   1040 <putenv@plt>
jmp    118e <newsetenv+0x5e>
mov    %rbx,%rdi
call   1030 <getenv@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    118e <newsetenv+0x5e>
jmp    113e <newsetenv+0xe>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    0x2e51(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s=%s\00"
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @newsetenv(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
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 %17, label %12
12: ; preds = %3
%13 = load i8*, i8** %5
%14 = call i64 @getenv(i8* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 0, i32* %4
br label %28
17: ; preds = %12, %3
%18 = load i8*, i8** %5
%19 = load i8*, i8** %6
%20 = call i32 @asprintf(i8** %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = load i32, i32* @ENOMEM
store i32 %23, i32* @errno
store i32 -1, i32* %4
br label %28
24: ; preds = %17
%25 = load i8*, i8** %8
%26 = call i32 @putenv(i8* %25)
store i32 %26, i32* %9
%27 = load i32, i32* %9
store i32 %27, i32* %4
br label %28
28: ; preds = %24, %22, %16
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s=%s\00"
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @newsetenv(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = icmp eq i32 %2, 0
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = tail call i64 @getenv(i8* %0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %7, %3
%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 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = load i32, i32* @ENOMEM
store i32 %14, i32* @errno
br label %18
15: ; preds = %10
%16 = load i8*, i8** %4
%17 = call i32 @putenv(i8* %16)
br label %18
18: ; preds = %7, %15, %13
%19 = phi i32 [ -1, %13 ], [ %17, %15 ], [ 0, %7 ]
ret i32 %19
}