int putenv(string) const char *string;
{
  const char *const name_end = index(string, '=');
  if (name_end) {
    char *name = __builtin_alloca(name_end - string + 1);
    memcpy(name, string, name_end - string);
    name[name_end - string] = '\0';
    return setenv(name, name_end + 1, 1);
  }
  unsetenv(string);
  return 0;
}


x86 assembly (clang, -O0):
<putenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x3d,%esi
call   1040 <index@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d4 <putenv+0x94>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rax,%rcx
add    $0x10,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rax
sub    %rcx,%rax
mov    %rax,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rax
sub    %rax,%rdx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <setenv@plt>
mov    %eax,-0x4(%rbp)
jmp    11e6 <putenv+0xa6>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1060 <unsetenv@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<putenv>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
mov    $0x3d,%esi
call   1040 <index@plt>
test   %rax,%rax
je     11a5 <putenv+0x65>
mov    %rax,%r15
mov    %rax,%r12
sub    %r14,%r12
lea    0x10(%r12),%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1050 <memcpy@plt>
movb   $0x0,(%r12,%rbx,1)
add    $0x1,%r15
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <setenv@plt>
mov    %eax,%ebx
jmp    11b1 <putenv+0x71>
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <unsetenv@plt>
mov    %ebx,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @putenv(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i8* @index(i8* %6, i32 61)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %36
10: ; preds = %1
%11 = load i8*, i8** %4
%12 = load i8*, i8** %3
%13 = ptrtoint i8* %11 to i64
%14 = ptrtoint i8* %12 to i64
%15 = sub i64 %13, %14
%16 = add nsw i64 %15, 1
%17 = alloca i8, i64 %16
store i8* %17, i8** %5
%18 = load i8*, i8** %5
%19 = load i8*, i8** %3
%20 = load i8*, i8** %4
%21 = load i8*, i8** %3
%22 = ptrtoint i8* %20 to i64
%23 = ptrtoint i8* %21 to i64
%24 = sub i64 %22, %23
%25 = load i8*, i8** %5
%26 = load i8*, i8** %4
%27 = load i8*, i8** %3
%28 = ptrtoint i8* %26 to i64
%29 = ptrtoint i8* %27 to i64
%30 = sub i64 %28, %29
%31 = getelementptr inbounds i8, i8* %25, i64 %30
store i8 0, i8* %31
%32 = load i8*, i8** %5
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i64 1
%35 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @setenv to i32 (i8*, i8*, i32, ...)*)(i8* %32, i8* %34, i32 1)
store i32 %35, i32* %2
br label %39
36: ; preds = %1
%37 = load i8*, i8** %3
%38 = call i32 (i8*, ...) bitcast (i32 (...)* @unsetenv to i32 (i8*, ...)*)(i8* %37)
store i32 0, i32* %2
br label %39
39: ; preds = %36, %10
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
define i32 @putenv(i8* %0) {
%2 = tail call i8* @index(i8* %0, i32 61)
%3 = icmp eq i8* %2, null
br i1 %3, label %13, label %4
4: ; preds = %1
%5 = ptrtoint i8* %2 to i64
%6 = ptrtoint i8* %0 to i64
%7 = sub i64 %5, %6
%8 = add nsw i64 %7, 1
%9 = alloca i8, i64 %8
%10 = getelementptr inbounds i8, i8* %9, i64 %7
store i8 0, i8* %10
%11 = getelementptr inbounds i8, i8* %2, i64 1
%12 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @setenv to i32 (i8*, i8*, i32, ...)*)(i8* %9, i8* %11, i32 1)
br label %15
13: ; preds = %1
%14 = tail call i32 (i8*, ...) bitcast (i32 (...)* @unsetenv to i32 (i8*, ...)*)(i8* %0)
br label %15
15: ; preds = %13, %4
%16 = phi i32 [ %12, %4 ], [ 0, %13 ]
ret i32 %16
}