#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 */
unsigned char *alloca(int);
scalar_t__ getenv(char const *);
int putenv(unsigned char *);
int /*<<< orphan*/ snprintf(unsigned char *, int, char *, char const *,
                            char const *);
int strlen(char const *);
int setenv(const char *name, const char *value, int overwrite) {
  unsigned char *buf;
  int buflen;
  buflen = strlen(name) + strlen(value) + 2;
  if (!(buf = alloca(buflen)))
    return -1;
  if (!overwrite && getenv(name))
    return 0;
  snprintf(buf, buflen, "%s=%s", name, value);
  return putenv(buf);
}


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)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1070 <alloca@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11a7 <setenv+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f7 <setenv+0xa7>
cmpl   $0x0,-0x1c(%rbp)
jne    11d0 <setenv+0x80>
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     11d0 <setenv+0x80>
movl   $0x0,-0x4(%rbp)
jmp    11f7 <setenv+0xa7>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xe1a(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x28(%rbp),%rdi
call   1060 <putenv@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setenv>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%ebp
add    $0x2,%ebp
mov    %ebp,%edi
call   1070 <alloca@plt>
test   %rax,%rax
je     11c3 <setenv+0x73>
mov    %rax,%rbx
test   %r12d,%r12d
jne    119c <setenv+0x4c>
mov    %r15,%rdi
call   1030 <getenv@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    11c8 <setenv+0x78>
lea    0xe5d(%rip),%rdx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r15,%rcx
mov    %r14,%r8
call   1050 <snprintf@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <putenv@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
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 i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = call i32 @strlen(i8* %10)
%12 = load i8*, i8** %6
%13 = call i32 @strlen(i8* %12)
%14 = add nsw i32 %11, %13
%15 = add nsw i32 %14, 2
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = call i8* @alloca(i32 %16)
store i8* %17, i8** %8
%18 = icmp ne i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %3
store i32 -1, i32* %4
br label %36
20: ; preds = %3
%21 = load i32, i32* %7
%22 = icmp ne i32 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %20
%24 = load i8*, i8** %5
%25 = call i64 @getenv(i8* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
store i32 0, i32* %4
br label %36
28: ; preds = %23, %20
%29 = load i8*, i8** %8
%30 = load i32, i32* %9
%31 = load i8*, i8** %5
%32 = load i8*, i8** %6
%33 = call i32 @snprintf(i8* %29, i32 %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %31, i8* %32)
%34 = load i8*, i8** %8
%35 = call i32 @putenv(i8* %34)
store i32 %35, i32* %4
br label %36
36: ; preds = %28, %27, %19
%37 = load i32, i32* %4
ret i32 %37
}

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