#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 */
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
char *strdup(char *);
int strlen(char const *);
int /*<<< orphan*/ strtok(char *, char *);
char *absolutePathForCommand(const char *cmd) {
  int cmdlen = strlen(cmd);
  char *goodcmd = malloc(cmdlen + 128);
  sprintf(goodcmd, "which %s", cmd);
  FILE *fp = popen(goodcmd, "r");
  char buf[32768];
  int rv;
  char *result = NULL;
  if ((rv = fread(buf, 1, sizeof(buf) - 1, fp)) > 0) {
    buf[rv] = 0;
    result = strdup(buf);
    strtok(result, "\r\n");
  }
  free(goodcmd);
  pclose(fp);
  return result;
}


x86 assembly (clang, -O0):
<absolutePathForCommand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8030,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
add    $0x80,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe34(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe2d(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x8030(%rbp)
lea    -0x8020(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x7fff,%edx
call   1040 <fread@plt>
mov    %eax,-0x8024(%rbp)
cmp    $0x0,%eax
jle    124e <absolutePathForCommand+0xbe>
movslq -0x8024(%rbp),%rax
movb   $0x0,-0x8020(%rbp,%rax,1)
lea    -0x8020(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x8030(%rbp)
mov    -0x8030(%rbp),%rdi
lea    0xdc2(%rip),%rsi
call   1090 <strtok@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1060 <pclose@plt>
mov    -0x8030(%rbp),%rax
add    $0x8030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<absolutePathForCommand>:
push   %r15
push   %r14
push   %rbx
sub    $0x8000,%rsp
mov    %rdi,%rbx
call   1050 <strlen@plt>
lea    0x80(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%r15
lea    0xe47(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   10a0 <sprintf@plt>
lea    0xe3e(%rip),%rsi
mov    %r15,%rdi
call   1080 <popen@plt>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x7fff,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jle    1211 <absolutePathForCommand+0x81>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
call   10b0 <strdup@plt>
mov    %rax,%rbx
lea    0xe04(%rip),%rsi
mov    %rax,%rdi
call   1090 <strtok@plt>
jmp    1213 <absolutePathForCommand+0x83>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1060 <pclose@plt>
mov    %rbx,%rax
add    $0x8000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [9 x i8] c"which %s\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"\0D\0A\00"
define i8* @absolutePathForCommand(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [32768 x i8]
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = add nsw i32 %11, 128
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %4
%14 = load i8*, i8** %4
%15 = load i8*, i8** %2
%16 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i8*, i8** %4
%18 = call i32* @popen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %5
store i8* null, i8** %8
%19 = getelementptr inbounds [32768 x i8], [32768 x i8]* %6, i64 0, i64 0
%20 = load i32*, i32** %5
%21 = call i32 @fread(i8* %19, i32 1, i32 32767, i32* %20)
store i32 %21, i32* %7
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %1
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [32768 x i8], [32768 x i8]* %6, i64 0, i64 %25
store i8 0, i8* %26
%27 = getelementptr inbounds [32768 x i8], [32768 x i8]* %6, i64 0, i64 0
%28 = call i8* @strdup(i8* %27)
store i8* %28, i8** %8
%29 = load i8*, i8** %8
%30 = call i32 @strtok(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %23, %1
%32 = load i8*, i8** %4
%33 = call i32 @free(i8* %32)
%34 = load i32*, i32** %5
%35 = call i32 @pclose(i32* %34)
%36 = load i8*, i8** %8
ret i8* %36
}

LLVM IR (-O2):
@.str = [9 x i8] c"which %s\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"\0D\0A\00"
define i8* @absolutePathForCommand(i8* %0) {
%2 = alloca [32768 x i8]
%3 = tail call i32 @strlen(i8* %0)
%4 = add nsw i32 %3, 128
%5 = tail call i8* @malloc(i32 %4)
%6 = tail call i32 @sprintf(i8* dereferenceable(1) %5, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = tail call i32* @popen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = getelementptr inbounds [32768 x i8], [32768 x i8]* %2, i64 0, i64 0
%9 = call i32 @fread(i8* %8, i32 1, i32 32767, i32* %7)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = zext i32 %9 to i64
%13 = getelementptr inbounds [32768 x i8], [32768 x i8]* %2, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i8* @strdup(i8* %8)
%15 = tail call i32 @strtok(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %11, %1
%17 = phi i8* [ %14, %11 ], [ null, %1 ]
%18 = tail call i32 @free(i8* %5)
%19 = tail call i32 @pclose(i32* %7)
ret i8* %17
}