#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 /*<<< orphan*/ *getenv(char const *);
char *malloc(scalar_t__);
int putenv(char *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int setenv(const char *name, const char *value, int overwrite) {
  char *envstring;
  if (!overwrite && getenv(name) != NULL)
    return 0;
  envstring = malloc(strlen(name) + 1 + strlen(value) + 1);
  if (envstring == NULL)
    return -1;
  strcpy(envstring, name);
  strcat(envstring, "=");
  strcat(envstring, value);
  return putenv(envstring);
}


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    119c <setenv+0x3c>
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     119c <setenv+0x3c>
movl   $0x0,-0x4(%rbp)
jmp    1217 <setenv+0xb7>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e1 <setenv+0x81>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <setenv+0xb7>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rdi
lea    0xe07(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strcat@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   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
test   %edx,%edx
je     11c9 <setenv+0x69>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     11dd <setenv+0x7d>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
lea    0xe57(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <putenv@plt>
mov    %r15,%rdi
call   1030 <getenv@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    11e2 <setenv+0x82>
jmp    116f <setenv+0xf>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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