#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*/ exit(int);
scalar_t__ isalnum(char);
scalar_t__ isalpha(char);
char *malloc(scalar_t__);
int /*<<< orphan*/ memmove(char *, char *, scalar_t__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strlen(char *);
char toupper(char);
char *setlocalenv(char *format, char *name, char *value) {
  char *result;
  result = malloc(strlen(format) + strlen(name) + strlen(value) + 1);
  if (!result) {
    perror("malloc");
    exit(1);
  }
  sprintf(result, format, name, value);
  {
    char *here, *there;
    for (here = there = result; *there != '=' && *there; there++) {
      if (*there == '-')
        *there = '_';
      if (isalpha(*there))
        *there = toupper(*there);
      if (isalnum(*there) || *there == '_') {
        *here = *there;
        here++;
      }
    }
    memmove(here, there, strlen(there) + 1);
  }
  return result;
}


x86 assembly (clang, -O0):
<setlocalenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1206 <setlocalenv+0x76>
lea    0xe09(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   10a0 <sprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x3d,%ecx
mov    %al,-0x41(%rbp)
je     124c <setlocalenv+0xbc>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    125c <setlocalenv+0xcc>
jmp    12f1 <setlocalenv+0x161>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
jne    1273 <setlocalenv+0xe3>
mov    -0x30(%rbp),%rax
movb   $0x5f,(%rax)
mov    -0x30(%rbp),%rax
movsbl (%rax),%edi
call   1070 <isalpha@plt>
cmp    $0x0,%rax
je     129d <setlocalenv+0x10d>
mov    -0x30(%rbp),%rax
movsbl (%rax),%edi
call   1030 <toupper@plt>
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %cl,(%rax)
mov    -0x30(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isalnum@plt>
cmp    $0x0,%rax
jne    12c3 <setlocalenv+0x133>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5f,%eax
jne    12db <setlocalenv+0x14b>
mov    -0x30(%rbp),%rax
mov    (%rax),%cl
mov    -0x28(%rbp),%rax
mov    %cl,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    12e0 <setlocalenv+0x150>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1227 <setlocalenv+0x97>
mov    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1080 <memmove@plt>
mov    -0x20(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setlocalenv>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r13
call   1040 <strlen@plt>
mov    %rax,%r14
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
add    %r14,%rbx
lea    (%rax,%rbx,1),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     127f <setlocalenv+0xef>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
call   10a0 <sprintf@plt>
mov    %r14,%r15
mov    %r14,%rbx
jmp    120b <setlocalenv+0x7b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %cl,(%r15)
add    $0x1,%r15
add    $0x1,%rbx
movzbl (%rbx),%eax
cmp    $0x2d,%al
je     1220 <setlocalenv+0x90>
test   %al,%al
je     125b <setlocalenv+0xcb>
cmp    $0x3d,%al
jne    1225 <setlocalenv+0x95>
jmp    125b <setlocalenv+0xcb>
nopl   0x0(%rax)
movb   $0x5f,(%rbx)
mov    $0x5f,%al
movsbl %al,%edi
call   1070 <isalpha@plt>
mov    %rax,%rcx
movzbl (%rbx),%eax
test   %rcx,%rcx
je     1242 <setlocalenv+0xb2>
movsbl %al,%edi
call   1030 <toupper@plt>
mov    %al,(%rbx)
movsbl %al,%edi
call   1050 <isalnum@plt>
movzbl (%rbx),%ecx
test   %rax,%rax
jne    1200 <setlocalenv+0x70>
cmp    $0x5f,%cl
mov    $0x5f,%cl
je     1200 <setlocalenv+0x70>
jmp    1207 <setlocalenv+0x77>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <memmove@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xd7a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"malloc\00"
define i8* @setlocalenv(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%10 = load i8*, i8** %4
%11 = call i64 @strlen(i8* %10)
%12 = load i8*, i8** %5
%13 = call i64 @strlen(i8* %12)
%14 = add nsw i64 %11, %13
%15 = load i8*, i8** %6
%16 = call i64 @strlen(i8* %15)
%17 = add nsw i64 %14, %16
%18 = add nsw i64 %17, 1
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = icmp ne i8* %20, null
br i1 %21, label %25, label %22
22: ; preds = %3
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %3
%26 = load i8*, i8** %7
%27 = load i8*, i8** %4
%28 = load i8*, i8** %5
%29 = load i8*, i8** %6
%30 = call i32 @sprintf(i8* %26, i8* %27, i8* %28, i8* %29)
%31 = load i8*, i8** %7
store i8* %31, i8** %9
store i8* %31, i8** %8
br label %32
32: ; preds = %78, %25
%33 = load i8*, i8** %9
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 61
br i1 %36, label %37, label %42
37: ; preds = %32
%38 = load i8*, i8** %9
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp ne i32 %40, 0
br label %42
42: ; preds = %37, %32
%43 = phi i1 [ false, %32 ], [ %41, %37 ]
br i1 %43, label %44, label %81
44: ; preds = %42
%45 = load i8*, i8** %9
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 45
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = load i8*, i8** %9
store i8 95, i8* %50
br label %51
51: ; preds = %49, %44
%52 = load i8*, i8** %9
%53 = load i8, i8* %52
%54 = call i64 @isalpha(i8 signext %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %61
56: ; preds = %51
%57 = load i8*, i8** %9
%58 = load i8, i8* %57
%59 = call signext i8 @toupper(i8 signext %58)
%60 = load i8*, i8** %9
store i8 %59, i8* %60
br label %61
61: ; preds = %56, %51
%62 = load i8*, i8** %9
%63 = load i8, i8* %62
%64 = call i64 @isalnum(i8 signext %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %71, label %66
66: ; preds = %61
%67 = load i8*, i8** %9
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = icmp eq i32 %69, 95
br i1 %70, label %71, label %77
71: ; preds = %66, %61
%72 = load i8*, i8** %9
%73 = load i8, i8* %72
%74 = load i8*, i8** %8
store i8 %73, i8* %74
%75 = load i8*, i8** %8
%76 = getelementptr inbounds i8, i8* %75, i32 1
store i8* %76, i8** %8
br label %77
77: ; preds = %71, %66
br label %78
78: ; preds = %77
%79 = load i8*, i8** %9
%80 = getelementptr inbounds i8, i8* %79, i32 1
store i8* %80, i8** %9
br label %32
81: ; preds = %42
%82 = load i8*, i8** %8
%83 = load i8*, i8** %9
%84 = load i8*, i8** %9
%85 = call i64 @strlen(i8* %84)
%86 = add nsw i64 %85, 1
%87 = call i32 @memmove(i8* %82, i8* %83, i64 %86)
%88 = load i8*, i8** %7
ret i8* %88
}

LLVM IR (-O2):
@.str = [7 x i8] c"malloc\00"
define i8* @setlocalenv(i8* nocapture %0, i8* %1, i8* %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%7 = add i64 %4, 1
%8 = add i64 %7, %5
%9 = add i64 %8, %6
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %3
%16 = tail call i32 @sprintf(i8* %10, i8* dereferenceable(1) %0, i8* %1, i8* %2)
br label %17
17: ; preds = %39, %15
%18 = phi i8* [ %10, %15 ], [ %40, %39 ]
%19 = phi i8* [ %10, %15 ], [ %41, %39 ]
%20 = load i8, i8* %19
switch i8 %20, label %22 [
i8 61, label %42
i8 0, label %42
i8 45, label %21
]
21: ; preds = %17
store i8 95, i8* %19
br label %22
22: ; preds = %17, %21
%23 = phi i8 [ %20, %17 ], [ 95, %21 ]
%24 = tail call i64 @isalpha(i8 signext %23)
%25 = icmp eq i64 %24, 0
%26 = load i8, i8* %19
br i1 %25, label %29, label %27
27: ; preds = %22
%28 = tail call signext i8 @toupper(i8 signext %26)
store i8 %28, i8* %19
br label %29
29: ; preds = %22, %27
%30 = phi i8 [ %28, %27 ], [ %26, %22 ]
%31 = tail call i64 @isalnum(i8 signext %30)
%32 = icmp eq i64 %31, 0
%33 = load i8, i8* %19
br i1 %32, label %34, label %36
34: ; preds = %29
%35 = icmp eq i8 %33, 95
br i1 %35, label %36, label %39
36: ; preds = %29, %34
%37 = phi i8 [ 95, %34 ], [ %33, %29 ]
store i8 %37, i8* %18
%38 = getelementptr inbounds i8, i8* %18, i64 1
br label %39
39: ; preds = %34, %36
%40 = phi i8* [ %38, %36 ], [ %18, %34 ]
%41 = getelementptr inbounds i8, i8* %19, i64 1
br label %17
42: ; preds = %17, %17
%43 = tail call i64 @strlen(i8* %19)
%44 = add nsw i64 %43, 1
%45 = tail call i32 @memmove(i8* %18, i8* %19, i64 %44)
ret i8* %10
}