#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*/ EINVAL;
int /*<<< orphan*/ errno;
char *getenv(char const *);
char *malloc(size_t);
scalar_t__ putenv(char *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *, char const *);
int strlen(char const *);
int /*<<< orphan*/ *strstr(char const *, char *);
int setenv_p(const char *name, const char *value, int overwrite) {
  char *envar, *newvar;
  size_t len;
  if (((name == NULL || strlen(name) == 0 || strstr(name, "=") != NULL)
           ? true
           : false)) {
    errno = EINVAL;
    return -1;
  }
  envar = getenv(name);
  if (envar != NULL && !overwrite)
    return 0;
  len = strlen(name) + strlen(value) + 2;
  newvar = malloc(len);
  if (newvar == NULL)
    return -1;
  snprintf(newvar, len, "%s=%s", name, value);
  if (putenv(newvar) != 0)
    return -1;
  else
    return 0;
}


x86 assembly (clang, -O0):
<setenv_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11aa <setenv_p+0x4a>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
je     11aa <setenv_p+0x4a>
mov    -0x10(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
je     11b9 <setenv_p+0x59>
mov    $0x1,%al
test   $0x1,%al
jne    11c8 <setenv_p+0x68>
jmp    11e6 <setenv_p+0x86>
xor    %eax,%eax
test   $0x1,%al
jne    11c8 <setenv_p+0x68>
jmp    11e6 <setenv_p+0x86>
mov    0x2e09(%rip),%rax
mov    (%rax),%ecx
mov    0x2e08(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <setenv_p+0x13f>
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1214 <setenv_p+0xb4>
cmpl   $0x0,-0x1c(%rbp)
jne    1214 <setenv_p+0xb4>
movl   $0x0,-0x4(%rbp)
jmp    129f <setenv_p+0x13f>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    125d <setenv_p+0xfd>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <setenv_p+0x13f>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xd8e(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x30(%rbp),%rdi
call   1060 <putenv@plt>
cmp    $0x0,%rax
je     1298 <setenv_p+0x138>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <setenv_p+0x13f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setenv_p>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     118f <setenv_p+0x2f>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <strlen@plt>
test   %eax,%eax
je     118f <setenv_p+0x2f>
mov    %rbx,%rdi
mov    $0x3d,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11b1 <setenv_p+0x51>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    0x2e41(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1030 <getenv@plt>
test   %ebp,%ebp
jne    11c7 <setenv_p+0x67>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    11a6 <setenv_p+0x46>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %ebp,%eax
add    $0x2,%eax
movslq %eax,%r15
mov    %r15,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     11a1 <setenv_p+0x41>
mov    %rax,%rbp
lea    0xe08(%rip),%rdx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %rbx,%rcx
mov    %r14,%r8
call   1060 <snprintf@plt>
mov    %rbp,%rdi
call   1070 <putenv@plt>
neg    %rax
sbb    %eax,%eax
jmp    11a6 <setenv_p+0x46>

LLVM IR (-O0):
@.str = [2 x i8] c"=\00"
@EINVAL = global i32 0
@errno = global i32 0
@.str.1 = [6 x i8] c"%s=%s\00"
define i32 @setenv_p(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %5
%12 = icmp eq i8* %11, null
br i1 %12, label %21, label %13
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = call i32 @strlen(i8* %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %13
%18 = load i8*, i8** %5
%19 = call i32* @strstr(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %22
21: ; preds = %17, %13, %3
br i1 true, label %23, label %25
22: ; preds = %17
br i1 false, label %23, label %25
23: ; preds = %22, %21
%24 = load i32, i32* @EINVAL
store i32 %24, i32* @errno
store i32 -1, i32* %4
br label %58
25: ; preds = %22, %21
%26 = load i8*, i8** %5
%27 = call i8* @getenv(i8* %26)
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %34
30: ; preds = %25
%31 = load i32, i32* %7
%32 = icmp ne i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %30
store i32 0, i32* %4
br label %58
34: ; preds = %30, %25
%35 = load i8*, i8** %5
%36 = call i32 @strlen(i8* %35)
%37 = load i8*, i8** %6
%38 = call i32 @strlen(i8* %37)
%39 = add nsw i32 %36, %38
%40 = add nsw i32 %39, 2
%41 = sext i32 %40 to i64
store i64 %41, i64* %10
%42 = load i64, i64* %10
%43 = call i8* @malloc(i64 %42)
store i8* %43, i8** %9
%44 = load i8*, i8** %9
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %47
46: ; preds = %34
store i32 -1, i32* %4
br label %58
47: ; preds = %34
%48 = load i8*, i8** %9
%49 = load i64, i64* %10
%50 = load i8*, i8** %5
%51 = load i8*, i8** %6
%52 = call i32 @snprintf(i8* %48, i64 %49, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %50, i8* %51)
%53 = load i8*, i8** %9
%54 = call i64 @putenv(i8* %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %47
store i32 -1, i32* %4
br label %58
57: ; preds = %47
store i32 0, i32* %4
br label %58
58: ; preds = %57, %56, %46, %33, %23
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@.str.1 = [6 x i8] c"%s=%s\00"
define i32 @setenv_p(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %11, label %5
5: ; preds = %3
%6 = tail call i32 @strlen(i8* %0)
%7 = icmp eq i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %5
%9 = tail call i8* @strchr(i8* %0, i32 61)
%10 = icmp eq i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %3, %5, %8
%12 = load i32, i32* @EINVAL
store i32 %12, i32* @errno
br label %31
13: ; preds = %8
%14 = tail call i8* @getenv(i8* %0)
%15 = icmp eq i8* %14, null
%16 = icmp ne i32 %2, 0
%17 = or i1 %16, %15
br i1 %17, label %18, label %31
18: ; preds = %13
%19 = tail call i32 @strlen(i8* %0)
%20 = tail call i32 @strlen(i8* %1)
%21 = add i32 %19, 2
%22 = add i32 %21, %20
%23 = sext i32 %22 to i64
%24 = tail call i8* @malloc(i64 %23)
%25 = icmp eq i8* %24, null
br i1 %25, label %31, label %26
26: ; preds = %18
%27 = tail call i32 @snprintf(i8* %24, i64 %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1)
%28 = tail call i64 @putenv(i8* %24)
%29 = icmp ne i64 %28, 0
%30 = sext i1 %29 to i32
br label %31
31: ; preds = %26, %18, %13, %11
%32 = phi i32 [ -1, %11 ], [ 0, %13 ], [ -1, %18 ], [ %30, %26 ]
ret i32 %32
}