#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 *concat(char *, char *);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
scalar_t__ is_nested(char *);
char *prepare_value(char *);
char *strtok_r(char *, char *, char **);
char *parse_value(char *value) {
  value = prepare_value(value);
  char *search = value, *parsed = NULL, *tok_ptr;
  char *name;
  if (value && is_nested(value)) {
    while (1) {
      parsed = concat(parsed, strtok_r(search, "${", &tok_ptr));
      name = strtok_r(NULL, "}", &tok_ptr);
      if (!name) {
        break;
      }
      parsed = concat(parsed, getenv(name + 1));
      search = NULL;
    }
    free(value);
    return parsed;
  }
  return value;
}


x86 assembly (clang, -O0):
<parse_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <prepare_value@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1248 <parse_value+0xe8>
mov    -0x10(%rbp),%rdi
call   1060 <is_nested@plt>
cmp    $0x0,%rax
je     1248 <parse_value+0xe8>
jmp    11ac <parse_value+0x4c>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe41(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1080 <strtok_r@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rsi
call   1070 <concat@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe20(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1080 <strtok_r@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1200 <parse_value+0xa0>
jmp    1232 <parse_value+0xd2>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1030 <getenv@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,%rsi
call   1070 <concat@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
jmp    11ac <parse_value+0x4c>
mov    -0x10(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1250 <parse_value+0xf0>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_value>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
call   1050 <prepare_value@plt>
test   %rax,%rax
je     1234 <parse_value+0xd4>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <is_nested@plt>
test   %rax,%rax
je     1237 <parse_value+0xd7>
lea    0xe6a(%rip),%rsi
lea    0x8(%rsp),%r15
mov    %r14,%rdi
mov    %r15,%rdx
call   1080 <strtok_r@plt>
xor    %edi,%edi
mov    %rax,%rsi
call   1070 <concat@plt>
mov    %rax,%rbx
lea    0xe49(%rip),%rsi
xor    %edi,%edi
mov    %r15,%rdx
call   1080 <strtok_r@plt>
test   %rax,%rax
je     1227 <parse_value+0xc7>
lea    0xe30(%rip),%r15
lea    0x8(%rsp),%r12
lea    0xe27(%rip),%r13
nopl   0x0(%rax)
add    $0x1,%rax
mov    %rax,%rdi
call   1030 <getenv@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1070 <concat@plt>
mov    %rax,%rbx
xor    %edi,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1080 <strtok_r@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1070 <concat@plt>
mov    %rax,%rbx
xor    %edi,%edi
mov    %r13,%rsi
mov    %r12,%rdx
call   1080 <strtok_r@plt>
test   %rax,%rax
jne    11e0 <parse_value+0x80>
mov    %r14,%rdi
call   1040 <free@plt>
mov    %rbx,%r14
jmp    1237 <parse_value+0xd7>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"${\00"
@.str.1 = [2 x i8] c"}\00"
define i8* @parse_value(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i8* @prepare_value(i8* %8)
store i8* %9, i8** %3
%10 = load i8*, i8** %3
store i8* %10, i8** %4
store i8* null, i8** %5
%11 = load i8*, i8** %3
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %37
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = call i64 @is_nested(i8* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %37
17: ; preds = %13
br label %18
18: ; preds = %17, %27
%19 = load i8*, i8** %5
%20 = load i8*, i8** %4
%21 = call i8* @strtok_r(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %6)
%22 = call i8* @concat(i8* %19, i8* %21)
store i8* %22, i8** %5
%23 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %6)
store i8* %23, i8** %7
%24 = load i8*, i8** %7
%25 = icmp ne i8* %24, null
br i1 %25, label %27, label %26
26: ; preds = %18
br label %33
27: ; preds = %18
%28 = load i8*, i8** %5
%29 = load i8*, i8** %7
%30 = getelementptr inbounds i8, i8* %29, i64 1
%31 = call i8* @getenv(i8* %30)
%32 = call i8* @concat(i8* %28, i8* %31)
store i8* %32, i8** %5
store i8* null, i8** %4
br label %18
33: ; preds = %26
%34 = load i8*, i8** %3
%35 = call i32 @free(i8* %34)
%36 = load i8*, i8** %5
store i8* %36, i8** %2
br label %39
37: ; preds = %13, %1
%38 = load i8*, i8** %3
store i8* %38, i8** %2
br label %39
39: ; preds = %37, %33
%40 = load i8*, i8** %2
ret i8* %40
}

LLVM IR (-O2):
@.str = [3 x i8] c"${\00"
@.str.1 = [2 x i8] c"}\00"
define i8* @parse_value(i8* %0) {
%2 = alloca i8*
%3 = tail call i8* @prepare_value(i8* %0)
%4 = bitcast i8** %2 to i8*
%5 = icmp eq i8* %3, null
br i1 %5, label %27, label %6
6: ; preds = %1
%7 = tail call i64 @is_nested(i8* %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %27, label %9
9: ; preds = %6
%10 = call i8* @strtok_r(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %2)
%11 = call i8* @concat(i8* null, i8* %10)
%12 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %2)
%13 = icmp eq i8* %12, null
br i1 %13, label %24, label %14
14: ; preds = %9, %14
%15 = phi i8* [ %22, %14 ], [ %12, %9 ]
%16 = phi i8* [ %21, %14 ], [ %11, %9 ]
%17 = getelementptr inbounds i8, i8* %15, i64 1
%18 = call i8* @getenv(i8* %17)
%19 = call i8* @concat(i8* %16, i8* %18)
%20 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %2)
%21 = call i8* @concat(i8* %19, i8* %20)
%22 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %2)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %14
24: ; preds = %14, %9
%25 = phi i8* [ %11, %9 ], [ %21, %14 ]
%26 = call i32 @free(i8* %3)
br label %27
27: ; preds = %1, %6, %24
%28 = phi i8* [ %25, %24 ], [ %3, %6 ], [ null, %1 ]
ret i8* %28
}