#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 */
scalar_t__ EINVAL;
scalar_t__ ENOMEM;
int asprintf(char **, char *, char const *, char const *);
scalar_t__ errno;
int putenv(char *);
int runcmd_setenv(const char *name, const char *value) {
  char *env_string = NULL;
  if (!name || !value) {
    errno = EINVAL;
    return -1;
  }
  errno = 0;
  if (asprintf(&env_string, "%s=%s", name, value) == -1)
    return -1;
  if (!env_string) {
    errno = ENOMEM;
    return -1;
  }
  return putenv(env_string);
}


x86 assembly (clang, -O0):
<runcmd_setenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     114e <runcmd_setenv+0x2e>
cmpq   $0x0,-0x18(%rbp)
jne    116e <runcmd_setenv+0x4e>
mov    0x2e7b(%rip),%rax
mov    (%rax),%rcx
mov    0x2e79(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11e0 <runcmd_setenv+0xc0>
mov    0x2e63(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x20(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   1040 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    11a9 <runcmd_setenv+0x89>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e0 <runcmd_setenv+0xc0>
cmpq   $0x0,-0x20(%rbp)
jne    11d4 <runcmd_setenv+0xb4>
mov    0x2e25(%rip),%rax
mov    (%rax),%rcx
mov    0x2e13(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11e0 <runcmd_setenv+0xc0>
mov    -0x20(%rbp),%rdi
call   1030 <putenv@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<runcmd_setenv>:
push   %rbx
sub    $0x10,%rsp
movq   $0x0,0x8(%rsp)
test   %rdi,%rdi
je     117a <runcmd_setenv+0x5a>
mov    %rsi,%rcx
test   %rsi,%rsi
je     117a <runcmd_setenv+0x5a>
mov    %rdi,%rdx
mov    0x2e93(%rip),%rbx
movq   $0x0,(%rbx)
lea    0xead(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1040 <asprintf@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ecx
je     11a2 <runcmd_setenv+0x82>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     1195 <runcmd_setenv+0x75>
call   1030 <putenv@plt>
jmp    11a2 <runcmd_setenv+0x82>
mov    0x2e4f(%rip),%rax
mov    (%rax),%rax
mov    0x2e4d(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
jmp    11a2 <runcmd_setenv+0x82>
mov    0x2e44(%rip),%rcx
mov    (%rcx),%rcx
mov    %rcx,(%rbx)
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@EINVAL = global i64 0
@errno = global i64 0
@.str = [6 x i8] c"%s=%s\00"
@ENOMEM = global i64 0
define i32 @runcmd_setenv(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
store i8* null, i8** %6
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %14, label %12
12: ; preds = %9, %2
%13 = load i64, i64* @EINVAL
store i64 %13, i64* @errno
store i32 -1, i32* %3
br label %28
14: ; preds = %9
store i64 0, i64* @errno
%15 = load i8*, i8** %4
%16 = load i8*, i8** %5
%17 = call i32 @asprintf(i8** %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %14
store i32 -1, i32* %3
br label %28
20: ; preds = %14
%21 = load i8*, i8** %6
%22 = icmp ne i8* %21, null
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = load i64, i64* @ENOMEM
store i64 %24, i64* @errno
store i32 -1, i32* %3
br label %28
25: ; preds = %20
%26 = load i8*, i8** %6
%27 = call i32 @putenv(i8* %26)
store i32 %27, i32* %3
br label %28
28: ; preds = %25, %23, %19, %12
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@EINVAL = global i64 0
@errno = global i64 0
@.str = [6 x i8] c"%s=%s\00"
@ENOMEM = global i64 0
define i32 @runcmd_setenv(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%5 = icmp ne i8* %0, null
%6 = icmp ne i8* %1, null
%7 = and i1 %5, %6
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = load i64, i64* @EINVAL
store i64 %9, i64* @errno
br label %20
10: ; preds = %2
store i64 0, i64* @errno
%11 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%12 = icmp eq i32 %11, -1
br i1 %12, label %20, label %13
13: ; preds = %10
%14 = load i8*, i8** %3
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = load i64, i64* @ENOMEM
store i64 %17, i64* @errno
br label %20
18: ; preds = %13
%19 = call i32 @putenv(i8* %14)
br label %20
20: ; preds = %10, %18, %16, %8
%21 = phi i32 [ %19, %18 ], [ -1, %16 ], [ -1, %8 ], [ -1, %10 ]
ret i32 %21
}