#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 */
char *malloc(int);
int putenv(char *);
int sprintf(char *, char *, char const *, char const *);
int strlen(char const *);
int mysetenv(const char *name, const char *value) {
  char *envstr;
  int strlength;
  if ((strlength = strlen(name) + strlen(value) + 2) &&
      ((envstr = malloc(strlength)) != NULL) &&
      (sprintf(envstr, "%s=%s", name, value) == strlength - 1)) {
    return putenv(envstr);
  }
  return 1;
}


x86 assembly (clang, -O0):
<mysetenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     11c8 <mysetenv+0x88>
mov    -0x24(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11c8 <mysetenv+0x88>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe5c(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11c8 <mysetenv+0x88>
mov    -0x20(%rbp),%rdi
call   1040 <putenv@plt>
mov    %eax,-0x4(%rbp)
jmp    11cf <mysetenv+0x8f>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mysetenv>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r12
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
add    %ebx,%ebp
mov    %ebp,%edi
add    $0x2,%edi
je     11a1 <mysetenv+0x61>
call   1050 <malloc@plt>
test   %rax,%rax
je     11a1 <mysetenv+0x61>
mov    %rax,%r15
lea    0xe84(%rip),%rsi
mov    %rax,%rdi
mov    %r12,%rdx
mov    %r14,%rcx
call   1060 <sprintf@plt>
add    $0x1,%ebp
cmp    %ebp,%eax
jne    11a1 <mysetenv+0x61>
mov    %r15,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <putenv@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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