#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;
int /*<<< orphan*/ *getenv(char const *);
char *malloc(scalar_t__);
scalar_t__ putenv(char *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ *strchr(char const *, char);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ unsetenv(char const *);
int setenv(const char *name, const char *value, int overwrite) {
  char *es;
  if (name == NULL || name[0] == '\0' || strchr(name, '=') != NULL ||
      value == NULL) {
    errno = EINVAL;
    return -1;
  }
  if (getenv(name) != NULL && overwrite == 0)
    return 0;
  unsetenv(name);
  es = malloc(strlen(name) + strlen(value) + 2);
  if (es == NULL)
    return -1;
  strcpy(es, name);
  strcat(es, "=");
  strcat(es, value);
  return (putenv(es) != 0) ? -1 : 0;
}


x86 assembly (clang, -O0):
<setenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11d1 <setenv+0x51>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11d1 <setenv+0x51>
mov    -0x10(%rbp),%rdi
mov    $0x3d,%esi
call   1060 <strchr@plt>
cmp    $0x0,%rax
jne    11d1 <setenv+0x51>
cmpq   $0x0,-0x18(%rbp)
jne    11ef <setenv+0x6f>
mov    0x2e00(%rip),%rax
mov    (%rax),%ecx
mov    0x2dff(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12a9 <setenv+0x129>
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     1218 <setenv+0x98>
cmpl   $0x0,-0x1c(%rbp)
jne    1218 <setenv+0x98>
movl   $0x0,-0x4(%rbp)
jmp    12a9 <setenv+0x129>
mov    -0x10(%rbp),%rdi
call   1090 <unsetenv@plt>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1262 <setenv+0xe2>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a9 <setenv+0x129>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rdi
lea    0xd86(%rip),%rsi
call   10a0 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <strcat@plt>
mov    -0x28(%rbp),%rdi
call   1070 <putenv@plt>
mov    %rax,%rdx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setenv>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     122f <setenv+0xaf>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     122f <setenv+0xaf>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rbx,%rdi
mov    $0x3d,%esi
call   1060 <strchr@plt>
test   %r14,%r14
je     122f <setenv+0xaf>
test   %rax,%rax
jne    122f <setenv+0xaf>
mov    %rbx,%rdi
call   1030 <getenv@plt>
test   %ebp,%ebp
jne    11cb <setenv+0x4b>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    1246 <setenv+0xc6>
mov    %rbx,%rdi
call   1090 <unsetenv@plt>
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   1080 <malloc@plt>
test   %rax,%rax
je     1241 <setenv+0xc1>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
lea    0xdf3(%rip),%rsi
mov    %rbp,%rdi
call   10a0 <strcat@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   10a0 <strcat@plt>
mov    %rbp,%rdi
call   1070 <putenv@plt>
neg    %rax
sbb    %eax,%eax
jmp    1246 <setenv+0xc6>
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
mov    0x2da1(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = 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*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %24, label %11
11: ; preds = %3
%12 = load i8*, i8** %5
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 0
br i1 %16, label %24, label %17
17: ; preds = %11
%18 = load i8*, i8** %5
%19 = call i32* @strchr(i8* %18, i8 signext 61)
%20 = icmp ne i32* %19, null
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = load i8*, i8** %6
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %21, %17, %11, %3
%25 = load i32, i32* @EINVAL
store i32 %25, i32* @errno
store i32 -1, i32* %4
br label %61
26: ; preds = %21
%27 = load i8*, i8** %5
%28 = call i32* @getenv(i8* %27)
%29 = icmp ne i32* %28, null
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
store i32 0, i32* %4
br label %61
34: ; preds = %30, %26
%35 = load i8*, i8** %5
%36 = call i32 @unsetenv(i8* %35)
%37 = load i8*, i8** %5
%38 = call i64 @strlen(i8* %37)
%39 = load i8*, i8** %6
%40 = call i64 @strlen(i8* %39)
%41 = add nsw i64 %38, %40
%42 = add nsw i64 %41, 2
%43 = call i8* @malloc(i64 %42)
store i8* %43, i8** %8
%44 = load i8*, i8** %8
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %47
46: ; preds = %34
store i32 -1, i32* %4
br label %61
47: ; preds = %34
%48 = load i8*, i8** %8
%49 = load i8*, i8** %5
%50 = call i32 @strcpy(i8* %48, i8* %49)
%51 = load i8*, i8** %8
%52 = call i32 @strcat(i8* %51, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%53 = load i8*, i8** %8
%54 = load i8*, i8** %6
%55 = call i32 @strcat(i8* %53, i8* %54)
%56 = load i8*, i8** %8
%57 = call i64 @putenv(i8* %56)
%58 = icmp ne i64 %57, 0
%59 = zext i1 %58 to i64
%60 = select i1 %58, i32 -1, i32 0
store i32 %60, i32* %4
br label %61
61: ; preds = %47, %46, %33, %24
%62 = load i32, i32* %4
ret i32 %62
}

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