#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 __add_to_environ(char *, int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ free(char *);
char *strchr(char *, char);
char *strndup(char *, char const *const);
int /*<<< orphan*/ unsetenv(char *);
int putenv(char *string) {
  int result;
  const char *const name_end = strchr(string, '=');
  if (name_end != NULL) {
    char *name = strndup(string, name_end - string);
    result = __add_to_environ(name, NULL, string, 1);
    free(name);
    return (result);
  }
  unsetenv(string);
  return 0;
}


x86 assembly (clang, -O0):
<putenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x3d,%esi
call   1040 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11be <putenv+0x6e>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rax
sub    %rax,%rsi
call   1060 <strndup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%esi
mov    $0x1,%ecx
call   1050 <__add_to_environ@plt>
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ce <putenv+0x7e>
mov    -0x10(%rbp),%rdi
call   1070 <unsetenv@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<putenv>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x3d,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1197 <putenv+0x47>
sub    %rbx,%rax
mov    %rbx,%rdi
mov    %rax,%rsi
call   1060 <strndup@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
mov    $0x1,%ecx
call   1050 <__add_to_environ@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
jmp    11a1 <putenv+0x51>
mov    %rbx,%rdi
call   1070 <unsetenv@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @putenv(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i8* @strchr(i8* %7, i8 signext 61)
store i8* %8, i8** %5
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %26
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = load i8*, i8** %5
%14 = load i8*, i8** %3
%15 = ptrtoint i8* %13 to i64
%16 = ptrtoint i8* %14 to i64
%17 = sub i64 %15, %16
%18 = inttoptr i64 %17 to i8*
%19 = call i8* @strndup(i8* %12, i8* %18)
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = load i8*, i8** %3
%22 = call i32 @__add_to_environ(i8* %20, i32* null, i8* %21, i32 1)
store i32 %22, i32* %4
%23 = load i8*, i8** %6
%24 = call i32 @free(i8* %23)
%25 = load i32, i32* %4
store i32 %25, i32* %2
br label %29
26: ; preds = %1
%27 = load i8*, i8** %3
%28 = call i32 @unsetenv(i8* %27)
store i32 0, i32* %2
br label %29
29: ; preds = %26, %11
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
define i32 @putenv(i8* %0) {
%2 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 61)
%3 = icmp eq i8* %2, null
br i1 %3, label %12, label %4
4: ; preds = %1
%5 = ptrtoint i8* %2 to i64
%6 = ptrtoint i8* %0 to i64
%7 = sub i64 %5, %6
%8 = inttoptr i64 %7 to i8*
%9 = tail call i8* @strndup(i8* %0, i8* %8)
%10 = tail call i32 @__add_to_environ(i8* %9, i32* null, i8* %0, i32 1)
%11 = tail call i32 @free(i8* %9)
br label %14
12: ; preds = %1
%13 = tail call i32 @unsetenv(i8* %0)
br label %14
14: ; preds = %12, %4
%15 = phi i32 [ %10, %4 ], [ 0, %12 ]
ret i32 %15
}