; LLVM IR from seppo0010/rlite :: src (rlite)

; 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.rlite = type { i64, i64, i32, ptr, i64, i64, i64, ptr, i32, i32, i32, i32, ptr, i64, i64, ptr, i64, i64, ptr, ptr, ptr, ptr }
%struct.rl_file_driver = type { ptr, ptr, i32 }
%struct.rl_memory_driver = type { ptr, i64 }

; 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: nounwind
declare void @free(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @rl_close(ptr noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = icmp ne ptr %6, null
  br i1 %7, label %9, label %8

8:                                                ; preds = %1
  store i32 0, ptr %2, align 4
  br label %77

9:                                                ; preds = %1
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.rlite, ptr %10, i32 0, i32 8
  %12 = load i32, ptr %11, align 8, !tbaa !9
  %13 = icmp eq i32 %12, 1
  br i1 %13, label %14, label %17

14:                                               ; preds = %9
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = call i32 @rl_unsubscribe_all(ptr noundef %15)
  br label %17

17:                                               ; preds = %14, %9
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = call i32 @rl_discard(ptr noundef %18)
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.rlite, ptr %20, i32 0, i32 8
  %22 = load i32, ptr %21, align 8, !tbaa !9
  %23 = icmp eq i32 %22, 1
  br i1 %23, label %24, label %31

24:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.rlite, ptr %25, i32 0, i32 7
  %27 = load ptr, ptr %26, align 8, !tbaa !13
  store ptr %27, ptr %4, align 8, !tbaa !5
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.rl_file_driver, ptr %28, i32 0, i32 1
  %30 = load ptr, ptr %29, align 8, !tbaa !14
  call void @free(ptr noundef %30) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %44

31:                                               ; preds = %17
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.rlite, ptr %32, i32 0, i32 8
  %34 = load i32, ptr %33, align 8, !tbaa !9
  %35 = icmp eq i32 %34, 0
  br i1 %35, label %36, label %43

36:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.rlite, ptr %37, i32 0, i32 7
  %39 = load ptr, ptr %38, align 8, !tbaa !13
  store ptr %39, ptr %5, align 8, !tbaa !5
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.rl_memory_driver, ptr %40, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !16
  call void @free(ptr noundef %42) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  br label %43

43:                                               ; preds = %36, %31
  br label %44

44:                                               ; preds = %43, %24
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.rlite, ptr %45, i32 0, i32 20
  %47 = load ptr, ptr %46, align 8, !tbaa !18
  %48 = icmp ne ptr %47, null
  br i1 %48, label %49, label %57

49:                                               ; preds = %44
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.rlite, ptr %50, i32 0, i32 20
  %52 = load ptr, ptr %51, align 8, !tbaa !18
  %53 = call i32 @remove(ptr noundef %52) #4
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.rlite, ptr %54, i32 0, i32 20
  %56 = load ptr, ptr %55, align 8, !tbaa !18
  call void @free(ptr noundef %56) #4
  br label %57

57:                                               ; preds = %49, %44
  %58 = load ptr, ptr %3, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.rlite, ptr %58, i32 0, i32 7
  %60 = load ptr, ptr %59, align 8, !tbaa !13
  call void @free(ptr noundef %60) #4
  %61 = load ptr, ptr %3, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.rlite, ptr %61, i32 0, i32 19
  %63 = load ptr, ptr %62, align 8, !tbaa !19
  call void @free(ptr noundef %63) #4
  %64 = load ptr, ptr %3, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.rlite, ptr %64, i32 0, i32 15
  %66 = load ptr, ptr %65, align 8, !tbaa !20
  call void @free(ptr noundef %66) #4
  %67 = load ptr, ptr %3, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.rlite, ptr %67, i32 0, i32 18
  %69 = load ptr, ptr %68, align 8, !tbaa !21
  call void @free(ptr noundef %69) #4
  %70 = load ptr, ptr %3, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.rlite, ptr %70, i32 0, i32 12
  %72 = load ptr, ptr %71, align 8, !tbaa !22
  call void @free(ptr noundef %72) #4
  %73 = load ptr, ptr %3, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.rlite, ptr %73, i32 0, i32 3
  %75 = load ptr, ptr %74, align 8, !tbaa !23
  call void @free(ptr noundef %75) #4
  %76 = load ptr, ptr %3, align 8, !tbaa !5
  call void @free(ptr noundef %76) #4
  store i32 0, ptr %2, align 4
  br label %77

77:                                               ; preds = %57, %8
  %78 = load i32, ptr %2, align 4
  ret i32 %78
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @rl_discard(ptr noundef) #2

declare i32 @rl_unsubscribe_all(ptr noundef) #3

; Function Attrs: nounwind
declare i32 @remove(ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = { "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, !12, i64 64}
!10 = !{!"rlite", !11, i64 0, !11, i64 8, !12, i64 16, !6, i64 24, !11, i64 32, !11, i64 40, !11, i64 48, !6, i64 56, !12, i64 64, !12, i64 68, !12, i64 72, !12, i64 76, !6, i64 80, !11, i64 88, !11, i64 96, !6, i64 104, !11, i64 112, !11, i64 120, !6, i64 128, !6, i64 136, !6, i64 144, !6, i64 152}
!11 = !{!"long", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !6, i64 56}
!14 = !{!15, !6, i64 8}
!15 = !{!"", !6, i64 0, !6, i64 8, !12, i64 16}
!16 = !{!17, !6, i64 0}
!17 = !{!"", !6, i64 0, !11, i64 8}
!18 = !{!10, !6, i64 144}
!19 = !{!10, !6, i64 136}
!20 = !{!10, !6, i64 104}
!21 = !{!10, !6, i64 128}
!22 = !{!10, !6, i64 80}
!23 = !{!10, !6, i64 24}


; 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"

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @rl_close(ptr noundef %0) local_unnamed_addr #1 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %38, label %2

2:                                                ; preds = %1
  %3 = getelementptr inbounds i8, ptr %0, i64 64
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = icmp eq i32 %4, 1
  br i1 %5, label %6, label %8

6:                                                ; preds = %2
  %7 = tail call i32 @rl_unsubscribe_all(ptr noundef nonnull %0) #4
  br label %8

8:                                                ; preds = %6, %2
  %9 = tail call i32 @rl_discard(ptr noundef nonnull %0)
  %10 = load i32, ptr %3, align 8, !tbaa !5
  switch i32 %10, label %19 [
    i32 1, label %11
    i32 0, label %15
  ]

11:                                               ; preds = %8
  %12 = getelementptr inbounds i8, ptr %0, i64 56
  %13 = load ptr, ptr %12, align 8, !tbaa !12
  %14 = getelementptr inbounds i8, ptr %13, i64 8
  br label %.sink.split

15:                                               ; preds = %8
  %16 = getelementptr inbounds i8, ptr %0, i64 56
  %17 = load ptr, ptr %16, align 8, !tbaa !12
  br label %.sink.split

.sink.split:                                      ; preds = %11, %15
  %.sink23 = phi ptr [ %17, %15 ], [ %14, %11 ]
  %18 = load ptr, ptr %.sink23, align 8, !tbaa !13
  tail call void @free(ptr noundef %18) #4
  br label %19

19:                                               ; preds = %.sink.split, %8
  %20 = getelementptr inbounds i8, ptr %0, i64 144
  %21 = load ptr, ptr %20, align 8, !tbaa !14
  %.not22 = icmp eq ptr %21, null
  br i1 %.not22, label %25, label %22

22:                                               ; preds = %19
  %23 = tail call i32 @remove(ptr noundef nonnull %21) #4
  %24 = load ptr, ptr %20, align 8, !tbaa !14
  tail call void @free(ptr noundef %24) #4
  br label %25

25:                                               ; preds = %22, %19
  %26 = getelementptr inbounds i8, ptr %0, i64 56
  %27 = load ptr, ptr %26, align 8, !tbaa !12
  tail call void @free(ptr noundef %27) #4
  %28 = getelementptr inbounds i8, ptr %0, i64 136
  %29 = load ptr, ptr %28, align 8, !tbaa !15
  tail call void @free(ptr noundef %29) #4
  %30 = getelementptr inbounds i8, ptr %0, i64 104
  %31 = load ptr, ptr %30, align 8, !tbaa !16
  tail call void @free(ptr noundef %31) #4
  %32 = getelementptr inbounds i8, ptr %0, i64 128
  %33 = load ptr, ptr %32, align 8, !tbaa !17
  tail call void @free(ptr noundef %33) #4
  %34 = getelementptr inbounds i8, ptr %0, i64 80
  %35 = load ptr, ptr %34, align 8, !tbaa !18
  tail call void @free(ptr noundef %35) #4
  %36 = getelementptr inbounds i8, ptr %0, i64 24
  %37 = load ptr, ptr %36, align 8, !tbaa !19
  tail call void @free(ptr noundef %37) #4
  tail call void @free(ptr noundef nonnull %0) #4
  br label %38

38:                                               ; preds = %1, %25
  ret i32 0
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @rl_discard(ptr noundef) local_unnamed_addr #1

declare i32 @rl_unsubscribe_all(ptr noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @remove(ptr nocapture noundef readonly) local_unnamed_addr #3

attributes #0 = { 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 #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 = { "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 = { 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 #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, !10, i64 64}
!6 = !{!"rlite", !7, i64 0, !7, i64 8, !10, i64 16, !11, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !11, i64 56, !10, i64 64, !10, i64 68, !10, i64 72, !10, i64 76, !11, i64 80, !7, i64 88, !7, i64 96, !11, i64 104, !7, i64 112, !7, i64 120, !11, i64 128, !11, i64 136, !11, i64 144, !11, i64 152}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !11, i64 56}
!13 = !{!11, !11, i64 0}
!14 = !{!6, !11, i64 144}
!15 = !{!6, !11, i64 136}
!16 = !{!6, !11, i64 104}
!17 = !{!6, !11, i64 128}
!18 = !{!6, !11, i64 80}
!19 = !{!6, !11, i64 24}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass