#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*/ free(char *);
char *getenv(char const *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int putenv(char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
int strlen(char const *);
int setenv(const char *key, const char *value, int overwrite) {
  int result = 255;
  char *current = getenv(key);
  if (overwrite && current) {
    int key_len = strlen(key);
    int val_len = strlen(value);
    int len = key_len + 1 + val_len + 1;
    char *buffer = malloc(len);
    memset(buffer, 0, len);
    sprintf(buffer, "%s=%s", key, value);
    result = putenv(buffer);
    free(buffer);
  }
  return result;
}


x86 assembly (clang, -O0):
<setenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0xff,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     121a <setenv+0xaa>
cmpq   $0x0,-0x20(%rbp)
je     121a <setenv+0xaa>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
add    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe00(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x38(%rbp),%rdi
call   1070 <putenv@plt>
mov    %eax,-0x18(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
mov    -0x18(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setenv>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <getenv@plt>
test   %ebp,%ebp
je     11ea <setenv+0x7a>
test   %rax,%rax
je     11ea <setenv+0x7a>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rax,%rbp,1),%r15d
add    $0x2,%r15d
mov    %r15d,%edi
call   1080 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1060 <memset@plt>
lea    0xe3a(%rip),%rsi
mov    %rbp,%rdi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1090 <sprintf@plt>
mov    %rbp,%rdi
call   1070 <putenv@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1040 <free@plt>
mov    %ebx,%eax
jmp    11ef <setenv+0x7f>
mov    $0xff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s=%s\00"
define i32 @setenv(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 255, i32* %7
%13 = load i8*, i8** %4
%14 = call i8* @getenv(i8* %13)
store i8* %14, i8** %8
%15 = load i32, i32* %6
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %43
17: ; preds = %3
%18 = load i8*, i8** %8
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %43
20: ; preds = %17
%21 = load i8*, i8** %4
%22 = call i32 @strlen(i8* %21)
store i32 %22, i32* %9
%23 = load i8*, i8** %5
%24 = call i32 @strlen(i8* %23)
store i32 %24, i32* %10
%25 = load i32, i32* %9
%26 = add nsw i32 %25, 1
%27 = load i32, i32* %10
%28 = add nsw i32 %26, %27
%29 = add nsw i32 %28, 1
store i32 %29, i32* %11
%30 = load i32, i32* %11
%31 = call i8* @malloc(i32 %30)
store i8* %31, i8** %12
%32 = load i8*, i8** %12
%33 = load i32, i32* %11
%34 = call i32 @memset(i8* %32, i32 0, i32 %33)
%35 = load i8*, i8** %12
%36 = load i8*, i8** %4
%37 = load i8*, i8** %5
%38 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %36, i8* %37)
%39 = load i8*, i8** %12
%40 = call i32 @putenv(i8* %39)
store i32 %40, i32* %7
%41 = load i8*, i8** %12
%42 = call i32 @free(i8* %41)
br label %43
43: ; preds = %20, %17, %3
%44 = load i32, i32* %7
ret i32 %44
}

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