; LLVM IR from aap/librw :: src/image.ll (ZN2rw5Image13setSearchPathEPKc)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%"struct.rw::MemoryFunctions" = type { ptr, ptr, ptr, ptr, ptr }
%"struct.rw::ImageGlobals" = type { ptr, i32, [10 x %"struct.rw::FileAssociation"], i32 }
%"struct.rw::FileAssociation" = type { ptr, ptr, ptr }

@_ZN2rw17imageModuleOffsetE = external dso_local global i32, align 4
@_ZN2rw6Engine8memfuncsE = external global %"struct.rw::MemoryFunctions", align 8
@_ZN2rw6engineE = external global ptr, align 8
@.str.13 = external hidden unnamed_addr constant [73 x i8], align 1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN2rw5Image13setSearchPathEPKc(ptr noundef %0) #1 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %7 = load ptr, ptr @_ZN2rw6engineE, align 8, !tbaa !5
  %8 = load i32, ptr @_ZN2rw17imageModuleOffsetE, align 4, !tbaa !9
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds i8, ptr %7, i64 %9
  store ptr %10, ptr %5, align 8, !tbaa !5
  %11 = load ptr, ptr getelementptr inbounds (%"struct.rw::MemoryFunctions", ptr @_ZN2rw6Engine8memfuncsE, i32 0, i32 2), align 8, !tbaa !11
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = getelementptr inbounds %"struct.rw::ImageGlobals", ptr %12, i32 0, i32 0
  %14 = load ptr, ptr %13, align 8, !tbaa !13
  call void %11(ptr noundef %14)
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = getelementptr inbounds %"struct.rw::ImageGlobals", ptr %15, i32 0, i32 1
  store i32 0, ptr %16, align 8, !tbaa !15
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %24

19:                                               ; preds = %1
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  %21 = call noundef ptr @_ZN2rw10strdup_LOCEPKcjS1_(ptr noundef %20, i32 noundef 196608, ptr noundef @.str.13)
  store ptr %21, ptr %3, align 8, !tbaa !5
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %"struct.rw::ImageGlobals", ptr %22, i32 0, i32 0
  store ptr %21, ptr %23, align 8, !tbaa !13
  br label %27

24:                                               ; preds = %1
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds %"struct.rw::ImageGlobals", ptr %25, i32 0, i32 0
  store ptr null, ptr %26, align 8, !tbaa !13
  store i32 1, ptr %6, align 4
  br label %52

27:                                               ; preds = %19
  br label %28

28:                                               ; preds = %45, %27
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = icmp ne ptr %29, null
  br i1 %30, label %31, label %35

31:                                               ; preds = %28
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = load i8, ptr %32, align 1, !tbaa !16
  %34 = icmp ne i8 %33, 0
  br label %35

35:                                               ; preds = %31, %28
  %36 = phi i1 [ false, %28 ], [ %34, %31 ]
  br i1 %36, label %37, label %51

37:                                               ; preds = %35
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = call noundef ptr @strchr(ptr noundef %38, i32 noundef 59) #5
  store ptr %39, ptr %4, align 8, !tbaa !5
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = icmp ne ptr %40, null
  br i1 %41, label %42, label %45

42:                                               ; preds = %37
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds i8, ptr %43, i32 1
  store ptr %44, ptr %4, align 8, !tbaa !5
  store i8 0, ptr %43, align 1, !tbaa !16
  br label %45

45:                                               ; preds = %42, %37
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = getelementptr inbounds %"struct.rw::ImageGlobals", ptr %46, i32 0, i32 1
  %48 = load i32, ptr %47, align 8, !tbaa !15
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %47, align 8, !tbaa !15
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %50, ptr %3, align 8, !tbaa !5
  br label %28, !llvm.loop !17

51:                                               ; preds = %35
  store i32 0, ptr %6, align 4
  br label %52

52:                                               ; preds = %51, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  %53 = load i32, ptr %6, align 4
  switch i32 %53, label %55 [
    i32 0, label %54
    i32 1, label %54
  ]

54:                                               ; preds = %52, %52
  ret void

55:                                               ; preds = %52
  unreachable
}

declare noundef ptr @_ZN2rw10strdup_LOCEPKcjS1_(ptr noundef, i32 noundef, ptr noundef) #2

