; LLVM IR from specinfo-ua/UAPKI :: library/uapkic/src (stacktrace)

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

%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%struct.ErrorsCtx_st = type { ptr, ptr }
%struct.StackTraceCtx_st = type { ptr, i64, ptr, ptr }

@errors_mutex = external hidden global %union.pthread_mutex_t, align 8
@errors_list = external hidden global ptr, align 8

; 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 i32 @pthread_mutex_lock(ptr noundef) #1

declare i64 @pthread_id() #2

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

; Function Attrs: nounwind uwtable
define dso_local void @stacktrace_free_current() #3 {
  %1 = alloca i64, align 8
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #4
  %5 = call i64 @pthread_id()
  store i64 %5, ptr %1, align 8, !tbaa !5
  %6 = call i32 @pthread_mutex_lock(ptr noundef @errors_mutex) #4
  %7 = load ptr, ptr @errors_list, align 8, !tbaa !9
  %8 = icmp ne ptr %7, null
  br i1 %8, label %9, label %79

9:                                                ; preds = %0
  %10 = load ptr, ptr @errors_list, align 8, !tbaa !9
  %11 = getelementptr inbounds %struct.ErrorsCtx_st, ptr %10, i32 0, i32 0
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %36

14:                                               ; preds = %9
  %15 = load ptr, ptr @errors_list, align 8, !tbaa !9
  %16 = getelementptr inbounds %struct.ErrorsCtx_st, ptr %15, i32 0, i32 0
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  %18 = getelementptr inbounds %struct.StackTraceCtx_st, ptr %17, i32 0, i32 1
  %19 = load i64, ptr %18, align 8, !tbaa !13
  %20 = load i64, ptr %1, align 8, !tbaa !5
  %21 = icmp eq i64 %19, %20
  br i1 %21, label %22, label %36

22:                                               ; preds = %14
  %23 = load ptr, ptr @errors_list, align 8, !tbaa !9
  %24 = getelementptr inbounds %struct.ErrorsCtx_st, ptr %23, i32 0, i32 1
  %25 = load ptr, ptr %24, align 8, !tbaa !15
  %26 = icmp eq ptr %25, null
  br i1 %26, label %27, label %29

27:                                               ; preds = %22
  %28 = load ptr, ptr @errors_list, align 8, !tbaa !9
  call void @errors_free(ptr noundef %28)
  store ptr null, ptr @errors_list, align 8, !tbaa !9
  br label %35

29:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  %30 = load ptr, ptr @errors_list, align 8, !tbaa !9
  store ptr %30, ptr %2, align 8, !tbaa !9
  %31 = load ptr, ptr @errors_list, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.ErrorsCtx_st, ptr %31, i32 0, i32 1
  %33 = load ptr, ptr %32, align 8, !tbaa !15
  store ptr %33, ptr @errors_list, align 8, !tbaa !9
  %34 = load ptr, ptr %2, align 8, !tbaa !9
  call void @errors_free(ptr noundef %34)
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  br label %35

35:                                               ; preds = %29, %27
  br label %78

36:                                               ; preds = %14, %9
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %37 = load ptr, ptr @errors_list, align 8, !tbaa !9
  store ptr %37, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  br label %38

38:                                               ; preds = %75, %36
  %39 = load ptr, ptr %3, align 8, !tbaa !9
  %40 = icmp ne ptr %39, null
  br i1 %40, label %41, label %77

41:                                               ; preds = %38
  %42 = load ptr, ptr %3, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.ErrorsCtx_st, ptr %42, i32 0, i32 1
  %44 = load ptr, ptr %43, align 8, !tbaa !15
  store ptr %44, ptr %4, align 8, !tbaa !9
  %45 = load ptr, ptr %4, align 8, !tbaa !9
  %46 = icmp ne ptr %45, null
  br i1 %46, label %47, label %75

47:                                               ; preds = %41
  %48 = load ptr, ptr %4, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.ErrorsCtx_st, ptr %48, i32 0, i32 0
  %50 = load ptr, ptr %49, align 8, !tbaa !11
  %51 = icmp ne ptr %50, null
  br i1 %51, label %52, label %75

52:                                               ; preds = %47
  %53 = load ptr, ptr %4, align 8, !tbaa !9
  %54 = getelementptr inbounds %struct.ErrorsCtx_st, ptr %53, i32 0, i32 0
  %55 = load ptr, ptr %54, align 8, !tbaa !11
  %56 = getelementptr inbounds %struct.StackTraceCtx_st, ptr %55, i32 0, i32 1
  %57 = load i64, ptr %56, align 8, !tbaa !13
  %58 = load i64, ptr %1, align 8, !tbaa !5
  %59 = icmp eq i64 %57, %58
  br i1 %59, label %60, label %75

60:                                               ; preds = %52
  %61 = load ptr, ptr %4, align 8, !tbaa !9
  %62 = getelementptr inbounds %struct.ErrorsCtx_st, ptr %61, i32 0, i32 1
  %63 = load ptr, ptr %62, align 8, !tbaa !15
  %64 = icmp ne ptr %63, null
  br i1 %64, label %65, label %69

