; LLVM IR from tezc/sc :: logger (sc_log)

; 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.sc_log = type { ptr, [256 x i8], [256 x i8], i64, %struct.sc_log_mutex, i32, i8, i8, ptr, ptr }
%struct.sc_log_mutex = type { %union.pthread_mutex_t }
%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 }

@sc_log = external dso_local global %struct.sc_log, 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

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

; Function Attrs: nounwind uwtable
declare dso_local void @sc_log_mutex_term(ptr noundef) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @sc_log_term() #3 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca %struct.sc_log, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  store i32 0, ptr %2, align 4, !tbaa !5
  %5 = load i8, ptr getelementptr inbounds (%struct.sc_log, ptr @sc_log, i32 0, i32 6), align 4, !tbaa !9, !range !15, !noundef !16
  %6 = trunc i8 %5 to i1
  br i1 %6, label %8, label %7

7:                                                ; preds = %0
  store i32 -1, ptr %1, align 4
  store i32 1, ptr %3, align 4
  br label %30

8:                                                ; preds = %0
  %9 = load ptr, ptr @sc_log, align 8, !tbaa !17
  %10 = icmp ne ptr %9, null
  br i1 %10, label %11, label %18

11:                                               ; preds = %8
  %12 = load ptr, ptr @sc_log, align 8, !tbaa !17
  %13 = call i32 @fclose(ptr noundef %12)
  store i32 %13, ptr %2, align 4, !tbaa !5
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %16, label %17

16:                                               ; preds = %11
  store i32 -1, ptr %2, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %16, %11
  br label %18

18:                                               ; preds = %17, %8
  call void @sc_log_mutex_term(ptr noundef getelementptr inbounds (%struct.sc_log, ptr @sc_log, i32 0, i32 4))
  call void @llvm.memset.p0.i64(ptr align 8 %4, i8 0, i64 592, i1 false)
  %19 = getelementptr inbounds %struct.sc_log, ptr %4, i32 0, i32 0
  %20 = getelementptr inbounds %struct.sc_log, ptr %4, i32 0, i32 1
  %21 = getelementptr inbounds %struct.sc_log, ptr %4, i32 0, i32 2
  %22 = getelementptr inbounds %struct.sc_log, ptr %4, i32 0, i32 3
  %23 = getelementptr inbounds %struct.sc_log, ptr %4, i32 0, i32 4
  %24 = getelementptr inbounds %struct.sc_log, ptr %4, i32 0, i32 5
  %25 = getelementptr inbounds %struct.sc_log, ptr %4, i32 0, i32 6
  %26 = getelementptr inbounds %struct.sc_log, ptr %4, i32 0, i32 7
  %27 = getelementptr inbounds %struct.sc_log, ptr %4, i32 0, i32 8
  %28 = getelementptr inbounds %struct.sc_log, ptr %4, i32 0, i32 9
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 @sc_log, ptr align 8 %4, i64 592, i1 false), !tbaa.struct !18
  %29 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %29, ptr %1, align 4
  store i32 1, ptr %3, align 4
  br label %30

30:                                               ; preds = %18, %7
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  %31 = load i32, ptr %1, align 4
  ret i32 %31
}

declare i32 @fclose(ptr noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { "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 }

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !14, i64 572}
!10 = !{!"sc_log", !11, i64 0, !7, i64 8, !7, i64 264, !12, i64 520, !13, i64 528, !7, i64 568, !14, i64 572, !14, i64 573, !11, i64 576, !11, i64 584}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!"sc_log_mutex", !7, i64 0}
!14 = !{!"_Bool", !7, i64 0}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = !{!10, !11, i64 0}
!18 = !{i64 0, i64 8, !19, i64 8, i64 256, !20, i64 264, i64 256, !20, i64 520, i64 8, !21, i64 528, i64 40, !20, i64 568, i64 4, !20, i64 572, i64 1, !22, i64 573, i64 1, !22, i64 576, i64 8, !19, i64 584, i64 8, !19}
!19 = !{!11, !11, i64 0}
!20 = !{!7, !7, i64 0}
!21 = !{!12, !12, i64 0}
!22 = !{!14, !14, i64 0}


; 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.sc_log = type { ptr, [256 x i8], [256 x i8], i64, %struct.sc_log_mutex, i32, i8, i8, ptr, ptr }
%struct.sc_log_mutex = type { %union.pthread_mutex_t }
%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 }

@sc_log = external dso_local global %struct.sc_log, align 8

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @sc_log_term() local_unnamed_addr #1 {
  %1 = load i8, ptr getelementptr inbounds (i8, ptr @sc_log, i64 572), align 4, !tbaa !5, !range !13, !noundef !14
  %2 = trunc nuw i8 %1 to i1
  br i1 %2, label %3, label %8

3:                                                ; preds = %0
  %4 = load ptr, ptr @sc_log, align 8, !tbaa !15
  %.not = icmp eq ptr %4, null
  br i1 %.not, label %7, label %5

5:                                                ; preds = %3
  %6 = tail call i32 @fclose(ptr noundef nonnull %4)
  %.not5 = icmp ne i32 %6, 0
  %spec.store.select = sext i1 %.not5 to i32
  br label %7

7:                                                ; preds = %5, %3
  %.0 = phi i32 [ %spec.store.select, %5 ], [ 0, %3 ]
  tail call void @sc_log_mutex_term(ptr noundef nonnull getelementptr inbounds (i8, ptr @sc_log, i64 528))
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(592) @sc_log, i8 0, i64 592, i1 false)
  br label %8

8:                                                ; preds = %0, %7
  %.03 = phi i32 [ %.0, %7 ], [ -1, %0 ]
  ret i32 %.03
}

; Function Attrs: nofree nounwind
declare noundef i32 @fclose(ptr nocapture noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { 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" }

!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, !12, i64 572}
!6 = !{!"sc_log", !7, i64 0, !8, i64 8, !8, i64 264, !10, i64 520, !11, i64 528, !8, i64 568, !12, i64 572, !12, i64 573, !7, i64 576, !7, i64 584}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"sc_log_mutex", !8, i64 0}
!12 = !{!"_Bool", !8, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = !{!6, !7, i64 0}


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