int setenv(name, value, clobber)
char *name;
char *value;
int clobber;
{
  char *malloc();
  char *getenv();
  char *cp;
  if (clobber == 0 && getenv(name) != 0)
    return (0);
  if ((cp = malloc(strlen(name) + strlen(value) + 2)) == 0)
    return (1);
  sprintf(cp, "%s=%s", name, value);
  return (putenv(cp));
}


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)
cmpl   $0x0,-0x1c(%rbp)
jne    118e <setenv+0x3e>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     118e <setenv+0x3e>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <setenv+0xa8>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
mov    $0x0,%al
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11d0 <setenv+0x80>
movl   $0x1,-0x4(%rbp)
jmp    11f8 <setenv+0xa8>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe1d(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
mov    -0x28(%rbp),%rdi
mov    $0x0,%al
call   1050 <putenv@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setenv>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
test   %edx,%edx
je     11af <setenv+0x5f>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
xor    %eax,%eax
call   1060 <malloc@plt>
test   %rax,%rax
je     11c2 <setenv+0x72>
mov    %rax,%rbx
lea    0xe70(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    %rbx,%rdi
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <putenv@plt>
xor    %ebx,%ebx
mov    %r15,%rdi
xor    %eax,%eax
call   1030 <getenv@plt>
test   %rax,%rax
jne    11c7 <setenv+0x77>
jmp    115f <setenv+0xf>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.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*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32, i32* %7
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %3
%12 = load i8*, i8** %5
%13 = call i8* (i8*, ...) bitcast (i8* (...)* @getenv to i8* (i8*, ...)*)(i8* %12)
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %11
store i32 0, i32* %4
br label %33
16: ; preds = %11, %3
%17 = load i8*, i8** %5
%18 = call i64 @strlen(i8* %17)
%19 = load i8*, i8** %6
%20 = call i64 @strlen(i8* %19)
%21 = add i64 %18, %20
%22 = add i64 %21, 2
%23 = call i8* (i64, ...) bitcast (i8* (...)* @malloc to i8* (i64, ...)*)(i64 %22)
store i8* %23, i8** %8
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %26
25: ; preds = %16
store i32 1, i32* %4
br label %33
26: ; preds = %16
%27 = load i8*, i8** %8
%28 = load i8*, i8** %5
%29 = load i8*, i8** %6
%30 = call i32 (i8*, i8*, ...) @sprintf(i8* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %28, i8* %29)
%31 = load i8*, i8** %8
%32 = call i32 (i8*, ...) bitcast (i32 (...)* @putenv to i32 (i8*, ...)*)(i8* %31)
store i32 %32, i32* %4
br label %33
33: ; preds = %26, %25, %15
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s=%s\00"
define i32 @setenv(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %5, label %8
5: ; preds = %3
%6 = tail call i8* (i8*, ...) bitcast (i8* (...)* @getenv to i8* (i8*, ...)*)(i8* %0)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %18
8: ; preds = %5, %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 i8* (i64, ...) bitcast (i8* (...)* @malloc to i8* (i64, ...)*)(i64 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %8
%16 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%17 = tail call i32 (i8*, ...) bitcast (i32 (...)* @putenv to i32 (i8*, ...)*)(i8* %13)
br label %18
18: ; preds = %8, %5, %15
%19 = phi i32 [ %17, %15 ], [ 0, %5 ], [ 1, %8 ]
ret i32 %19
}