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

; 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.19 = 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: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZN2rw14mustmalloc_LOCEmjPKc(i64 noundef, i32 noundef, ptr noundef) #1

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #2

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_ZN2rw5Image11getFilenameEPKc(ptr noundef) #3 align 2

; Function Attrs: nounwind
declare ptr @strcpy(ptr noundef, ptr noundef) #4

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN2rw5Image4readEPKc(ptr noundef %0) #3 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = call i64 @strlen(ptr noundef %10) #6
  %12 = add i64 %11, 20
  %13 = mul i64 %12, 1
  %14 = call noundef ptr @_ZN2rw14mustmalloc_LOCEmjPKc(i64 noundef %13, i32 noundef 65560, ptr noundef @.str.19)
  store ptr %14, ptr %5, align 8, !tbaa !5
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = call ptr @strcpy(ptr noundef %15, ptr noundef %16) #5
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = call i64 @strlen(ptr noundef %19) #6
  %21 = getelementptr inbounds i8, ptr %18, i64 %20
  store ptr %21, ptr %6, align 8, !tbaa !5
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %22, i32 1
  store ptr %23, ptr %6, align 8, !tbaa !5
  store i8 46, ptr %22, align 1, !tbaa !9
  store i32 0, ptr %4, align 4, !tbaa !10
  br label %24

24:                                               ; preds = %86, %1
  %25 = load i32, ptr %4, align 4, !tbaa !10
  %26 = load ptr, ptr @_ZN2rw6engineE, align 8, !tbaa !5
  %27 = load i32, ptr @_ZN2rw17imageModuleOffsetE, align 4, !tbaa !10
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds i8, ptr %26, i64 %28
  %30 = getelementptr inbounds %"struct.rw::ImageGlobals", ptr %29, i32 0, i32 3
  %31 = load i32, ptr %30, align 8, !tbaa !12
  %32 = icmp slt i32 %25, %31
  br i1 %32, label %33, label %89

33:                                               ; preds = %24
  %34 = load ptr, ptr @_ZN2rw6engineE, align 8, !tbaa !5
  %35 = load i32, ptr @_ZN2rw17imageModuleOffsetE, align 4, !tbaa !10
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds i8, ptr %34, i64 %36
  %38 = getelementptr inbounds %"struct.rw::ImageGlobals", ptr %37, i32 0, i32 2
  %39 = load i32, ptr %4, align 4, !tbaa !10
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [10 x %"struct.rw::FileAssociation"], ptr %38, i64 0, i64 %40
  %42 = getelementptr inbounds %"struct.rw::FileAssociation", ptr %41, i32 0, i32 1
  %43 = load ptr, ptr %42, align 8, !tbaa !14
  %44 = icmp eq ptr %43, null
  br i1 %44, label %45, label %46

45:                                               ; preds = %33
  br label %86

46:                                               ; preds = %33
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = load ptr, ptr @_ZN2rw6engineE, align 8, !tbaa !5
  %49 = load i32, ptr @_ZN2rw17imageModuleOffsetE, align 4, !tbaa !10
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds i8, ptr %48, i64 %50
  %52 = getelementptr inbounds %"struct.rw::ImageGlobals", ptr %51, i32 0, i32 2
  %53 = load i32, ptr %4, align 4, !tbaa !10
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [10 x %"struct.rw::FileAssociation"], ptr %52, i64 0, i64 %54
  %56 = getelementptr inbounds %"struct.rw::FileAssociation", ptr %55, i32 0, i32 0
  %57 = load ptr, ptr %56, align 8, !tbaa !16
  %58 = call ptr @strncpy(ptr noundef %47, ptr noundef %57, i64 noundef 19) #5
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  %60 = call noundef ptr @_ZN2rw5Image11getFilenameEPKc(ptr noundef %59)
  store ptr %60, ptr %7, align 8, !tbaa !5
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  %62 = icmp ne ptr %61, null
  br i1 %62, label %63, label %85

63:                                               ; preds = %46
  %64 = load ptr, ptr @_ZN2rw6engineE, align 8, !tbaa !5
  %65 = load i32, ptr @_ZN2rw17imageModuleOffsetE, align 4, !tbaa !10
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds i8, ptr %64, i64 %66
  %68 = getelementptr inbounds %"struct.rw::ImageGlobals", ptr %67, i32 0, i32 2
  %69 = load i32, ptr %4, align 4, !tbaa !10
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [10 x %"struct.rw::FileAssociation"], ptr %68, i64 0, i64 %70
  %72 = getelementptr inbounds %"struct.rw::FileAssociation", ptr %71, i32 0, i32 1
  %73 = load ptr, ptr %72, align 8, !tbaa !14
  %74 = load ptr, ptr %7, align 8, !tbaa !5
  %75 = call noundef ptr %73(ptr noundef %74)
  store ptr %75, ptr %8, align 8, !tbaa !5
  %76 = load ptr, ptr getelementptr inbounds (%"struct.rw::MemoryFunctions", ptr @_ZN2rw6Engine8memfuncsE, i32 0, i32 2), align 8, !tbaa !17
  %77 = load ptr, ptr %7, align 8, !tbaa !5
  call void %76(ptr noundef %77)
  %78 = load ptr, ptr %8, align 8, !tbaa !5
  %79 = icmp ne ptr %78, null
  br i1 %79, label %80, label %84

