#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
scalar_t__ findLibHelper(char const *, char *);
int /*<<< orphan*/ *fopen(char *, char *);
char *getenv(char *);
scalar_t__ isspace(char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strtok(char *, char *);
int platform_findlib(const char *name, char *buffer, int len) {
  FILE *file;
  char *libpaths;
  char *token;
  libpaths = getenv("LD_LIBRARY_PATH");
  token = libpaths != NULL ? strtok(libpaths, ":") : NULL;
  while (token != NULL) {
    if (findLibHelper(name, buffer)) {
      strcpy(buffer, token);
      return 1;
    }
    token = strtok(NULL, ":");
  }
  file = fopen("/etc/ld.so.conf", "rt");
  if (file == NULL)
    return 0;
  while (!feof(file)) {
    char *ptr;
    fgets(buffer, 4096, file);
    ptr = &buffer[strlen(buffer) - 1];
    while (isspace(*ptr))
      *(ptr--) = '\0';
    if (findLibHelper(name, buffer)) {
      fclose(file);
      return 1;
    }
  }
  fclose(file);
  if (findLibHelper(name, "/lib")) {
    strcpy(buffer, "/lib");
    return 1;
  }
  if (findLibHelper(name, "/usr/lib")) {
    strcpy(buffer, "/usr/lib");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<platform_findlib>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    0xe46(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11e7 <platform_findlib+0x47>
mov    -0x30(%rbp),%rdi
lea    0xe37(%rip),%rsi
call   10c0 <strtok@plt>
mov    %rax,-0x48(%rbp)
jmp    11f2 <platform_findlib+0x52>
xor    %eax,%eax
mov    %rax,-0x48(%rbp)
jmp    11f2 <platform_findlib+0x52>
mov    -0x48(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     124e <platform_findlib+0xae>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <findLibHelper@plt>
cmp    $0x0,%rax
je     1235 <platform_findlib+0x95>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1040 <strcpy@plt>
movl   $0x1,-0x4(%rbp)
jmp    13af <platform_findlib+0x20f>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdd0(%rip),%rsi
call   10c0 <strtok@plt>
mov    %rax,-0x38(%rbp)
jmp    11fa <platform_findlib+0x5a>
lea    0xdbd(%rip),%rdi
lea    0xdc6(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    127c <platform_findlib+0xdc>
movl   $0x0,-0x4(%rbp)
jmp    13af <platform_findlib+0x20f>
jmp    1281 <platform_findlib+0xe1>
mov    -0x28(%rbp),%rdi
call   1090 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    129f <platform_findlib+0xff>
jmp    1333 <platform_findlib+0x193>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x1000,%esi
call   1080 <fgets@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movsbl (%rax),%edi
call   1070 <isspace@plt>
cmp    $0x0,%rax
je     1302 <platform_findlib+0x162>
mov    -0x40(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x40(%rbp)
movb   $0x0,(%rax)
jmp    12d5 <platform_findlib+0x135>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <findLibHelper@plt>
cmp    $0x0,%rax
je     132e <platform_findlib+0x18e>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    13af <platform_findlib+0x20f>
jmp    1281 <platform_findlib+0xe1>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rdi
lea    0xce2(%rip),%rsi
call   10a0 <findLibHelper@plt>
cmp    $0x0,%rax
je     1372 <platform_findlib+0x1d2>
mov    -0x18(%rbp),%rdi
lea    0xcc8(%rip),%rsi
call   1040 <strcpy@plt>
movl   $0x1,-0x4(%rbp)
jmp    13af <platform_findlib+0x20f>
mov    -0x10(%rbp),%rdi
lea    0xca8(%rip),%rsi
call   10a0 <findLibHelper@plt>
cmp    $0x0,%rax
je     13a8 <platform_findlib+0x208>
mov    -0x18(%rbp),%rdi
lea    0xc8e(%rip),%rsi
call   1040 <strcpy@plt>
movl   $0x1,-0x4(%rbp)
jmp    13af <platform_findlib+0x20f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<platform_findlib>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe4b(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     121d <platform_findlib+0x7d>
lea    0xe4a(%rip),%rsi
mov    %rax,%rdi
call   10c0 <strtok@plt>
test   %rax,%rax
je     121d <platform_findlib+0x7d>
mov    %rax,%rbp
mov    %r14,%rdi
mov    %rbx,%rsi
call   10a0 <findLibHelper@plt>
test   %rax,%rax
jne    1212 <platform_findlib+0x72>
lea    0xe23(%rip),%r15
nopl   (%rax)
xor    %edi,%edi
mov    %r15,%rsi
call   10c0 <strtok@plt>
test   %rax,%rax
je     121d <platform_findlib+0x7d>
mov    %rax,%rbp
mov    %r14,%rdi
mov    %rbx,%rsi
call   10a0 <findLibHelper@plt>
test   %rax,%rax
je     11f0 <platform_findlib+0x50>
mov    %rbx,%rdi
mov    %rbp,%rsi
jmp    1307 <platform_findlib+0x167>
lea    0xdee(%rip),%rdi
lea    0xdf7(%rip),%rsi
call   10b0 <fopen@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     1312 <platform_findlib+0x172>
mov    %rax,%r12
jmp    1260 <platform_findlib+0xc0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   10a0 <findLibHelper@plt>
test   %rax,%rax
jne    12df <platform_findlib+0x13f>
mov    %r12,%rdi
call   1090 <feof@plt>
test   %eax,%eax
jne    12ba <platform_findlib+0x11a>
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r12,%rdx
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
movslq %eax,%rbp
movsbl -0x1(%rbp,%rbx,1),%edi
call   1070 <isspace@plt>
test   %rax,%rax
je     1250 <platform_findlib+0xb0>
lea    (%rbx,%rbp,1),%rcx
add    $0xffffffffffffffff,%rcx
xchg   %ax,%ax
lea    -0x1(%rcx),%rbp
movb   $0x0,(%rcx)
movsbl -0x1(%rcx),%edi
call   1070 <isspace@plt>
mov    %rbp,%rcx
test   %rax,%rax
jne    12a0 <platform_findlib+0x100>
jmp    1250 <platform_findlib+0xb0>
mov    %r12,%rdi
call   1050 <fclose@plt>
lea    0xd60(%rip),%rsi
mov    %r14,%rdi
call   10a0 <findLibHelper@plt>
test   %rax,%rax
je     12e9 <platform_findlib+0x149>
lea    0xd4c(%rip),%rsi
jmp    1304 <platform_findlib+0x164>
mov    %r12,%rdi
call   1050 <fclose@plt>
jmp    130c <platform_findlib+0x16c>
lea    0xd35(%rip),%rsi
mov    %r14,%rdi
call   10a0 <findLibHelper@plt>
test   %rax,%rax
je     1312 <platform_findlib+0x172>
lea    0xd21(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"LD_LIBRARY_PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [16 x i8] c"/etc/ld.so.conf\00"
@.str.3 = [3 x i8] c"rt\00"
@.str.4 = [5 x i8] c"/lib\00"
@.str.5 = [9 x i8] c"/usr/lib\00"
define i32 @platform_findlib(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %9
%13 = load i8*, i8** %9
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i8*, i8** %9
%17 = call i8* @strtok(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %19
18: ; preds = %3
br label %19
19: ; preds = %18, %15
%20 = phi i8* [ %17, %15 ], [ null, %18 ]
store i8* %20, i8** %10
br label %21
21: ; preds = %33, %19
%22 = load i8*, i8** %10
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %35
24: ; preds = %21
%25 = load i8*, i8** %5
%26 = load i8*, i8** %6
%27 = call i64 @findLibHelper(i8* %25, i8* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = load i8*, i8** %6
%31 = load i8*, i8** %10
%32 = call i32 @strcpy(i8* %30, i8* %31)
store i32 1, i32* %4
br label %90
33: ; preds = %24
%34 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %34, i8** %10
br label %21
35: ; preds = %21
%36 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %36, i32** %8
%37 = load i32*, i32** %8
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 0, i32* %4
br label %90
40: ; preds = %35
br label %41
41: ; preds = %72, %40
%42 = load i32*, i32** %8
%43 = call i32 @feof(i32* %42)
%44 = icmp ne i32 %43, 0
%45 = xor i1 %44, true
br i1 %45, label %46, label %73
46: ; preds = %41
%47 = load i8*, i8** %6
%48 = load i32*, i32** %8
%49 = call i32 @fgets(i8* %47, i32 4096, i32* %48)
%50 = load i8*, i8** %6
%51 = load i8*, i8** %6
%52 = call i32 @strlen(i8* %51)
%53 = sub nsw i32 %52, 1
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %50, i64 %54
store i8* %55, i8** %11
br label %56
56: ; preds = %61, %46
%57 = load i8*, i8** %11
%58 = load i8, i8* %57
%59 = call i64 @isspace(i8 signext %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %56
%62 = load i8*, i8** %11
%63 = getelementptr inbounds i8, i8* %62, i32 -1
store i8* %63, i8** %11
store i8 0, i8* %62
br label %56
64: ; preds = %56
%65 = load i8*, i8** %5
%66 = load i8*, i8** %6
%67 = call i64 @findLibHelper(i8* %65, i8* %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %64
%70 = load i32*, i32** %8
%71 = call i32 @fclose(i32* %70)
store i32 1, i32* %4
br label %90
72: ; preds = %64
br label %41
73: ; preds = %41
%74 = load i32*, i32** %8
%75 = call i32 @fclose(i32* %74)
%76 = load i8*, i8** %5
%77 = call i64 @findLibHelper(i8* %76, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%78 = icmp ne i64 %77, 0
br i1 %78, label %79, label %82
79: ; preds = %73
%80 = load i8*, i8** %6
%81 = call i32 @strcpy(i8* %80, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %4
br label %90
82: ; preds = %73
%83 = load i8*, i8** %5
%84 = call i64 @findLibHelper(i8* %83, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%85 = icmp ne i64 %84, 0
br i1 %85, label %86, label %89
86: ; preds = %82
%87 = load i8*, i8** %6
%88 = call i32 @strcpy(i8* %87, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %4
br label %90
89: ; preds = %82
store i32 0, i32* %4
br label %90
90: ; preds = %89, %86, %79, %69, %39, %29
%91 = load i32, i32* %4
ret i32 %91
}

LLVM IR (-O2):
@.str = [16 x i8] c"LD_LIBRARY_PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [16 x i8] c"/etc/ld.so.conf\00"
@.str.3 = [3 x i8] c"rt\00"
@.str.4 = [5 x i8] c"/lib\00"
@.str.5 = [9 x i8] c"/usr/lib\00"
define i32 @platform_findlib(i8* %0, i8* %1, i32 %2) {
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %21, label %6
6: ; preds = %3
%7 = tail call i8* @strtok(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %21, label %9
9: ; preds = %6
%10 = tail call i64 @findLibHelper(i8* %0, i8* %1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %18, label %15
12: ; preds = %18
%13 = tail call i64 @findLibHelper(i8* %0, i8* %1)
%14 = icmp eq i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %12, %9
%16 = phi i8* [ %7, %9 ], [ %19, %12 ]
%17 = tail call i32 @strcpy(i8* %1, i8* %16)
br label %58
18: ; preds = %9, %12
%19 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %12
21: ; preds = %18, %3, %6
%22 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %58, label %24
24: ; preds = %21, %42
%25 = tail call i32 @feof(i32* %22)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %47
27: ; preds = %24
%28 = tail call i32 @fgets(i8* %1, i32 4096, i32* %22)
%29 = tail call i32 @strlen(i8* %1)
%30 = add nsw i32 %29, -1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %1, i64 %31
%33 = load i8, i8* %32
%34 = tail call i64 @isspace(i8 signext %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %42, label %36
36: ; preds = %27, %36
%37 = phi i8* [ %38, %36 ], [ %32, %27 ]
%38 = getelementptr inbounds i8, i8* %37, i64 -1
store i8 0, i8* %37
%39 = load i8, i8* %38
%40 = tail call i64 @isspace(i8 signext %39)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %36
42: ; preds = %36, %27
%43 = tail call i64 @findLibHelper(i8* %0, i8* %1)
%44 = icmp eq i64 %43, 0
br i1 %44, label %24, label %45
45: ; preds = %42
%46 = tail call i32 @fclose(i32* %22)
br label %58
47: ; preds = %24
%48 = tail call i32 @fclose(i32* %22)
%49 = tail call i64 @findLibHelper(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%50 = icmp eq i64 %49, 0
br i1 %50, label %53, label %51
51: ; preds = %47
%52 = tail call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %58
53: ; preds = %47
%54 = tail call i64 @findLibHelper(i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%55 = icmp eq i64 %54, 0
br i1 %55, label %58, label %56
56: ; preds = %53
%57 = tail call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %58
58: ; preds = %45, %53, %21, %56, %51, %15
%59 = phi i32 [ 1, %15 ], [ 1, %45 ], [ 1, %51 ], [ 1, %56 ], [ 0, %21 ], [ 0, %53 ]
ret i32 %59
}