#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 strlen(char const *);
scalar_t__ strncmp(char const *, char *, int const);
char *find_env(const char *prefix, char *defaultstr) {
  extern char **environ;
  char **ptr;
  const int len = strlen(prefix);
  for (ptr = environ; *ptr != NULL; ptr++) {
    if (strncmp(prefix, *ptr, len) == 0)
      return *ptr;
  }
  return defaultstr;
}


x86 assembly (clang, -O0):
<find_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     119b <find_env+0x7b>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x24(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1185 <find_env+0x65>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11a3 <find_env+0x83>
jmp    118a <find_env+0x6a>
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
jmp    114a <find_env+0x2a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_env>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    0x2eac(%rip),%rax
mov    (%rax),%rbp
mov    0x0(%rbp),%rsi
test   %rsi,%rsi
je     1173 <find_env+0x53>
add    $0x8,%rbp
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r15d,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     116f <find_env+0x4f>
mov    0x0(%rbp),%rsi
add    $0x8,%rbp
test   %rsi,%rsi
jne    1150 <find_env+0x30>
jmp    1173 <find_env+0x53>
mov    -0x8(%rbp),%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@environ = external global i8**
define i8* @find_env(i8* %0, i8* %1) {
%3 = alloca i8*
%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)
store i32 %9, i32* %7
%10 = load i8**, i8*** @environ
store i8** %10, i8*** %6
br label %11
11: ; preds = %26, %2
%12 = load i8**, i8*** %6
%13 = load i8*, i8** %12
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %29
15: ; preds = %11
%16 = load i8*, i8** %4
%17 = load i8**, i8*** %6
%18 = load i8*, i8** %17
%19 = load i32, i32* %7
%20 = call i64 @strncmp(i8* %16, i8* %18, i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i8**, i8*** %6
%24 = load i8*, i8** %23
store i8* %24, i8** %3
br label %31
25: ; preds = %15
br label %26
26: ; preds = %25
%27 = load i8**, i8*** %6
%28 = getelementptr inbounds i8*, i8** %27, i32 1
store i8** %28, i8*** %6
br label %11
29: ; preds = %11
%30 = load i8*, i8** %5
store i8* %30, i8** %3
br label %31
31: ; preds = %29, %22
%32 = load i8*, i8** %3
ret i8* %32
}

LLVM IR (-O2):
@environ = external global i8**
define i8* @find_env(i8* %0, i8* readnone %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = load i8**, i8*** @environ
%5 = load i8*, i8** %4
%6 = icmp eq i8* %5, null
br i1 %6, label %18, label %7
7: ; preds = %2, %14
%8 = phi i8* [ %16, %14 ], [ %5, %2 ]
%9 = phi i8** [ %15, %14 ], [ %4, %2 ]
%10 = tail call i64 @strncmp(i8* %0, i8* %8, i32 %3)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = load i8*, i8** %9
br label %18
14: ; preds = %7
%15 = getelementptr inbounds i8*, i8** %9, i64 1
%16 = load i8*, i8** %15
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %7
18: ; preds = %14, %2, %12
%19 = phi i8* [ %13, %12 ], [ %1, %2 ], [ %1, %14 ]
ret i8* %19
}