#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 */
char *getenv(char *);
scalar_t__ malloc_check(char *, int);
char *strchr(char const *, char);
int strlen(char const *);
char *strsave(char const *, char *);
char **shellpath(void) {
  const char *path = getenv("PATH");
  if (!path)
    path = "./";
  char **vector = (char **)malloc_check("hold path elements",
                                        strlen(path) * sizeof(*vector));
  const char *p = path;
  int next = 0;
  while (p) {
    char *q = strchr(p, ':');
    vector[next++] = strsave(p, q);
    p = q ? q + 1 : NULL;
  }
  vector[next] = NULL;
  return vector;
}


x86 assembly (clang, -O0):
<shellpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xea1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    117e <shellpath+0x2e>
lea    0xe8b(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
cltq
shl    $0x3,%rax
mov    %eax,%esi
lea    0xe72(%rip),%rdi
call   1060 <malloc_check@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1225 <shellpath+0xd5>
mov    -0x18(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <strsave@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
cmpq   $0x0,-0x28(%rbp)
je     120d <shellpath+0xbd>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1218 <shellpath+0xc8>
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
jmp    1218 <shellpath+0xc8>
mov    -0x30(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    11ae <shellpath+0x5e>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shellpath>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xea1(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe97(%rip),%r15
cmovne %rax,%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0x0(,%rax,8),%esi
lea    0xe80(%rip),%rdi
call   1060 <malloc_check@plt>
mov    %rax,%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,%r12
mov    %r15,%rdi
mov    %rax,%rsi
call   1070 <strsave@plt>
mov    %rax,(%r14,%rbx,8)
add    $0x1,%rbx
lea    0x1(%r12),%r15
test   %r12,%r12
jne    11a0 <shellpath+0x50>
mov    %ebx,%eax
movq   $0x0,(%r14,%rax,8)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [3 x i8] c"./\00"
@.str.2 = [19 x i8] c"hold path elements\00"
define i8** @shellpath() {
%1 = alloca i8*
%2 = alloca i8**
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %6, i8** %1
%7 = load i8*, i8** %1
%8 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %0
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %1
br label %10
10: ; preds = %9, %0
%11 = load i8*, i8** %1
%12 = call i32 @strlen(i8* %11)
%13 = sext i32 %12 to i64
%14 = mul i64 %13, 8
%15 = trunc i64 %14 to i32
%16 = call i64 @malloc_check(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = inttoptr i64 %16 to i8**
store i8** %17, i8*** %2
%18 = load i8*, i8** %1
store i8* %18, i8** %3
store i32 0, i32* %4
br label %19
19: ; preds = %39, %10
%20 = load i8*, i8** %3
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %41
22: ; preds = %19
%23 = load i8*, i8** %3
%24 = call i8* @strchr(i8* %23, i8 signext 58)
store i8* %24, i8** %5
%25 = load i8*, i8** %3
%26 = load i8*, i8** %5
%27 = call i8* @strsave(i8* %25, i8* %26)
%28 = load i8**, i8*** %2
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 1
store i32 %30, i32* %4
%31 = sext i32 %29 to i64
%32 = getelementptr inbounds i8*, i8** %28, i64 %31
store i8* %27, i8** %32
%33 = load i8*, i8** %5
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %38
35: ; preds = %22
%36 = load i8*, i8** %5
%37 = getelementptr inbounds i8, i8* %36, i64 1
br label %39
38: ; preds = %22
br label %39
39: ; preds = %38, %35
%40 = phi i8* [ %37, %35 ], [ null, %38 ]
store i8* %40, i8** %3
br label %19
41: ; preds = %19
%42 = load i8**, i8*** %2
%43 = load i32, i32* %4
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8*, i8** %42, i64 %44
store i8* null, i8** %45
%46 = load i8**, i8*** %2
ret i8** %46
}

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [3 x i8] c"./\00"
@.str.2 = [19 x i8] c"hold path elements\00"
define i8** @shellpath() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
%3 = select i1 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %1
%4 = tail call i32 @strlen(i8* %3)
%5 = shl i32 %4, 3
%6 = tail call i64 @malloc_check(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %5)
%7 = inttoptr i64 %6 to i8**
br label %8
8: ; preds = %0, %8
%9 = phi i64 [ 0, %0 ], [ %13, %8 ]
%10 = phi i8* [ %3, %0 ], [ %16, %8 ]
%11 = tail call i8* @strchr(i8* %10, i8 signext 58)
%12 = tail call i8* @strsave(i8* %10, i8* %11)
%13 = add nuw i64 %9, 1
%14 = getelementptr inbounds i8*, i8** %7, i64 %9
store i8* %12, i8** %14
%15 = icmp eq i8* %11, null
%16 = getelementptr inbounds i8, i8* %11, i64 1
br i1 %15, label %17, label %8
17: ; preds = %8
%18 = and i64 %13, 4294967295
%19 = getelementptr inbounds i8*, i8** %7, i64 %18
store i8* null, i8** %19
ret i8** %7
}