; Function Attrs: nounwind willreturn memory(read)
declare noundef ptr @strchr(ptr noundef, i32 noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { nounwind willreturn memory(read) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { nounwind }
attributes #5 = { nounwind willreturn memory(read) }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 16}
!12 = !{!"_ZTSN2rw15MemoryFunctionsE", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!13 = !{!14, !6, i64 0}
!14 = !{!"_ZTSN2rw12ImageGlobalsE", !6, i64 0, !10, i64 8, !7, i64 16, !10, i64 256}
!15 = !{!14, !10, i64 8}
!16 = !{!7, !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%"struct.rw::MemoryFunctions" = type { ptr, ptr, ptr, ptr, ptr }

@_ZN2rw17imageModuleOffsetE = external dso_local local_unnamed_addr global i32, align 4
@_ZN2rw6Engine8memfuncsE = external local_unnamed_addr global %"struct.rw::MemoryFunctions", align 8
@_ZN2rw6engineE = external local_unnamed_addr global ptr, align 8
@.str.13 = external hidden unnamed_addr constant [73 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN2rw5Image13setSearchPathEPKc(ptr noundef %0) local_unnamed_addr #0 align 2 {
  %2 = load ptr, ptr @_ZN2rw6engineE, align 8, !tbaa !5
  %3 = load i32, ptr @_ZN2rw17imageModuleOffsetE, align 4, !tbaa !9
  %4 = sext i32 %3 to i64
  %5 = getelementptr inbounds i8, ptr %2, i64 %4
  %6 = load ptr, ptr getelementptr inbounds (i8, ptr @_ZN2rw6Engine8memfuncsE, i64 16), align 8, !tbaa !11
  %7 = load ptr, ptr %5, align 8, !tbaa !13
  tail call void %6(ptr noundef %7)
  %8 = getelementptr inbounds i8, ptr %5, i64 8
  store i32 0, ptr %8, align 8, !tbaa !15
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %12, label %9

9:                                                ; preds = %1
  %10 = tail call noundef ptr @_ZN2rw10strdup_LOCEPKcjS1_(ptr noundef nonnull %0, i32 noundef 196608, ptr noundef nonnull @.str.13)
  store ptr %10, ptr %5, align 8, !tbaa !13
  %.not1619 = icmp eq ptr %10, null
  br i1 %.not1619, label %.critedge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %9
  %11 = load i8, ptr %10, align 1, !tbaa !16
  %.not1724 = icmp eq i8 %11, 0
  br i1 %.not1724, label %.critedge, label %.lr.ph26

12:                                               ; preds = %1
  store ptr null, ptr %5, align 8, !tbaa !13
  br label %.critedge

.lr.ph26:                                         ; preds = %.lr.ph.preheader, %.lr.ph
  %.02025 = phi ptr [ %16, %.lr.ph ], [ %10, %.lr.ph.preheader ]
  %13 = tail call noundef ptr @strchr(ptr noundef nonnull dereferenceable(1) %.02025, i32 noundef 59) #3
  %.not18 = icmp eq ptr %13, null
  br i1 %.not18, label %.thread, label %.lr.ph

.thread:                                          ; preds = %.lr.ph26
  %14 = load i32, ptr %8, align 8, !tbaa !15
  %15 = add nsw i32 %14, 1
  store i32 %15, ptr %8, align 8, !tbaa !15
  br label %.critedge

.lr.ph:                                           ; preds = %.lr.ph26
  %16 = getelementptr inbounds i8, ptr %13, i64 1
  store i8 0, ptr %13, align 1, !tbaa !16
  %17 = load i32, ptr %8, align 8, !tbaa !15
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr %8, align 8, !tbaa !15
  %19 = load i8, ptr %16, align 1, !tbaa !16
  %.not17 = icmp eq i8 %19, 0
  br i1 %.not17, label %.critedge, label %.lr.ph26, !llvm.loop !17

.critedge:                                        ; preds = %.lr.ph, %.lr.ph.preheader, %.thread, %9, %12
  ret void
}

declare noundef ptr @_ZN2rw10strdup_LOCEPKcjS1_(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare noundef ptr @strchr(ptr noundef, i32 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { mustprogress nofree nounwind willreturn memory(argmem: read) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { nounwind willreturn memory(read) }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 16}
!12 = !{!"_ZTSN2rw15MemoryFunctionsE", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!13 = !{!14, !6, i64 0}
!14 = !{!"_ZTSN2rw12ImageGlobalsE", !6, i64 0, !10, i64 8, !7, i64 16, !10, i64 256}
!15 = !{!14, !10, i64 8}
!16 = !{!7, !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass