#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*/ EX_OSERR;
int /*<<< orphan*/ EX_USAGE;
int /*<<< orphan*/ check_dir(char *, size_t, char const *, char);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ free(char *);
char *getenv(char const *);
char *strdup(char *);
size_t strlen(char *);
char *strsep(char **, char *);
void parse_env(const char *envname, const char *expr, char output_delim) {
  char *directory, *envp, *string, *tofree;
  size_t dir_len;
  if (!(envp = getenv(envname)))
    errx(EX_USAGE, "no such environment variable '%s'", envname);
  if (!(string = strdup(envp)))
    err(EX_OSERR, "could not strdup environment");
  tofree = string;
  while ((directory = strsep(&string, ":")) != NULL) {
    dir_len = strlen(directory);
    if (dir_len > 0)
      check_dir(directory, dir_len, expr, output_delim);
  }
  free(tofree);
}


x86 assembly (clang, -O0):
<parse_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11c5 <parse_env+0x45>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe40(%rip),%rsi
call   1060 <errx@plt>
mov    -0x28(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11f1 <parse_env+0x71>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
call   1070 <err@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xe3b(%rip),%rsi
call   1080 <strsep@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1249 <parse_env+0xc9>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jbe    1244 <parse_env+0xc4>
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x10(%rbp),%rdx
movsbl -0x11(%rbp),%ecx
call   1090 <check_dir@plt>
jmp    11f9 <parse_env+0x79>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_env>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r13d
mov    %rsi,%r12
mov    %rdi,%rbp
call   1030 <getenv@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b9 <parse_env+0x39>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
mov    %rbp,%rdx
call   1060 <errx@plt>
mov    %rbx,%rdi
call   10a0 <strdup@plt>
mov    %rax,%r14
mov    %rax,(%rsp)
test   %rax,%rax
jne    11e2 <parse_env+0x62>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
call   1070 <err@plt>
lea    0xe56(%rip),%rsi
mov    %rsp,%rdi
call   1080 <strsep@plt>
test   %rax,%rax
je     1242 <parse_env+0xc2>
mov    %rax,%r15
mov    %r13d,%eax
lea    0xe3c(%rip),%r13
mov    %rsp,%rbx
movsbl %al,%ebp
jmp    1223 <parse_env+0xa3>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r13,%rsi
call   1080 <strsep@plt>
mov    %rax,%r15
test   %rax,%rax
je     1242 <parse_env+0xc2>
mov    %r15,%rdi
call   1050 <strlen@plt>
test   %rax,%rax
je     1210 <parse_env+0x90>
mov    %r15,%rdi
mov    %rax,%rsi
mov    %r12,%rdx
mov    %ebp,%ecx
call   1090 <check_dir@plt>
jmp    1210 <parse_env+0x90>
mov    %r14,%rdi
call   1040 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EX_USAGE = global i32 0
@.str = [34 x i8] c"no such environment variable '%s'\00"
@EX_OSERR = global i32 0
@.str.1 = [29 x i8] c"could not strdup environment\00"
@.str.2 = [2 x i8] c":\00"
define void @parse_env(i8* %0, i8* %1, i8 signext %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8 %2, i8* %6
%12 = load i8*, i8** %4
%13 = call i8* @getenv(i8* %12)
store i8* %13, i8** %8
%14 = icmp ne i8* %13, null
br i1 %14, label %19, label %15
15: ; preds = %3
%16 = load i32, i32* @EX_USAGE
%17 = load i8*, i8** %4
%18 = call i32 @errx(i32 %16, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %15, %3
%20 = load i8*, i8** %8
%21 = call i8* @strdup(i8* %20)
store i8* %21, i8** %9
%22 = icmp ne i8* %21, null
br i1 %22, label %26, label %23
23: ; preds = %19
%24 = load i32, i32* @EX_OSERR
%25 = call i32 @err(i32 %24, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %23, %19
%27 = load i8*, i8** %9
store i8* %27, i8** %10
br label %28
28: ; preds = %42, %26
%29 = call i8* @strsep(i8** %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %29, i8** %7
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %43
31: ; preds = %28
%32 = load i8*, i8** %7
%33 = call i64 @strlen(i8* %32)
store i64 %33, i64* %11
%34 = load i64, i64* %11
%35 = icmp ugt i64 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %31
%37 = load i8*, i8** %7
%38 = load i64, i64* %11
%39 = load i8*, i8** %5
%40 = load i8, i8* %6
%41 = call i32 @check_dir(i8* %37, i64 %38, i8* %39, i8 signext %40)
br label %42
42: ; preds = %36, %31
br label %28
43: ; preds = %28
%44 = load i8*, i8** %10
%45 = call i32 @free(i8* %44)
ret void
}

LLVM IR (-O2):
@EX_USAGE = global i32 0
@.str = [34 x i8] c"no such environment variable '%s'\00"
@EX_OSERR = global i32 0
@.str.1 = [29 x i8] c"could not strdup environment\00"
@.str.2 = [2 x i8] c":\00"
define void @parse_env(i8* %0, i8* %1, i8 signext %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = tail call i8* @getenv(i8* %0)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = load i32, i32* @EX_USAGE
%10 = tail call i32 @errx(i32 %9, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %11
11: ; preds = %8, %3
%12 = tail call i8* @strdup(i8* %6)
store i8* %12, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @EX_OSERR
%16 = tail call i32 @err(i32 %15, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %14, %11
%18 = call i8* @strsep(i8** %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %29, label %20
20: ; preds = %17, %26
%21 = phi i8* [ %27, %26 ], [ %18, %17 ]
%22 = call i64 @strlen(i8* %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = call i32 @check_dir(i8* %21, i64 %22, i8* %1, i8 signext %2)
br label %26
26: ; preds = %24, %20
%27 = call i8* @strsep(i8** %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %20
29: ; preds = %26, %17
%30 = call i32 @free(i8* %12)
ret void
}