#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int open(char *, int /*<<< orphan*/);
char *sstrdup(char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char *strtok(char *, char *);
char *get_path(char *paths, char *file) {
  char *backpaths = sstrdup(paths);
  char *dir = strtok(backpaths, ":");
  while (dir) {
    char *ret = malloc(strlen(dir) + strlen("/") + strlen(file) + 1);
    int tmpfd;
    strcpy(ret, dir);
    strcat(ret, "/");
    strcat(ret, file);
    tmpfd = open(ret, O_RDONLY);
    if (tmpfd > 0) {
      close(tmpfd);
      free(backpaths);
      return ret;
    }
    free(ret);
    dir = strtok(NULL, ":");
  }
  free(backpaths);
  return NULL;
}


x86 assembly (clang, -O0):
<get_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <sstrdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe48(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1299 <get_path+0x109>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x48(%rbp)
lea    0xe22(%rip),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%rdi
lea    0xdda(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10b0 <strcat@plt>
mov    -0x30(%rbp),%rdi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    1277 <get_path+0xe7>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12aa <get_path+0x11a>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd75(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    11c1 <get_path+0x31>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
call   1060 <sstrdup@plt>
mov    %rax,%rbx
lea    0xe50(%rip),%rsi
mov    %rax,%rdi
call   10a0 <strtok@plt>
test   %rax,%rax
mov    %rbx,0x10(%rsp)
je     1254 <get_path+0xc4>
mov    %rax,%rbp
lea    0xe32(%rip),%r12
mov    0x2e11(%rip),%r13
xor    %eax,%eax
mov    %rax,0x8(%rsp)
xchg   %ax,%ax
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbp,%rsi
call   1040 <strcpy@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   10b0 <strcat@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   10b0 <strcat@plt>
mov    0x0(%r13),%esi
mov    %r15,%rdi
call   1090 <open@plt>
test   %eax,%eax
jg     125d <get_path+0xcd>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %edi,%edi
lea    0xdbb(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11e0 <get_path+0x50>
jmp    1269 <get_path+0xd9>
xor    %eax,%eax
mov    %rax,0x8(%rsp)
jmp    1269 <get_path+0xd9>
mov    %eax,%edi
call   1070 <close@plt>
mov    %r15,0x8(%rsp)
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c":\00"
@.str.1 = [2 x i8] c"/\00"
@O_RDONLY = global i32 0
define i8* @get_path(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = call i8* @sstrdup(i8* %10)
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = call i8* @strtok(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %13, i8** %7
br label %14
14: ; preds = %46, %2
%15 = load i8*, i8** %7
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %50
17: ; preds = %14
%18 = load i8*, i8** %7
%19 = call i64 @strlen(i8* %18)
%20 = call i64 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = add nsw i64 %19, %20
%22 = load i8*, i8** %5
%23 = call i64 @strlen(i8* %22)
%24 = add nsw i64 %21, %23
%25 = add nsw i64 %24, 1
%26 = call i8* @malloc(i64 %25)
store i8* %26, i8** %8
%27 = load i8*, i8** %8
%28 = load i8*, i8** %7
%29 = call i32 @strcpy(i8* %27, i8* %28)
%30 = load i8*, i8** %8
%31 = call i32 @strcat(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%32 = load i8*, i8** %8
%33 = load i8*, i8** %5
%34 = call i32 @strcat(i8* %32, i8* %33)
%35 = load i8*, i8** %8
%36 = load i32, i32* @O_RDONLY
%37 = call i32 @open(i8* %35, i32 %36)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %17
%41 = load i32, i32* %9
%42 = call i32 @close(i32 %41)
%43 = load i8*, i8** %6
%44 = call i32 @free(i8* %43)
%45 = load i8*, i8** %8
store i8* %45, i8** %3
br label %53
46: ; preds = %17
%47 = load i8*, i8** %8
%48 = call i32 @free(i8* %47)
%49 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %49, i8** %7
br label %14
50: ; preds = %14
%51 = load i8*, i8** %6
%52 = call i32 @free(i8* %51)
store i8* null, i8** %3
br label %53
53: ; preds = %50, %40
%54 = load i8*, i8** %3
ret i8* %54
}

LLVM IR (-O2):
@.str = [2 x i8] c":\00"
@.str.1 = [2 x i8] c"/\00"
@O_RDONLY = global i32 0
define i8* @get_path(i8* %0, i8* %1) {
%3 = tail call i8* @sstrdup(i8* %0)
%4 = tail call i8* @strtok(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %25, label %6
6: ; preds = %2, %21
%7 = phi i8* [ %23, %21 ], [ %4, %2 ]
%8 = tail call i64 @strlen(i8* %7)
%9 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%10 = add i64 %8, 2
%11 = add i64 %10, %9
%12 = tail call i8* @malloc(i64 %11)
%13 = tail call i32 @strcpy(i8* %12, i8* %7)
%14 = tail call i32 @strcat(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @strcat(i8* %12, i8* %1)
%16 = load i32, i32* @O_RDONLY
%17 = tail call i32 @open(i8* %12, i32 %16)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %6
%20 = tail call i32 @close(i32 %17)
br label %25
21: ; preds = %6
%22 = tail call i32 @free(i8* %12)
%23 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %6
25: ; preds = %21, %2, %19
%26 = phi i8* [ %12, %19 ], [ null, %2 ], [ null, %21 ]
%27 = tail call i32 @free(i8* %3)
ret i8* %26
}