#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*/ ENOMEM;
int /*<<< orphan*/ _SC_ARG_MAX;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ *getenv(char const *);
char *malloc(int);
int /*<<< orphan*/ pexit(char *);
int putenv(char *);
int sprintf(char *, char *, char const *, char const *);
int strlen(char const *);
long sysconf(int /*<<< orphan*/);
int _setenv(const char *envname, const char *envvalue, int overwrite) {
  int envLength;
  char *envStr;
  long argMax;
  if (envname == NULL || *envname == '\0' || *envname == '=') {
    errno = EINVAL;
    return -1;
  }
  argMax = sysconf(_SC_ARG_MAX);
  envLength = strlen(envname) + strlen(envvalue) + 2;
  if ((long)envLength >= argMax) {
    errno = ENOMEM;
    return -1;
  }
  if (getenv(envname) != NULL && !overwrite) {
    return 0;
  }
  envStr = malloc(envLength);
  if (envStr == NULL) {
    pexit("malloc");
  }
  if (sprintf(envStr, "%s=%s", envname, envvalue) == -1) {
    pexit("sprintf");
  }
  return putenv(envStr);
}


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)
cmpq   $0x0,-0x10(%rbp)
je     11ae <_setenv+0x3e>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11ae <_setenv+0x3e>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3d,%eax
jne    11cc <_setenv+0x5c>
mov    0x2e13(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1a(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <_setenv+0x141>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
mov    %eax,-0x20(%rbp)
movslq -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
jl     122c <_setenv+0xbc>
mov    0x2dcb(%rip),%rax
mov    (%rax),%ecx
mov    0x2dba(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <_setenv+0x141>
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     1255 <_setenv+0xe5>
cmpl   $0x0,-0x1c(%rbp)
jne    1255 <_setenv+0xe5>
movl   $0x0,-0x4(%rbp)
jmp    12b1 <_setenv+0x141>
mov    -0x20(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1278 <_setenv+0x108>
lea    0xd8d(%rip),%rdi
call   1090 <pexit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xd7c(%rip),%rsi
call   1080 <sprintf@plt>
cmp    $0xffffffff,%eax
jne    12a5 <_setenv+0x135>
lea    0xd6d(%rip),%rdi
call   1090 <pexit@plt>
mov    -0x28(%rbp),%rdi
call   1050 <putenv@plt>
mov    %eax,-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
test   %rdi,%rdi
je     118a <_setenv+0x1a>
mov    %rdi,%rbx
mov    (%rdi),%al
cmp    $0x3d,%al
je     118a <_setenv+0x1a>
test   %al,%al
jne    11aa <_setenv+0x3a>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    0x2e3e(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %edx,%ebp
mov    %rsi,%r14
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %eax,%r15d
add    $0x2,%r15d
movslq %r15d,%rax
cmp    %rax,%r12
jle    124a <_setenv+0xda>
mov    %rbx,%rdi
call   1030 <getenv@plt>
test   %ebp,%ebp
jne    11f8 <_setenv+0x88>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    11a1 <_setenv+0x31>
mov    %r15d,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1214 <_setenv+0xa4>
lea    0xdf1(%rip),%rdi
call   1090 <pexit@plt>
lea    0xdec(%rip),%rsi
mov    %rbp,%rdi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1080 <sprintf@plt>
cmp    $0xffffffff,%eax
jne    123a <_setenv+0xca>
lea    0xdd8(%rip),%rdi
call   1090 <pexit@plt>
mov    %rbp,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <putenv@plt>
mov    0x2d8f(%rip),%rax
jmp    1191 <_setenv+0x21>

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

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