80:                                               ; preds = %63
  %81 = load ptr, ptr getelementptr inbounds (%"struct.rw::MemoryFunctions", ptr @_ZN2rw6Engine8memfuncsE, i32 0, i32 2), align 8, !tbaa !17
  %82 = load ptr, ptr %5, align 8, !tbaa !5
  call void %81(ptr noundef %82)
  %83 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %83, ptr %2, align 8
  store i32 1, ptr %9, align 4
  br label %92

84:                                               ; preds = %63
  br label %85

85:                                               ; preds = %84, %46
  br label %86

86:                                               ; preds = %85, %45
  %87 = load i32, ptr %4, align 4, !tbaa !10
  %88 = add nsw i32 %87, 1
  store i32 %88, ptr %4, align 4, !tbaa !10
  br label %24, !llvm.loop !19

89:                                               ; preds = %24
  %90 = load ptr, ptr getelementptr inbounds (%"struct.rw::MemoryFunctions", ptr @_ZN2rw6Engine8memfuncsE, i32 0, i32 2), align 8, !tbaa !17
  %91 = load ptr, ptr %5, align 8, !tbaa !5
  call void %90(ptr noundef %91)
  store ptr null, ptr %2, align 8
  store i32 1, ptr %9, align 4
  br label %92

92:                                               ; preds = %89, %80
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  %93 = load ptr, ptr %2, align 8
  ret ptr %93
}

