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


x86 assembly (clang, -O0):
<safeputenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a1 <safeputenv+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <safeputenv+0x97>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe4c(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <putenv@plt>
cmp    $0xffffffff,%eax
jne    11e0 <safeputenv+0x90>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <safeputenv+0x97>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safeputenv>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    $0xffffffff,%eax
test   %rbx,%rbx
je     11b8 <safeputenv+0x68>
lea    0xe74(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   1070 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <putenv@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11b8 <safeputenv+0x68>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s=%s\00"
define i32 @safeputenv(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %4
%8 = call i64 @strlen(i8* %7)
%9 = load i8*, i8** %5
%10 = call i64 @strlen(i8* %9)
%11 = add nsw i64 %8, %10
%12 = add nsw i64 %11, 2
%13 = call i8* @malloc(i64 %12)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %2
store i32 -1, i32* %3
br label %29
17: ; preds = %2
%18 = load i8*, i8** %6
%19 = load i8*, i8** %4
%20 = load i8*, i8** %5
%21 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %20)
%22 = load i8*, i8** %6
%23 = call i32 @putenv(i8* %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = load i8*, i8** %6
%27 = call i32 @free(i8* %26)
store i32 -1, i32* %3
br label %29
28: ; preds = %17
store i32 0, i32* %3
br label %29
29: ; preds = %28, %25, %16
%30 = load i32, i32* %3
ret i32 %30
}

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