65:                                               ; preds = %60
  %66 = load ptr, ptr %4, align 8, !tbaa !9
  %67 = getelementptr inbounds %struct.ErrorsCtx_st, ptr %66, i32 0, i32 1
  %68 = load ptr, ptr %67, align 8, !tbaa !15
  br label %70

69:                                               ; preds = %60
  br label %70

70:                                               ; preds = %69, %65
  %71 = phi ptr [ %68, %65 ], [ null, %69 ]
  %72 = load ptr, ptr %3, align 8, !tbaa !9
  %73 = getelementptr inbounds %struct.ErrorsCtx_st, ptr %72, i32 0, i32 1
  store ptr %71, ptr %73, align 8, !tbaa !15
  %74 = load ptr, ptr %4, align 8, !tbaa !9
  call void @errors_free(ptr noundef %74)
  br label %77

75:                                               ; preds = %52, %47, %41
  %76 = load ptr, ptr %4, align 8, !tbaa !9
  store ptr %76, ptr %3, align 8, !tbaa !9
  br label %38, !llvm.loop !16

77:                                               ; preds = %70, %38
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  br label %78

78:                                               ; preds = %77, %35
  br label %79

79:                                               ; preds = %78, %0
  %80 = call i32 @pthread_mutex_unlock(ptr noundef @errors_mutex) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #4
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @errors_free(ptr noundef) #3

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 = { "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 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 }

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"ErrorsCtx_st", !10, i64 0, !10, i64 8}
!13 = !{!14, !6, i64 8}
!14 = !{!"StackTraceCtx_st", !10, i64 0, !6, i64 8, !10, i64 16, !10, i64 24}
!15 = !{!12, !10, i64 8}
!16 = distinct !{!16, !17}
!17 = !{!"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"

%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }

@errors_mutex = external hidden global %union.pthread_mutex_t, align 8
@errors_list = external hidden local_unnamed_addr global ptr, align 8

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

declare i64 @pthread_id() local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local void @stacktrace_free_current() local_unnamed_addr #2 {
  %1 = tail call i64 @pthread_id() #3
  %2 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull @errors_mutex) #3
  %3 = load ptr, ptr @errors_list, align 8, !tbaa !5
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %.loopexit, label %4

4:                                                ; preds = %0
  %5 = load ptr, ptr %3, align 8, !tbaa !9
  %.not18 = icmp eq ptr %5, null
  br i1 %.not18, label %16, label %6

6:                                                ; preds = %4
  %7 = getelementptr inbounds i8, ptr %5, i64 8
  %8 = load i64, ptr %7, align 8, !tbaa !11
  %9 = icmp eq i64 %8, %1
  br i1 %9, label %10, label %16

10:                                               ; preds = %6
  %11 = getelementptr inbounds i8, ptr %3, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !14
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %15

14:                                               ; preds = %10
  tail call void @errors_free(ptr noundef nonnull %3)
  store ptr null, ptr @errors_list, align 8, !tbaa !5
  br label %.loopexit

15:                                               ; preds = %10
  store ptr %12, ptr @errors_list, align 8, !tbaa !5
  tail call void @errors_free(ptr noundef nonnull %3)
  br label %.loopexit

16:                                               ; preds = %6, %4
  %17 = getelementptr inbounds i8, ptr %3, i64 8
  %18 = load ptr, ptr %17, align 8, !tbaa !14
  %cond32 = icmp eq ptr %18, null
  br i1 %cond32, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %16, %29
  %19 = phi ptr [ %31, %29 ], [ %18, %16 ]
  %.02433 = phi ptr [ %19, %29 ], [ %3, %16 ]
  %20 = load ptr, ptr %19, align 8, !tbaa !9
  %.not21 = icmp eq ptr %20, null
  br i1 %.not21, label %29, label %21

21:                                               ; preds = %.lr.ph
  %22 = getelementptr inbounds i8, ptr %20, i64 8
  %23 = load i64, ptr %22, align 8, !tbaa !11
  %24 = icmp eq i64 %23, %1
  br i1 %24, label %25, label %29

25:                                               ; preds = %21
  %26 = getelementptr inbounds i8, ptr %.02433, i64 8
  %27 = getelementptr inbounds i8, ptr %19, i64 8
  %28 = load ptr, ptr %27, align 8, !tbaa !14
  store ptr %28, ptr %26, align 8, !tbaa !14
  tail call void @errors_free(ptr noundef nonnull %19)
  br label %.loopexit

29:                                               ; preds = %21, %.lr.ph
  %30 = getelementptr inbounds i8, ptr %19, i64 8
  %31 = load ptr, ptr %30, align 8, !tbaa !14
  %cond = icmp eq ptr %31, null
  br i1 %cond, label %.loopexit, label %.lr.ph

.loopexit:                                        ; preds = %29, %16, %25, %15, %14, %0
  %32 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull @errors_mutex) #3
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @errors_free(ptr noundef) local_unnamed_addr #2

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 = { "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 = { 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, !6, i64 0}
!10 = !{!"ErrorsCtx_st", !6, i64 0, !6, i64 8}
!11 = !{!12, !13, i64 8}
!12 = !{!"StackTraceCtx_st", !6, i64 0, !13, i64 8, !6, i64 16, !6, i64 24}
!13 = !{!"long", !7, i64 0}
!14 = !{!10, !6, i64 8}


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