#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*/ assert(int);
char *malloc(int);
int /*<<< orphan*/ putenv(char *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
int setenv(const char *name, const char *val, int overwrite) {
  int len = strlen(name) + strlen(val) + 2;
  char *env = malloc(len);
  if (!env) {
    return -1;
  }
  assert(overwrite != 0);
  strcpy(env, name);
  strcat(env, "=");
  strcat(env, val);
  putenv(env);
  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   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b8 <setenv+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <setenv+0xa3>
cmpl   $0x0,-0x1c(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rdi
lea    0xe1f(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x28(%rbp),%rdi
call   1050 <putenv@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setenv>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x2,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11cf <setenv+0x6f>
mov    %rax,%rbx
xor    %edi,%edi
test   %ebp,%ebp
setne  %dil
call   1060 <assert@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
lea    0xe50(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rdi
call   1050 <putenv@plt>
xor    %eax,%eax
jmp    11d4 <setenv+0x74>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
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 i32
%9 = alloca i8*
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* %8
%16 = load i32, i32* %8
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %9
%18 = load i8*, i8** %9
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %3
store i32 -1, i32* %4
br label %36
21: ; preds = %3
%22 = load i32, i32* %7
%23 = icmp ne i32 %22, 0
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = load i8*, i8** %9
%27 = load i8*, i8** %5
%28 = call i32 @strcpy(i8* %26, i8* %27)
%29 = load i8*, i8** %9
%30 = call i32 @strcat(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%31 = load i8*, i8** %9
%32 = load i8*, i8** %6
%33 = call i32 @strcat(i8* %31, i8* %32)
%34 = load i8*, i8** %9
%35 = call i32 @putenv(i8* %34)
store i32 0, i32* %4
br label %36
36: ; preds = %21, %20
%37 = load i32, i32* %4
ret i32 %37
}

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