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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *getenv(char *);
scalar_t__ is_usable_library(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
char *strtok(char *, char *);
int find_xlib_by_env(char *library, uint16_t desired_architecture) {
  char *ld_env = getenv("LD_LIBRARY_PATH");
  if (ld_env == 0) {
    return 0;
  }
  char *ld_to_parse = strdup(ld_env);
  int found_library = 0;
  char *t = strtok(ld_to_parse, ":");
  char potential_library[(1024) + 1];
  while ((t != NULL) && (!found_library)) {
    snprintf(potential_library, (1024), "%s/libX11.so.6", t);
    if (is_usable_library(potential_library, desired_architecture)) {
      strcpy(library, potential_library);
      found_library = 1;
    }
    t = strtok(NULL, ":");
  }
  free(ld_to_parse);
  return found_library;
}


x86 assembly (clang, -O0):
<find_xlib_by_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xe77(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a9 <find_xlib_by_env+0x39>
movl   $0x0,-0x4(%rbp)
jmp    127a <find_xlib_by_env+0x10a>
mov    -0x20(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe48(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x38(%rbp)
mov    %al,-0x441(%rbp)
je     11f3 <find_xlib_by_env+0x83>
cmpl   $0x0,-0x2c(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x441(%rbp)
mov    -0x441(%rbp),%al
test   $0x1,%al
jne    1206 <find_xlib_by_env+0x96>
jmp    126b <find_xlib_by_env+0xfb>
lea    -0x440(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x400,%esi
lea    0xdf5(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x440(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1080 <is_usable_library@plt>
cmp    $0x0,%rax
je     1252 <find_xlib_by_env+0xe2>
mov    -0x10(%rbp),%rdi
lea    -0x440(%rbp),%rsi
call   1050 <strcpy@plt>
movl   $0x1,-0x2c(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xdb3(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x38(%rbp)
jmp    11d1 <find_xlib_by_env+0x61>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_xlib_by_env>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xe73(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     121e <find_xlib_by_env+0xae>
mov    %rax,%rdi
call   1090 <strdup@plt>
mov    %rax,%rbx
lea    0xe63(%rip),%rsi
mov    %rax,%rdi
call   1070 <strtok@plt>
test   %rax,%rax
mov    %rbx,0x8(%rsp)
je     1223 <find_xlib_by_env+0xb3>
mov    %r14,(%rsp)
lea    0xe48(%rip),%r12
lea    0x10(%rsp),%rbx
lea    0xe3a(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r12,%rdx
mov    %rax,%rcx
call   1060 <snprintf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <is_usable_library@plt>
test   %rax,%rax
jne    1228 <find_xlib_by_env+0xb8>
mov    %rax,%r15
xor    %r14d,%r14d
xor    %edi,%edi
mov    %r13,%rsi
call   1070 <strtok@plt>
test   %r15,%r15
jne    124a <find_xlib_by_env+0xda>
test   %rax,%rax
jne    11e0 <find_xlib_by_env+0x70>
jmp    124a <find_xlib_by_env+0xda>
xor    %r14d,%r14d
jmp    1254 <find_xlib_by_env+0xe4>
xor    %r14d,%r14d
jmp    124a <find_xlib_by_env+0xda>
lea    0x10(%rsp),%rsi
mov    (%rsp),%rdi
call   1050 <strcpy@plt>
lea    0xdd3(%rip),%rsi
xor    %edi,%edi
call   1070 <strtok@plt>
mov    $0x1,%r14d
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
mov    %r14d,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
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 = [15 x i8] c"%s/libX11.so.6\00"
define i32 @find_xlib_by_env(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca [1025 x i8]
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 0, i32* %3
br label %47
15: ; preds = %2
%16 = load i8*, i8** %6
%17 = call i8* @strdup(i8* %16)
store i8* %17, i8** %7
store i32 0, i32* %8
%18 = load i8*, i8** %7
%19 = call i8* @strtok(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %19, i8** %9
br label %20
20: ; preds = %41, %15
%21 = load i8*, i8** %9
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32, i32* %8
%25 = icmp ne i32 %24, 0
%26 = xor i1 %25, true
br label %27
27: ; preds = %23, %20
%28 = phi i1 [ false, %20 ], [ %26, %23 ]
br i1 %28, label %29, label %43
29: ; preds = %27
%30 = getelementptr inbounds [1025 x i8], [1025 x i8]* %10, i64 0, i64 0
%31 = load i8*, i8** %9
%32 = call i32 @snprintf(i8* %30, i32 1024, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = getelementptr inbounds [1025 x i8], [1025 x i8]* %10, i64 0, i64 0
%34 = load i32, i32* %5
%35 = call i64 @is_usable_library(i8* %33, i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %29
%38 = load i8*, i8** %4
%39 = getelementptr inbounds [1025 x i8], [1025 x i8]* %10, i64 0, i64 0
%40 = call i32 @strcpy(i8* %38, i8* %39)
store i32 1, i32* %8
br label %41
41: ; preds = %37, %29
%42 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %42, i8** %9
br label %20
43: ; preds = %27
%44 = load i8*, i8** %7
%45 = call i32 @free(i8* %44)
%46 = load i32, i32* %8
store i32 %46, i32* %3
br label %47
47: ; preds = %43, %14
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@.str = [16 x i8] c"LD_LIBRARY_PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [15 x i8] c"%s/libX11.so.6\00"
define i32 @find_xlib_by_env(i8* %0, i32 %1) {
%3 = alloca [1025 x i8]
%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 %26, label %6
6: ; preds = %2
%7 = tail call i8* @strdup(i8* %4)
%8 = tail call i8* @strtok(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 0
%10 = icmp eq i8* %8, null
br i1 %10, label %23, label %11
11: ; preds = %6, %19
%12 = phi i8* [ %20, %19 ], [ %8, %6 ]
%13 = call i32 @snprintf(i8* %9, i32 1024, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%14 = call i64 @is_usable_library(i8* %9, i32 %1)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = call i32 @strcpy(i8* %0, i8* %9)
%18 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %23
19: ; preds = %11
%20 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp ne i8* %20, null
%22 = and i1 %15, %21
br i1 %22, label %11, label %23
23: ; preds = %19, %16, %6
%24 = phi i32 [ 0, %6 ], [ 1, %16 ], [ 0, %19 ]
%25 = call i32 @free(i8* %7)
br label %26
26: ; preds = %2, %23
%27 = phi i32 [ %24, %23 ], [ 0, %2 ]
ret i32 %27
}