int setenv(char *var, char *val) {
  char *env;
  env = malloc((strlen(var) + strlen(val) + 2) * sizeof(env[0]));
  if (env == ((void *)0))
    return -1;
  strcpy(env, var);
  strcat(env, "=");
  strcat(env, val);
  if (putenv(env) < 0)
    return -1;
  free(env);
  return 0;
}


x86 assembly (clang, -O0):
<setenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x28(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
shl    $0x0,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b5 <setenv+0x55>
movl   $0xffffffff,-0x4(%rbp)
jmp    120f <setenv+0xaf>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    $0x0,%al
call   1060 <putenv@plt>
cmp    $0x0,%eax
jge    11ff <setenv+0x9f>
movl   $0xffffffff,-0x4(%rbp)
jmp    120f <setenv+0xaf>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
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   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%rbx
call   1050 <strlen@plt>
mov    %rax,%rbp
mov    %r15,%rdi
call   1050 <strlen@plt>
lea    (%rax,%rbp,1),%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11d6 <setenv+0x76>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
movw   $0x3d,0x0(%rbp,%rax,1)
mov    %rbp,%rdi
mov    %r15,%rsi
call   1080 <strcat@plt>
mov    %rbp,%rdi
xor    %eax,%eax
call   1060 <putenv@plt>
test   %eax,%eax
js     11d6 <setenv+0x76>
mov    %rbp,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"=\00"
define i32 @setenv(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %4
%8 = call i64 @strlen(i8* %7)
%9 = load i8*, i8** %5
%10 = call i64 @strlen(i8* %9)
%11 = add i64 %8, %10
%12 = add i64 %11, 2
%13 = mul i64 %12, 1
%14 = call i8* @malloc(i64 %13)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -1, i32* %3
br label %33
18: ; preds = %2
%19 = load i8*, i8** %6
%20 = load i8*, i8** %4
%21 = call i8* @strcpy(i8* %19, i8* %20)
%22 = load i8*, i8** %6
%23 = call i8* @strcat(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = load i8*, i8** %6
%25 = load i8*, i8** %5
%26 = call i8* @strcat(i8* %24, i8* %25)
%27 = load i8*, i8** %6
%28 = call i32 (i8*, ...) bitcast (i32 (...)* @putenv to i32 (i8*, ...)*)(i8* %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %18
store i32 -1, i32* %3
br label %33
31: ; preds = %18
%32 = load i8*, i8** %6
store i32 0, i32* %3
br label %33
33: ; preds = %31, %30, %17
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
define i32 @setenv(i8* nocapture %0, i8* nocapture %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = add i64 %3, 2
%6 = add i64 %5, %4
%7 = tail call i8* @malloc(i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %18, label %9
9: ; preds = %2
%10 = tail call i8* @strcpy(i8* %7, i8* dereferenceable(1) %0)
%11 = tail call i64 @strlen(i8* %7)
%12 = getelementptr i8, i8* %7, i64 %11
%13 = bitcast i8* %12 to i16*
store i16 61, i16* %13
%14 = tail call i8* @strcat(i8* %7, i8* dereferenceable(1) %1)
%15 = tail call i32 (i8*, ...) bitcast (i32 (...)* @putenv to i32 (i8*, ...)*)(i8* %7)
%16 = icmp slt i32 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %9
tail call void @free(i8* %7)
br label %18
18: ; preds = %9, %2, %17
%19 = phi i32 [ 0, %17 ], [ -1, %2 ], [ -1, %9 ]
ret i32 %19
}