#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 *);
scalar_t__ malloc(scalar_t__);
int putenv(char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
scalar_t__ strlen(char const *);
int setenv(const char *name, const char *value, int overwrite) {
  char *n;
  if (!getenv(name) || overwrite) {
    n = (char *)malloc(strlen(name) + strlen(value) + 2);
    sprintf(n, "%s=%s", name, value);
    return putenv(n);
  }
  return 0;
}


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   1030 <getenv@plt>
cmp    $0x0,%eax
je     117f <setenv+0x2f>
cmpl   $0x0,-0x1c(%rbp)
je     11d2 <setenv+0x82>
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
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe44(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <putenv@plt>
mov    %eax,-0x4(%rbp)
jmp    11d9 <setenv+0x89>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setenv>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <getenv@plt>
test   %ebp,%ebp
jne    1170 <setenv+0x20>
test   %eax,%eax
je     1170 <setenv+0x20>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rax,%rbp,1),%rdi
add    $0x2,%rdi
call   1060 <malloc@plt>
mov    %rax,%rbp
lea    0xe66(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1070 <sprintf@plt>
mov    %rbp,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <putenv@plt>

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

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