#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*/ lprintf(int, char *, ...);
char *malloc(scalar_t__);
scalar_t__ putenv(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strlen(char *);
void set_env_var(char *var, char *string) {
  char *v;
  v = malloc(strlen(var) + strlen(string) + 2);
  if (v == NULL)
    lprintf(1, "set_env_var: cannot malloc");
  else {
    sprintf(v, "%s=%s", var, string);
    lprintf(5, "setenv: '%s'", v);
    if (putenv(v) != 0)
      lprintf(1, "putenv failed");
  }
}


x86 assembly (clang, -O0):
<set_env_var>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x20(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ad <set_env_var+0x5d>
mov    $0x1,%edi
lea    0xe5f(%rip),%rsi
mov    $0x0,%al
call   1070 <lprintf@plt>
jmp    1207 <set_env_var+0xb7>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe5b(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x18(%rbp),%rdx
mov    $0x5,%edi
lea    0xe4c(%rip),%rsi
mov    $0x0,%al
call   1070 <lprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <putenv@plt>
cmp    $0x0,%rax
je     1202 <set_env_var+0xb2>
mov    $0x1,%edi
lea    0xe33(%rip),%rsi
mov    $0x0,%al
call   1070 <lprintf@plt>
jmp    1207 <set_env_var+0xb7>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_env_var>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     11c1 <set_env_var+0x71>
mov    %rax,%rbx
lea    0xe94(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   1060 <sprintf@plt>
lea    0xe85(%rip),%rsi
mov    $0x5,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <lprintf@plt>
mov    %rbx,%rdi
call   1040 <putenv@plt>
test   %rax,%rax
je     11d9 <set_env_var+0x89>
lea    0xe6f(%rip),%rsi
jmp    11c8 <set_env_var+0x78>
lea    0xe38(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
jmp    1070 <lprintf@plt>
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [27 x i8] c"set_env_var: cannot malloc\00"
@.str.1 = [6 x i8] c"%s=%s\00"
@.str.2 = [13 x i8] c"setenv: '%s'\00"
@.str.3 = [14 x i8] c"putenv failed\00"
define void @set_env_var(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 %14, label %9
9: ; preds = %2
%10 = tail call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1)
%11 = tail call i32 (i32, i8*, ...) @lprintf(i32 5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%12 = tail call i64 @putenv(i8* %7)
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %9, %2
%15 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), %9 ]
%16 = tail call i32 (i32, i8*, ...) @lprintf(i32 1, i8* %15)
br label %17
17: ; preds = %14, %9
ret void
}