#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;
scalar_t__ __putenv(char *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ getenv(char const *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
scalar_t__ strchr(char const *, char);
int strlen(char const *);
int setenv(const char *var, const char *value, int overwrite) {
  char *s;
  int l1, l2;
  if (strchr(var, '=')) {
    errno = EINVAL;
    return -1;
  }
  if (!overwrite && getenv(var))
    return 0;
  l1 = strlen(var);
  l2 = strlen(value);
  s = malloc(l1 + l2 + 2);
  memcpy(s, var, l1);
  s[l1] = '=';
  memcpy(s + l1 + 1, value, l2);
  s[l1 + l2 + 1] = 0;
  if (__putenv(s, 1)) {
    free(s);
    errno = ENOMEM;
    return -1;
  }
  return 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)
mov    -0x10(%rbp),%rdi
mov    $0x3d,%esi
call   1060 <strchr@plt>
cmp    $0x0,%rax
je     11b9 <setenv+0x49>
mov    0x2e2e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <setenv+0x12d>
cmpl   $0x0,-0x1c(%rbp)
jne    11e2 <setenv+0x72>
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     11e2 <setenv+0x72>
movl   $0x0,-0x4(%rbp)
jmp    129d <setenv+0x12d>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
add    -0x30(%rbp),%edi
add    $0x2,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x3d,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
add    -0x30(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1090 <__putenv@plt>
cmp    $0x0,%rax
je     1296 <setenv+0x126>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%ecx
mov    0x2d50(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <setenv+0x12d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setenv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x3d,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     119e <setenv+0x2e>
mov    0x2e37(%rip),%rax
jmp    1220 <setenv+0xb0>
test   %ebp,%ebp
je     123f <setenv+0xcf>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%r12d
lea    (%r12,%rbp,1),%r13d
lea    (%r12,%rbp,1),%edi
add    $0x2,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1070 <memcpy@plt>
movslq %ebp,%rax
movb   $0x3d,(%r15,%rax,1)
lea    (%r15,%rax,1),%rdi
add    $0x1,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1070 <memcpy@plt>
movslq %r13d,%rax
movb   $0x0,0x1(%rax,%r15,1)
mov    %r15,%rdi
mov    $0x1,%esi
call   1090 <__putenv@plt>
test   %rax,%rax
je     1256 <setenv+0xe6>
mov    %r15,%rdi
call   1040 <free@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    0x2daf(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1030 <getenv@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    1230 <setenv+0xc0>
jmp    11a6 <setenv+0x36>
xor    %eax,%eax
jmp    1230 <setenv+0xc0>

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

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
define i32 @setenv(i8* %0, i8* %1, i32 %2) {
%4 = tail call i64 @strchr(i8* %0, i8 signext 61)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %29
6: ; preds = %3
%7 = icmp eq i32 %2, 0
br i1 %7, label %8, label %11
8: ; preds = %6
%9 = tail call i64 @getenv(i8* %0)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %32
11: ; preds = %8, %6
%12 = tail call i32 @strlen(i8* %0)
%13 = tail call i32 @strlen(i8* %1)
%14 = add nsw i32 %13, %12
%15 = add nsw i32 %14, 2
%16 = tail call i8* @malloc(i32 %15)
%17 = tail call i32 @memcpy(i8* %16, i8* %0, i32 %12)
%18 = sext i32 %12 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
store i8 61, i8* %19
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = tail call i32 @memcpy(i8* %20, i8* %1, i32 %13)
%22 = add nsw i32 %14, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %16, i64 %23
store i8 0, i8* %24
%25 = tail call i64 @__putenv(i8* %16, i32 1)
%26 = icmp eq i64 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %11
%28 = tail call i32 @free(i8* %16)
br label %29
29: ; preds = %3, %27
%30 = phi i32* [ @ENOMEM, %27 ], [ @EINVAL, %3 ]
%31 = load i32, i32* %30
store i32 %31, i32* @errno
br label %32
32: ; preds = %29, %11, %8
%33 = phi i32 [ 0, %8 ], [ 0, %11 ], [ -1, %29 ]
ret i32 %33
}