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

/* Variables and functions */
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *find_command_path(char *command, char *command_path) {
  strcpy(command_path, "");
  char *which_command = (char *)malloc((30) * sizeof(char));
  strcpy(which_command, "which ");
  strcat(which_command, command);
  FILE *fp = popen(which_command, "r");
  char tmp[10];
  while (fgets(tmp, sizeof(tmp), fp) != 0)
    strcat(command_path, tmp);
  pclose(fp);
  command_path[strlen(command_path) - 1] = '\0';
  return (0);
}


x86 assembly (clang, -O0):
<find_command_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   1030 <strcpy@plt>
mov    $0x1e,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x18(%rbp),%rdi
lea    0xe41(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x20(%rbp)
lea    -0x2a(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0xa,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     11fd <find_command_path+0x8d>
mov    -0x10(%rbp),%rdi
lea    -0x2a(%rbp),%rsi
call   1090 <strcat@plt>
jmp    11cf <find_command_path+0x5f>
mov    -0x20(%rbp),%rdi
call   1050 <pclose@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_command_path>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe80(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    $0x1e,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
lea    0xe5e(%rip),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1090 <strcat@plt>
lea    0xe4b(%rip),%rsi
mov    %rbx,%rdi
call   1080 <popen@plt>
mov    %rax,%r15
lea    0x6(%rsp),%rdi
mov    $0xa,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1210 <find_command_path+0xa0>
lea    0x6(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    11f0 <find_command_path+0x80>
mov    %r15,%rdi
call   1050 <pclose@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r14,1)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [7 x i8] c"which \00"
@.str.2 = [2 x i8] c"r\00"
define i8* @find_command_path(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [10 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %4
%9 = call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%10 = call i64 @malloc(i32 30)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%13 = call i32 @strcpy(i8* %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%14 = load i8*, i8** %5
%15 = load i8*, i8** %3
%16 = call i32 @strcat(i8* %14, i8* %15)
%17 = load i8*, i8** %5
%18 = call i32* @popen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %18, i32** %6
br label %19
19: ; preds = %24, %2
%20 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%21 = load i32*, i32** %6
%22 = call i64 @fgets(i8* %20, i32 10, i32* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i8*, i8** %4
%26 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%27 = call i32 @strcat(i8* %25, i8* %26)
br label %19
28: ; preds = %19
%29 = load i32*, i32** %6
%30 = call i32 @pclose(i32* %29)
%31 = load i8*, i8** %4
%32 = load i8*, i8** %4
%33 = call i32 @strlen(i8* %32)
%34 = sub nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %31, i64 %35
store i8 0, i8* %36
ret i8* null
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [7 x i8] c"which \00"
@.str.2 = [2 x i8] c"r\00"
define noalias i8* @find_command_path(i8* %0, i8* %1) {
%3 = alloca [10 x i8]
%4 = tail call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%5 = tail call i64 @malloc(i32 30)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @strcat(i8* %6, i8* %0)
%9 = tail call i32* @popen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%10 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%11 = call i64 @fgets(i8* %10, i32 10, i32* %9)
%12 = icmp eq i64 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %2, %13
%14 = call i32 @strcat(i8* %1, i8* %10)
%15 = call i64 @fgets(i8* %10, i32 10, i32* %9)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %13
17: ; preds = %13, %2
%18 = call i32 @pclose(i32* %9)
%19 = call i32 @strlen(i8* %1)
%20 = add nsw i32 %19, -1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %1, i64 %21
store i8 0, i8* %22
ret i8* null
}