#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 */
scalar_t__ errno;
char *getenv(char const *);
scalar_t__ malloc(size_t);
int putenv(char *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *, char const *);
scalar_t__ strlen(char const *);
int setenv(const char *name, const char *value, int overwrite) {
  errno = 0;
  char *prev = getenv(name);
  if (errno != 0) {
    return -1;
  }
  if (prev != NULL && !overwrite) {
    return 0;
  }
  size_t len = 0;
  len += strlen(name);
  len += 1;
  len += strlen(value);
  len += 1;
  char *buf = (char *)malloc(len);
  if (buf == NULL) {
    return -1;
  }
  snprintf(buf, len, "%s=%s", name, value);
  return putenv(buf);
}


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    0x2e76(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e5b(%rip),%rax
cmpq   $0x0,(%rax)
je     119b <setenv+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <setenv+0xfa>
cmpq   $0x0,-0x28(%rbp)
je     11bc <setenv+0x6c>
cmpl   $0x0,-0x1c(%rbp)
jne    11bc <setenv+0x6c>
movl   $0x0,-0x4(%rbp)
jmp    124a <setenv+0xfa>
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1222 <setenv+0xd2>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <setenv+0xfa>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xdc7(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x38(%rbp),%rdi
call   1060 <putenv@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setenv>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e79(%rip),%rax
movq   $0x0,(%rax)
call   1030 <getenv@plt>
test   %ebx,%ebx
jne    1181 <setenv+0x31>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    11dc <setenv+0x8c>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%r12
add    $0x2,%r12
mov    %r12,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     11d7 <setenv+0x87>
mov    %rax,%rbx
lea    0xe4d(%rip),%rdx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15,%rcx
mov    %r14,%r8
call   1050 <snprintf@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1060 <putenv@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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