#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 contains(char *, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ strcpy(char *, char *);
char *search_for_value(char *envp[], char *key) {
  int i = 0;
  while (envp[i] != NULL) {
    int index = contains(envp[i], key);
    if (index >= 0) {
      char *result = (char *)malloc(sizeof(char) * 100);
      memset(result, '\0', 100);
      strcpy(result, envp[i] + (index + 1));
      return result;
    }
    i++;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<search_for_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     11e5 <search_for_value+0xa5>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <contains@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     11d7 <search_for_value+0x97>
mov    $0x64,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x20(%rbp),%eax
add    $0x1,%eax
cltq
add    %rax,%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ed <search_for_value+0xad>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1157 <search_for_value+0x17>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_for_value>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
mov    (%rdi),%rdi
test   %rdi,%rdi
je     117c <search_for_value+0x3c>
mov    %rsi,%r12
xor    %r14d,%r14d
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %r12,%rsi
call   1040 <contains@plt>
test   %eax,%eax
jns    1181 <search_for_value+0x41>
mov    0x8(%r15,%rbx,8),%rdi
add    $0x1,%rbx
test   %rdi,%rdi
jne    1160 <search_for_value+0x20>
jmp    11b7 <search_for_value+0x77>
xor    %r14d,%r14d
jmp    11b7 <search_for_value+0x77>
mov    %eax,%ebp
mov    %ebx,%ebx
mov    $0x64,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
mov    (%r15,%rbx,8),%rax
mov    %ebp,%ecx
lea    (%rcx,%rax,1),%rsi
add    $0x1,%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @search_for_value(i8** %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i32 0, i32* %6
br label %9
9: ; preds = %43, %2
%10 = load i8**, i8*** %4
%11 = load i32, i32* %6
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8*, i8** %10, i64 %12
%14 = load i8*, i8** %13
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %46
16: ; preds = %9
%17 = load i8**, i8*** %4
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8*, i8** %17, i64 %19
%21 = load i8*, i8** %20
%22 = load i8*, i8** %5
%23 = call i32 @contains(i8* %21, i8* %22)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp sge i32 %24, 0
br i1 %25, label %26, label %43
26: ; preds = %16
%27 = call i64 @malloc(i32 100)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %8
%29 = load i8*, i8** %8
%30 = call i32 @memset(i8* %29, i8 signext 0, i32 100)
%31 = load i8*, i8** %8
%32 = load i8**, i8*** %4
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8*, i8** %32, i64 %34
%36 = load i8*, i8** %35
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
%41 = call i32 @strcpy(i8* %31, i8* %40)
%42 = load i8*, i8** %8
store i8* %42, i8** %3
br label %47
43: ; preds = %16
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 1
store i32 %45, i32* %6
br label %9
46: ; preds = %9
store i8* null, i8** %3
br label %47
47: ; preds = %46, %26
%48 = load i8*, i8** %3
ret i8* %48
}

LLVM IR (-O2):
define i8* @search_for_value(i8** nocapture %0, i8* %1) {
%3 = load i8*, i8** %0
%4 = icmp eq i8* %3, null
br i1 %4, label %26, label %5
5: ; preds = %2, %21
%6 = phi i64 [ %22, %21 ], [ 0, %2 ]
%7 = phi i8* [ %24, %21 ], [ %3, %2 ]
%8 = tail call i32 @contains(i8* %7, i8* %1)
%9 = icmp sgt i32 %8, -1
br i1 %9, label %10, label %21
10: ; preds = %5
%11 = and i64 %6, 4294967295
%12 = getelementptr inbounds i8*, i8** %0, i64 %11
%13 = tail call i64 @malloc(i32 100)
%14 = inttoptr i64 %13 to i8*
%15 = tail call i32 @memset(i8* %14, i8 signext 0, i32 100)
%16 = load i8*, i8** %12
%17 = add nuw nsw i32 %8, 1
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = tail call i32 @strcpy(i8* %14, i8* %19)
br label %26
21: ; preds = %5
%22 = add nuw i64 %6, 1
%23 = getelementptr inbounds i8*, i8** %0, i64 %22
%24 = load i8*, i8** %23
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %5
26: ; preds = %21, %2, %10
%27 = phi i8* [ %14, %10 ], [ null, %2 ], [ null, %21 ]
ret i8* %27
}