; Function Attrs: nounwind
declare ptr @strncpy(ptr noundef, ptr noundef, i64 noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint 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 = { 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 #3 = { 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 #4 = { nounwind "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 #5 = { nounwind }
attributes #6 = { 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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !11, i64 256}
!13 = !{!"_ZTSN2rw12ImageGlobalsE", !6, i64 0, !11, i64 8, !7, i64 16, !11, i64 256}
!14 = !{!15, !6, i64 8}
!15 = !{!"_ZTSN2rw15FileAssociationE", !6, i64 0, !6, i64 8, !6, i64 16}
!16 = !{!15, !6, i64 0}
!17 = !{!18, !6, i64 16}
!18 = !{!"_ZTSN2rw15MemoryFunctionsE", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!19 = distinct !{!19, !20}
!20 = !{!"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 }
%"struct.rw::FileAssociation" = type { 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.19 = external hidden unnamed_addr constant [73 x i8], align 1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZN2rw14mustmalloc_LOCEmjPKc(i64 noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_ZN2rw5Image11getFilenameEPKc(ptr noundef) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite)
declare ptr @strcpy(ptr noalias noundef returned writeonly, ptr noalias nocapture noundef readonly) local_unnamed_addr #3

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN2rw5Image4readEPKc(ptr nocapture noundef readonly %0) local_unnamed_addr #2 align 2 {
  %2 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #4
  %3 = add i64 %2, 20
  %4 = tail call noundef ptr @_ZN2rw14mustmalloc_LOCEmjPKc(i64 noundef %3, i32 noundef 65560, ptr noundef nonnull @.str.19)
  %5 = tail call ptr @strcpy(ptr noundef nonnull dereferenceable(1) %4, ptr noundef nonnull dereferenceable(1) %0) #5
  %6 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %4) #4
  %7 = getelementptr inbounds i8, ptr %4, i64 %6
  %8 = getelementptr inbounds i8, ptr %7, i64 1
  store i8 46, ptr %7, align 1, !tbaa !5
  %9 = load ptr, ptr @_ZN2rw6engineE, align 8, !tbaa !8
  %10 = load i32, ptr @_ZN2rw17imageModuleOffsetE, align 4, !tbaa !10
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds i8, ptr %9, i64 %11
  %13 = getelementptr inbounds i8, ptr %12, i64 256
  %14 = load i32, ptr %13, align 8, !tbaa !12
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1, %36
  %16 = phi i32 [ %37, %36 ], [ %10, %1 ]
  %17 = phi ptr [ %38, %36 ], [ %9, %1 ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %36 ], [ 0, %1 ]
  %18 = phi ptr [ %40, %36 ], [ %12, %1 ]
  %19 = getelementptr inbounds i8, ptr %18, i64 16
  %20 = getelementptr inbounds [10 x %"struct.rw::FileAssociation"], ptr %19, i64 0, i64 %indvars.iv
  %21 = getelementptr inbounds i8, ptr %20, i64 8
  %22 = load ptr, ptr %21, align 8, !tbaa !14
  %23 = icmp eq ptr %22, null
  br i1 %23, label %36, label %24

24:                                               ; preds = %.lr.ph
  %25 = load ptr, ptr %20, align 8, !tbaa !16
  %26 = tail call ptr @strncpy(ptr noundef nonnull dereferenceable(1) %8, ptr noundef nonnull dereferenceable(1) %25, i64 noundef 19) #5
  %27 = tail call noundef ptr @_ZN2rw5Image11getFilenameEPKc(ptr noundef %4)
  %.not = icmp eq ptr %27, null
  %.pre27 = load ptr, ptr @_ZN2rw6engineE, align 8, !tbaa !8
  %.pre29 = load i32, ptr @_ZN2rw17imageModuleOffsetE, align 4, !tbaa !10
  br i1 %.not, label %36, label %28

28:                                               ; preds = %24
  %29 = sext i32 %.pre29 to i64
  %30 = getelementptr inbounds i8, ptr %.pre27, i64 %29
  %31 = getelementptr inbounds i8, ptr %30, i64 16
  %32 = getelementptr inbounds [10 x %"struct.rw::FileAssociation"], ptr %31, i64 0, i64 %indvars.iv, i32 1
  %33 = load ptr, ptr %32, align 8, !tbaa !14
  %34 = tail call noundef ptr %33(ptr noundef nonnull %27)
  %35 = load ptr, ptr getelementptr inbounds (i8, ptr @_ZN2rw6Engine8memfuncsE, i64 16), align 8, !tbaa !17
  tail call void %35(ptr noundef nonnull %27)
  %.not23 = icmp eq ptr %34, null
  br i1 %.not23, label %._crit_edge26, label %._crit_edge

._crit_edge26:                                    ; preds = %28
  %.pre = load ptr, ptr @_ZN2rw6engineE, align 8, !tbaa !8
  %.pre28 = load i32, ptr @_ZN2rw17imageModuleOffsetE, align 4, !tbaa !10
  br label %36

36:                                               ; preds = %._crit_edge26, %24, %.lr.ph
  %37 = phi i32 [ %.pre28, %._crit_edge26 ], [ %.pre29, %24 ], [ %16, %.lr.ph ]
  %38 = phi ptr [ %.pre, %._crit_edge26 ], [ %.pre27, %24 ], [ %17, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %39 = sext i32 %37 to i64
  %40 = getelementptr inbounds i8, ptr %38, i64 %39
  %41 = getelementptr inbounds i8, ptr %40, i64 256
  %42 = load i32, ptr %41, align 8, !tbaa !12
  %43 = sext i32 %42 to i64
  %44 = icmp slt i64 %indvars.iv.next, %43
  br i1 %44, label %.lr.ph, label %._crit_edge, !llvm.loop !19

._crit_edge:                                      ; preds = %36, %28, %1
  %.0 = phi ptr [ null, %1 ], [ %34, %28 ], [ null, %36 ]
  %45 = load ptr, ptr getelementptr inbounds (i8, ptr @_ZN2rw6Engine8memfuncsE, i64 16), align 8, !tbaa !17
  tail call void %45(ptr noundef %4)
  ret ptr %.0
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite)
declare ptr @strncpy(ptr noalias noundef returned writeonly, ptr noalias nocapture noundef readonly, i64 noundef) local_unnamed_addr #3

attributes #0 = { inlinehint 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 = { 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 #2 = { 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 #3 = { mustprogress nofree nounwind willreturn memory(argmem: readwrite) "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 willreturn memory(read) }
attributes #5 = { nounwind }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !6, i64 0}
!12 = !{!13, !11, i64 256}
!13 = !{!"_ZTSN2rw12ImageGlobalsE", !9, i64 0, !11, i64 8, !6, i64 16, !11, i64 256}
!14 = !{!15, !9, i64 8}
!15 = !{!"_ZTSN2rw15FileAssociationE", !9, i64 0, !9, i64 8, !9, i64 16}
!16 = !{!15, !9, i64 0}
!17 = !{!18, !9, i64 16}
!18 = !{!"_ZTSN2rw15MemoryFunctionsE", !9, i64 0, !9, i64 8, !9, i64 16, !9, i64 24, !9, i64 32}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass