; LLVM IR from Aeternum-Veritas/Merry :: merry (merry_dynl)

; 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.MerryDynLoader = type { ptr, i64, i64 }
%struct.MerryDynEntry = type { [255 x i8], ptr, i8 }

@stdout = external global ptr, align 8
@loader = external hidden global %struct.MerryDynLoader, align 8
@.str.3 = external hidden unnamed_addr constant [58 x i8], align 1

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #0

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

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

; Function Attrs: nounwind uwtable
define dso_local void @merry_loader_close() #2 {
  %1 = alloca i64, align 8
  %2 = load ptr, ptr @stdout, align 8, !tbaa !5
  %3 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %2, ptr noundef @.str.3, ptr noundef null) #3
  %4 = load i64, ptr getelementptr inbounds (%struct.MerryDynLoader, ptr @loader, i32 0, i32 2), align 8, !tbaa !9
  %5 = load i64, ptr getelementptr inbounds (%struct.MerryDynLoader, ptr @loader, i32 0, i32 1), align 8, !tbaa !12
  %6 = icmp ne i64 %4, %5
  br i1 %6, label %7, label %33

7:                                                ; preds = %0
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  store i64 0, ptr %1, align 8, !tbaa !13
  br label %8

8:                                                ; preds = %29, %7
  %9 = load i64, ptr %1, align 8, !tbaa !13
  %10 = load i64, ptr getelementptr inbounds (%struct.MerryDynLoader, ptr @loader, i32 0, i32 1), align 8, !tbaa !12
  %11 = icmp ult i64 %9, %10
  br i1 %11, label %13, label %12

12:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  br label %32

13:                                               ; preds = %8
  %14 = load ptr, ptr @loader, align 8, !tbaa !14
  %15 = load i64, ptr %1, align 8, !tbaa !13
  %16 = getelementptr inbounds %struct.MerryDynEntry, ptr %14, i64 %15
  %17 = getelementptr inbounds %struct.MerryDynEntry, ptr %16, i32 0, i32 2
  %18 = load i8, ptr %17, align 8, !tbaa !15
  %19 = zext i8 %18 to i32
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %28

21:                                               ; preds = %13
  %22 = load ptr, ptr @loader, align 8, !tbaa !14
  %23 = load i64, ptr %1, align 8, !tbaa !13
  %24 = getelementptr inbounds %struct.MerryDynEntry, ptr %22, i64 %23
  %25 = getelementptr inbounds %struct.MerryDynEntry, ptr %24, i32 0, i32 1
  %26 = load ptr, ptr %25, align 8, !tbaa !17
  %27 = call i32 @dlclose(ptr noundef %26) #3
  br label %28

28:                                               ; preds = %21, %13
  br label %29

29:                                               ; preds = %28
  %30 = load i64, ptr %1, align 8, !tbaa !13
  %31 = add i64 %30, 1
  store i64 %31, ptr %1, align 8, !tbaa !13
  br label %8, !llvm.loop !18

32:                                               ; preds = %12
  br label %33

33:                                               ; preds = %32, %0
  %34 = load ptr, ptr @loader, align 8, !tbaa !14
  %35 = icmp ne ptr %34, null
  br i1 %35, label %36, label %38

36:                                               ; preds = %33
  %37 = load ptr, ptr @loader, align 8, !tbaa !14
  call void @free(ptr noundef %37) #3
  br label %38

38:                                               ; preds = %36, %33
  ret void
}

; Function Attrs: nounwind
declare i32 @dlclose(ptr noundef) #0

; Function Attrs: nounwind
declare void @free(ptr noundef) #0

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind 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 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 16}
!10 = !{!"MerryDynLoader", !6, i64 0, !11, i64 8, !11, i64 16}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !6, i64 0}
!15 = !{!16, !7, i64 264}
!16 = !{!"MerryDynEntry", !7, i64 0, !6, i64 256, !7, i64 264}
!17 = !{!16, !6, i64 256}
!18 = distinct !{!18, !19}
!19 = !{!"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.MerryDynLoader = type { ptr, i64, i64 }
%struct.MerryDynEntry = type { [255 x i8], ptr, i8 }

@stdout = external local_unnamed_addr global ptr, align 8
@loader = external hidden local_unnamed_addr global %struct.MerryDynLoader, align 8
@.str.3 = external hidden unnamed_addr constant [58 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @merry_loader_close() local_unnamed_addr #1 {
  %1 = load ptr, ptr @stdout, align 8, !tbaa !5
  %2 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %1, ptr noundef nonnull @.str.3, ptr noundef null) #4
  %3 = load i64, ptr getelementptr inbounds (i8, ptr @loader, i64 16), align 8, !tbaa !9
  %4 = load i64, ptr getelementptr inbounds (i8, ptr @loader, i64 8), align 8
  %.not = icmp ne i64 %3, %4
  %5 = icmp ne i64 %4, 0
  %or.cond = and i1 %.not, %5
  %.pre10 = load ptr, ptr @loader, align 8, !tbaa !12
  br i1 %or.cond, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %0, %16
  %6 = phi i64 [ %17, %16 ], [ %4, %0 ]
  %7 = phi ptr [ %18, %16 ], [ %.pre10, %0 ]
  %.06 = phi i64 [ %19, %16 ], [ 0, %0 ]
  %8 = getelementptr inbounds %struct.MerryDynEntry, ptr %7, i64 %.06
  %9 = getelementptr inbounds i8, ptr %8, i64 264
  %10 = load i8, ptr %9, align 8, !tbaa !13
  %11 = icmp eq i8 %10, 0
  br i1 %11, label %12, label %16

12:                                               ; preds = %.lr.ph
  %13 = getelementptr inbounds i8, ptr %8, i64 256
  %14 = load ptr, ptr %13, align 8, !tbaa !15
  %15 = tail call i32 @dlclose(ptr noundef %14) #4
  %.pre = load ptr, ptr @loader, align 8, !tbaa !12
  %.pre9 = load i64, ptr getelementptr inbounds (i8, ptr @loader, i64 8), align 8, !tbaa !16
  br label %16

16:                                               ; preds = %.lr.ph, %12
  %17 = phi i64 [ %6, %.lr.ph ], [ %.pre9, %12 ]
  %18 = phi ptr [ %7, %.lr.ph ], [ %.pre, %12 ]
  %19 = add nuw i64 %.06, 1
  %20 = icmp ult i64 %19, %17
  br i1 %20, label %.lr.ph, label %.loopexit, !llvm.loop !17

.loopexit:                                        ; preds = %16, %0
  %21 = phi ptr [ %.pre10, %0 ], [ %18, %16 ]
  %.not5 = icmp eq ptr %21, null
  br i1 %.not5, label %23, label %22

22:                                               ; preds = %.loopexit
  tail call void @free(ptr noundef nonnull %21) #4
  br label %23

23:                                               ; preds = %22, %.loopexit
  ret void
}

; Function Attrs: nounwind
declare i32 @dlclose(ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #3

attributes #0 = { nofree 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 #1 = { nounwind 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 "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 nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 }

!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/C++ TBAA"}
!9 = !{!10, !11, i64 16}
!10 = !{!"MerryDynLoader", !6, i64 0, !11, i64 8, !11, i64 16}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !6, i64 0}
!13 = !{!14, !7, i64 264}
!14 = !{!"MerryDynEntry", !7, i64 0, !6, i64 256, !7, i64 264}
!15 = !{!14, !6, i64 256}
!16 = !{!10, !11, i64 8}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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