#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*/ environ_name_to_value(char *, char *);
scalar_t__ environ_variable_exists(char *, char *);
int /*<<< orphan*/ search_replace(char *, char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *resolve_environment_variables(char *return_string, char *s) {
  char variable_name[128];
  char variable_value[128];
  char looking_string[1024];
  char looking_string_old[1024];
  *return_string = '\0';
  strcpy(looking_string, s);
  while (1) {
    strcpy(looking_string_old, looking_string);
    if (environ_variable_exists(variable_name, looking_string)) {
      environ_name_to_value(variable_value, variable_name);
      search_replace(variable_name, variable_value, looking_string);
    }
    if (strcmp(looking_string, looking_string_old) == 0)
      break;
  }
  strcpy(return_string, looking_string);
  return return_string;
}


x86 assembly (clang, -O0):
<resolve_environment_variables>:
push   %rbp
mov    %rsp,%rbp
sub    $0x910,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x510(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x910(%rbp),%rdi
lea    -0x510(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x90(%rbp),%rdi
lea    -0x510(%rbp),%rsi
call   1070 <environ_variable_exists@plt>
cmp    $0x0,%rax
je     11d7 <resolve_environment_variables+0x87>
lea    -0x110(%rbp),%rdi
lea    -0x90(%rbp),%rsi
call   1060 <environ_name_to_value@plt>
lea    -0x90(%rbp),%rdi
lea    -0x110(%rbp),%rsi
lea    -0x510(%rbp),%rdx
call   1040 <search_replace@plt>
lea    -0x510(%rbp),%rdi
lea    -0x910(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11f9 <resolve_environment_variables+0xa9>
jmp    11fe <resolve_environment_variables+0xae>
jmp    117a <resolve_environment_variables+0x2a>
mov    -0x8(%rbp),%rdi
lea    -0x510(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rax
add    $0x910,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_environment_variables>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x900,%rsp
mov    %rdi,%r14
movb   $0x0,(%rdi)
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0x500(%rsp),%r15
lea    0x80(%rsp),%r12
mov    %rsp,%r13
jmp    11a0 <resolve_environment_variables+0x50>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11d6 <resolve_environment_variables+0x86>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1070 <environ_variable_exists@plt>
test   %rax,%rax
je     1190 <resolve_environment_variables+0x40>
mov    %r13,%rdi
mov    %r12,%rsi
call   1060 <environ_name_to_value@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1040 <search_replace@plt>
jmp    1190 <resolve_environment_variables+0x40>
lea    0x100(%rsp),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rax
add    $0x900,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @resolve_environment_variables(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [128 x i8]
%6 = alloca [128 x i8]
%7 = alloca [1024 x i8]
%8 = alloca [1024 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
store i8 0, i8* %9
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%11 = load i8*, i8** %4
%12 = call i32 @strcpy(i8* %10, i8* %11)
br label %13
13: ; preds = %2, %35
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%16 = call i32 @strcpy(i8* %14, i8* %15)
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%19 = call i64 @environ_variable_exists(i8* %17, i8* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %13
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%24 = call i32 @environ_name_to_value(i8* %22, i8* %23)
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%28 = call i32 @search_replace(i8* %25, i8* %26, i8* %27)
br label %29
29: ; preds = %21, %13
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%32 = call i64 @strcmp(i8* %30, i8* %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %29
br label %36
35: ; preds = %29
br label %13
36: ; preds = %34
%37 = load i8*, i8** %3
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%39 = call i32 @strcpy(i8* %37, i8* %38)
%40 = load i8*, i8** %3
ret i8* %40
}

LLVM IR (-O2):
define i8* @resolve_environment_variables(i8* returned %0, i8* %1) {
%3 = alloca [128 x i8]
%4 = alloca [128 x i8]
%5 = alloca [1024 x i8]
%6 = alloca [1024 x i8]
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
store i8 0, i8* %0
%11 = call i32 @strcpy(i8* %9, i8* %1)
br label %12
12: ; preds = %19, %2
%13 = call i32 @strcpy(i8* %10, i8* %9)
%14 = call i64 @environ_variable_exists(i8* %7, i8* %9)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = call i32 @environ_name_to_value(i8* %8, i8* %7)
%18 = call i32 @search_replace(i8* %7, i8* %8, i8* %9)
br label %19
19: ; preds = %16, %12
%20 = call i64 @strcmp(i8* %9, i8* %10)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %12
22: ; preds = %19
%23 = call i32 @strcpy(i8* %0, i8* %9)
ret i8* %0
}