; 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 }
%struct.sc_log_level_pair = type { i32, ptr }

@sc_log = external dso_local global %struct.sc_log, align 8
@sc_log_levels = external hidden constant [5 x %struct.sc_log_level_pair], align 16

; 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 uwtable
define dso_local i32 @sc_log_set_level(ptr noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 5, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store i64 0, ptr %5, align 8, !tbaa !9
  br label %8

8:                                                ; preds = %28, %1
  %9 = load i64, ptr %5, align 8, !tbaa !9
  %10 = load i64, ptr %4, align 8, !tbaa !9
  %11 = icmp ult i64 %9, %10
  br i1 %11, label %13, label %12

12:                                               ; preds = %8
  store i32 2, ptr %6, align 4
  br label %31

13:                                               ; preds = %8
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = load i64, ptr %5, align 8, !tbaa !9
  %16 = getelementptr inbounds [5 x %struct.sc_log_level_pair], ptr @sc_log_levels, i64 0, i64 %15
  %17 = getelementptr inbounds %struct.sc_log_level_pair, ptr %16, i32 0, i32 1
  %18 = load ptr, ptr %17, align 8, !tbaa !11
  %19 = call i32 @sc_strcasecmp(ptr noundef %14, ptr noundef %18)
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %27

21:                                               ; preds = %13
  %22 = load i64, ptr %5, align 8, !tbaa !9
  %23 = getelementptr inbounds [5 x %struct.sc_log_level_pair], ptr @sc_log_levels, i64 0, i64 %22
  %24 = getelementptr inbounds %struct.sc_log_level_pair, ptr %23, i32 0, i32 0
  %25 = load i32, ptr %24, align 16, !tbaa !14
  store i32 %25, ptr %7, align 4, !tbaa !15
  %26 = load i32, ptr %7, align 4
  store atomic i32 %26, ptr getelementptr inbounds (%struct.sc_log, ptr @sc_log, i32 0, i32 5) monotonic, align 8
  store i32 0, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %31

27:                                               ; preds = %13
  br label %28

28:                                               ; preds = %27
  %29 = load i64, ptr %5, align 8, !tbaa !9
  %30 = add i64 %29, 1
  store i64 %30, ptr %5, align 8, !tbaa !9
  br label %8, !llvm.loop !16

31:                                               ; preds = %21, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  %32 = load i32, ptr %6, align 4
  switch i32 %32, label %34 [
    i32 2, label %33
  ]

33:                                               ; preds = %31
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %34

34:                                               ; preds = %33, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %35 = load i32, ptr %2, align 4
  ret i32 %35
}

; Function Attrs: nounwind uwtable
declare hidden i32 @sc_strcasecmp(ptr noundef, ptr noundef) #1

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

!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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"sc_log_level_pair", !13, i64 0, !6, i64 8}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !13, i64 0}
!15 = !{!13, !13, i64 0}
!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"

%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 }
%struct.sc_log_level_pair = type { i32, ptr }

@sc_log = external dso_local local_unnamed_addr global %struct.sc_log, align 8
@sc_log_levels = external hidden local_unnamed_addr constant [5 x %struct.sc_log_level_pair], align 16

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @sc_log_set_level(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr getelementptr inbounds (i8, ptr @sc_log_levels, i64 8), align 8, !tbaa !5
  %3 = tail call i32 @sc_strcasecmp(ptr noundef %0, ptr noundef %2)
  %4 = icmp eq i32 %3, 0
  br i1 %4, label %21, label %5

5:                                                ; preds = %1
  %6 = load ptr, ptr getelementptr inbounds (i8, ptr @sc_log_levels, i64 24), align 8, !tbaa !5
  %7 = tail call i32 @sc_strcasecmp(ptr noundef %0, ptr noundef %6)
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %21, label %9

9:                                                ; preds = %5
  %10 = load ptr, ptr getelementptr inbounds (i8, ptr @sc_log_levels, i64 40), align 8, !tbaa !5
  %11 = tail call i32 @sc_strcasecmp(ptr noundef %0, ptr noundef %10)
  %12 = icmp eq i32 %11, 0
  br i1 %12, label %21, label %13

13:                                               ; preds = %9
  %14 = load ptr, ptr getelementptr inbounds (i8, ptr @sc_log_levels, i64 56), align 8, !tbaa !5
  %15 = tail call i32 @sc_strcasecmp(ptr noundef %0, ptr noundef %14)
  %16 = icmp eq i32 %15, 0
  br i1 %16, label %21, label %17

17:                                               ; preds = %13
  %18 = load ptr, ptr getelementptr inbounds (i8, ptr @sc_log_levels, i64 72), align 8, !tbaa !5
  %19 = tail call i32 @sc_strcasecmp(ptr noundef %0, ptr noundef %18)
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %.loopexit

21:                                               ; preds = %17, %13, %9, %5, %1
  %.lcssa = phi ptr [ @sc_log_levels, %1 ], [ getelementptr inbounds (i8, ptr @sc_log_levels, i64 16), %5 ], [ getelementptr inbounds (i8, ptr @sc_log_levels, i64 32), %9 ], [ getelementptr inbounds (i8, ptr @sc_log_levels, i64 48), %13 ], [ getelementptr inbounds (i8, ptr @sc_log_levels, i64 64), %17 ]
  %22 = load i32, ptr %.lcssa, align 16, !tbaa !11
  store atomic i32 %22, ptr getelementptr inbounds (i8, ptr @sc_log, i64 568) monotonic, align 8
  br label %.loopexit

.loopexit:                                        ; preds = %17, %21
  %spec.select = phi i32 [ 0, %21 ], [ -1, %17 ]
  ret i32 %spec.select
}

; Function Attrs: nounwind uwtable
declare hidden i32 @sc_strcasecmp(ptr noundef, ptr noundef) local_unnamed_addr #0

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

!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 8}
!6 = !{!"sc_log_level_pair", !7, i64 0, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 0}


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