#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 /*<<< orphan*/ errno;
char *getenv(char const *);
char *malloc(scalar_t__);
int putenv(int /*<<< orphan*/);
int /*<<< orphan*/ strcat(int /*<<< orphan*/, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int _setenv(const char *name, const char *value, int overwrite) {
  char *oldValue = getenv(name);
  char *nameValue = malloc(strlen(name) + strlen(value) + 2);
  if (nameValue == NULL) {
    errno = ENOMEM;
    return -1;
  }
  if (oldValue == NULL || overwrite)
    return putenv(strcat(strcat(strcpy(nameValue, name), "="), value));
  return 0;
}


x86 assembly (clang, -O0):
<_setenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d3 <_setenv+0x73>
mov    0x2e24(%rip),%rax
mov    (%rax),%ecx
mov    0x2e13(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1224 <_setenv+0xc4>
cmpq   $0x0,-0x28(%rbp)
je     11e8 <_setenv+0x88>
cmpl   $0x0,-0x1c(%rbp)
je     121d <_setenv+0xbd>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    %eax,%edi
lea    0xe02(%rip),%rsi
call   1080 <strcat@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%rsi
call   1080 <strcat@plt>
mov    %eax,%edi
call   1060 <putenv@plt>
mov    %eax,-0x4(%rbp)
jmp    1224 <_setenv+0xc4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_setenv>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <getenv@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rax,%rbp,1),%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     11dc <_setenv+0x7c>
test   %r12d,%r12d
jne    11aa <_setenv+0x4a>
xor    %ecx,%ecx
test   %r15,%r15
jne    11f3 <_setenv+0x93>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
lea    0xe44(%rip),%rsi
mov    %eax,%edi
call   1080 <strcat@plt>
mov    %eax,%edi
mov    %r14,%rsi
call   1080 <strcat@plt>
mov    %eax,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <putenv@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    0x2dec(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ecx
mov    %ecx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
@.str = [2 x i8] c"=\00"
define i32 @_setenv(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 i8*, i8** %5
%11 = call i8* @getenv(i8* %10)
store i8* %11, i8** %8
%12 = load i8*, i8** %5
%13 = call i64 @strlen(i8* %12)
%14 = load i8*, i8** %6
%15 = call i64 @strlen(i8* %14)
%16 = add nsw i64 %13, %15
%17 = add nsw i64 %16, 2
%18 = call i8* @malloc(i64 %17)
store i8* %18, i8** %9
%19 = load i8*, i8** %9
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = load i32, i32* @ENOMEM
store i32 %22, i32* @errno
store i32 -1, i32* %4
br label %38
23: ; preds = %3
%24 = load i8*, i8** %8
%25 = icmp eq i8* %24, null
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = load i32, i32* %7
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %26, %23
%30 = load i8*, i8** %9
%31 = load i8*, i8** %5
%32 = call i32 @strcpy(i8* %30, i8* %31)
%33 = call i32 @strcat(i32 %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%34 = load i8*, i8** %6
%35 = call i32 @strcat(i32 %33, i8* %34)
%36 = call i32 @putenv(i32 %35)
store i32 %36, i32* %4
br label %38
37: ; preds = %26
store i32 0, i32* %4
br label %38
38: ; preds = %37, %29, %21
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
@.str = [2 x i8] c"=\00"
define i32 @_setenv(i8* %0, i8* %1, i32 %2) {
%4 = tail call i8* @getenv(i8* %0)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%7 = add i64 %5, 2
%8 = add i64 %7, %6
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = load i32, i32* @ENOMEM
store i32 %12, i32* @errno
br label %22
13: ; preds = %3
%14 = icmp eq i8* %4, null
%15 = icmp ne i32 %2, 0
%16 = or i1 %15, %14
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = tail call i32 @strcpy(i8* %9, i8* %0)
%19 = tail call i32 @strcat(i32 %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = tail call i32 @strcat(i32 %19, i8* %1)
%21 = tail call i32 @putenv(i32 %20)
br label %22
22: ; preds = %13, %17, %11
%23 = phi i32 [ -1, %11 ], [ %21, %17 ], [ 0, %13 ]
ret i32 %23
}