#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*/ add_completion(int /*<<< orphan*/);
int /*<<< orphan*/ collect_builtin_env(char const *);
scalar_t__ str_has_prefix(char const *, char const *);
char *strchr(char const *, char);
int /*<<< orphan*/ xstrslice(char const *, int /*<<< orphan*/, int);
void collect_env(const char *prefix) {
  extern char **environ;
  int i;
  for (i = 0; environ[i]; i++) {
    const char *e = environ[i];
    if (str_has_prefix(e, prefix)) {
      const char *end = strchr(e, '=');
      if (end)
        add_completion(xstrslice(e, 0, end - e));
    }
  }
  collect_builtin_env(prefix);
}


x86 assembly (clang, -O0):
<collect_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%rax
movslq -0xc(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     11fd <collect_env+0xad>
mov    0x2e65(%rip),%rax
mov    (%rax),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <str_has_prefix@plt>
cmp    $0x0,%rax
je     11ea <collect_env+0x9a>
mov    -0x18(%rbp),%rdi
mov    $0x3d,%esi
call   1030 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11e5 <collect_env+0x95>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1040 <xstrslice@plt>
mov    %eax,%edi
call   1060 <add_completion@plt>
jmp    11ea <collect_env+0x9a>
jmp    11ef <collect_env+0x9f>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1163 <collect_env+0x13>
mov    -0x8(%rbp),%rdi
call   1070 <collect_builtin_env@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<collect_env>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e86(%rip),%r15
mov    (%r15),%rax
mov    (%rax),%rbx
test   %rbx,%rbx
je     11c9 <collect_env+0x79>
mov    $0x8,%r12d
jmp    1190 <collect_env+0x40>
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r15),%rax
mov    (%rax,%r12,1),%rbx
add    $0x8,%r12
test   %rbx,%rbx
je     11c9 <collect_env+0x79>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <str_has_prefix@plt>
test   %rax,%rax
je     1180 <collect_env+0x30>
mov    %rbx,%rdi
mov    $0x3d,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     1180 <collect_env+0x30>
sub    %ebx,%eax
mov    %rbx,%rdi
xor    %esi,%esi
mov    %eax,%edx
call   1040 <xstrslice@plt>
mov    %eax,%edi
call   1060 <add_completion@plt>
jmp    1180 <collect_env+0x30>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1070 <collect_builtin_env@plt>

LLVM IR (-O0):
@environ = external global i8**
define void @collect_env(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
store i32 0, i32* %3
br label %6
6: ; preds = %40, %1
%7 = load i8**, i8*** @environ
%8 = load i32, i32* %3
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8*, i8** %7, i64 %9
%11 = load i8*, i8** %10
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %43
13: ; preds = %6
%14 = load i8**, i8*** @environ
%15 = load i32, i32* %3
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8*, i8** %14, i64 %16
%18 = load i8*, i8** %17
store i8* %18, i8** %4
%19 = load i8*, i8** %4
%20 = load i8*, i8** %2
%21 = call i64 @str_has_prefix(i8* %19, i8* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %39
23: ; preds = %13
%24 = load i8*, i8** %4
%25 = call i8* @strchr(i8* %24, i8 signext 61)
store i8* %25, i8** %5
%26 = load i8*, i8** %5
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %38
28: ; preds = %23
%29 = load i8*, i8** %4
%30 = load i8*, i8** %5
%31 = load i8*, i8** %4
%32 = ptrtoint i8* %30 to i64
%33 = ptrtoint i8* %31 to i64
%34 = sub i64 %32, %33
%35 = trunc i64 %34 to i32
%36 = call i32 @xstrslice(i8* %29, i32 0, i32 %35)
%37 = call i32 @add_completion(i32 %36)
br label %38
38: ; preds = %28, %23
br label %39
39: ; preds = %38, %13
br label %40
40: ; preds = %39
%41 = load i32, i32* %3
%42 = add nsw i32 %41, 1
store i32 %42, i32* %3
br label %6
43: ; preds = %6
%44 = load i8*, i8** %2
%45 = call i32 @collect_builtin_env(i8* %44)
ret void
}

LLVM IR (-O2):
@environ = external global i8**
define void @collect_env(i8* %0) {
%2 = load i8**, i8*** @environ
%3 = load i8*, i8** %2
%4 = icmp eq i8* %3, null
br i1 %4, label %26, label %5
5: ; preds = %1, %20
%6 = phi i64 [ %21, %20 ], [ 0, %1 ]
%7 = phi i8* [ %24, %20 ], [ %3, %1 ]
%8 = tail call i64 @str_has_prefix(i8* %7, i8* %0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %20, label %10
10: ; preds = %5
%11 = tail call i8* @strchr(i8* %7, i8 signext 61)
%12 = icmp eq i8* %11, null
br i1 %12, label %20, label %13
13: ; preds = %10
%14 = ptrtoint i8* %11 to i64
%15 = ptrtoint i8* %7 to i64
%16 = sub i64 %14, %15
%17 = trunc i64 %16 to i32
%18 = tail call i32 @xstrslice(i8* %7, i32 0, i32 %17)
%19 = tail call i32 @add_completion(i32 %18)
br label %20
20: ; preds = %10, %13, %5
%21 = add nuw i64 %6, 1
%22 = load i8**, i8*** @environ
%23 = getelementptr inbounds i8*, i8** %22, i64 %21
%24 = load i8*, i8** %23
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %5
26: ; preds = %20, %1
%27 = tail call i32 @collect_builtin_env(i8* %0)
ret void
}