; LLVM IR from ohler55/agoo :: ext/agoo (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._agooLog = type { ptr, [1024 x i8], [16 x i8], ptr, i32, i32, i64, i64, i8, i8, i8, i8, i8, i32, i64, i64, [16 x i8], ptr, ptr, %struct._agooAtom, %struct._agooAtom, %struct._agooAtom, %struct._agooAtom, i32, i32, ptr }
%struct._agooAtom = type { ptr, %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._agooErr = type { i32, [256 x i8] }

@agoo_log = external dso_local global %struct._agooLog, align 8
@.str.4 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [13 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [23 x i8], align 1

; 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
declare dso_local void @agoo_log_rotate() #1

; Function Attrs: nounwind
declare i32 @getpid() #2

; Function Attrs: nounwind
declare i32 @snprintf(ptr noundef, i64 noundef, ptr noundef, ...) #2

declare noalias ptr @fopen(ptr noundef, ptr noundef) #3

; Function Attrs: nounwind uwtable
define dso_local void @agoo_log_open_file() #1 {
  %1 = alloca [1500 x i8], align 16
  %2 = alloca %struct._agooErr, align 4
  call void @llvm.lifetime.start.p0(i64 1500, ptr %1) #4
  %3 = load i8, ptr getelementptr inbounds (%struct._agooLog, ptr @agoo_log, i32 0, i32 12), align 4, !tbaa !5, !range !14, !noundef !15
  %4 = trunc i8 %3 to i1
  br i1 %4, label %5, label %9

5:                                                ; preds = %0
  %6 = getelementptr inbounds [1500 x i8], ptr %1, i64 0, i64 0
  %7 = call i32 @getpid() #4
  %8 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %6, i64 noundef 1500, ptr noundef @.str.6, ptr noundef getelementptr inbounds (%struct._agooLog, ptr @agoo_log, i32 0, i32 1), ptr noundef getelementptr inbounds (%struct._agooLog, ptr @agoo_log, i32 0, i32 2), i32 noundef %7) #4
  br label %12

9:                                                ; preds = %0
  %10 = getelementptr inbounds [1500 x i8], ptr %1, i64 0, i64 0
  %11 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %10, i64 noundef 1500, ptr noundef @.str.4, ptr noundef getelementptr inbounds (%struct._agooLog, ptr @agoo_log, i32 0, i32 1), ptr noundef getelementptr inbounds (%struct._agooLog, ptr @agoo_log, i32 0, i32 2)) #4
  br label %12

12:                                               ; preds = %9, %5
  %13 = getelementptr inbounds [1500 x i8], ptr %1, i64 0, i64 0
  %14 = call noalias ptr @fopen(ptr noundef %13, ptr noundef @.str.7)
  store ptr %14, ptr getelementptr inbounds (%struct._agooLog, ptr @agoo_log, i32 0, i32 3), align 8, !tbaa !16
  %15 = load ptr, ptr getelementptr inbounds (%struct._agooLog, ptr @agoo_log, i32 0, i32 3), align 8, !tbaa !16
  %16 = icmp eq ptr null, %15
  br i1 %16, label %17, label %21

17:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 260, ptr %2) #4
  %18 = getelementptr inbounds [1500 x i8], ptr %1, i64 0, i64 0
  %19 = call i32 (ptr, ptr, ...) @agoo_err_no(ptr noundef %2, ptr noundef @.str.8, ptr noundef %18)
  %20 = load ptr, ptr getelementptr inbounds (%struct._agooLog, ptr @agoo_log, i32 0, i32 25), align 8, !tbaa !17
  call void %20(ptr noundef %2)
  call void @llvm.lifetime.end.p0(i64 260, ptr %2) #4
  br label %21

21:                                               ; preds = %17, %12
  %22 = load ptr, ptr getelementptr inbounds (%struct._agooLog, ptr @agoo_log, i32 0, i32 3), align 8, !tbaa !16
  %23 = call i64 @ftell(ptr noundef %22)
  store i64 %23, ptr getelementptr inbounds (%struct._agooLog, ptr @agoo_log, i32 0, i32 6), align 8, !tbaa !18
  %24 = load i32, ptr getelementptr inbounds (%struct._agooLog, ptr @agoo_log, i32 0, i32 5), align 4, !tbaa !19
  %25 = sext i32 %24 to i64
  %26 = load i64, ptr getelementptr inbounds (%struct._agooLog, ptr @agoo_log, i32 0, i32 6), align 8, !tbaa !18
  %27 = icmp sle i64 %25, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %21
  call void @agoo_log_rotate()
  br label %29

29:                                               ; preds = %28, %21
  call void @llvm.lifetime.end.p0(i64 1500, ptr %1) #4
  ret void
}

declare i32 @agoo_err_no(ptr noundef, ptr noundef, ...) #3

declare i64 @ftell(ptr noundef) #3

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 "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, !12, i64 1084}
!6 = !{!"_agooLog", !7, i64 0, !8, i64 8, !8, i64 1032, !7, i64 1048, !10, i64 1056, !10, i64 1060, !11, i64 1064, !11, i64 1072, !12, i64 1080, !12, i64 1081, !12, i64 1082, !12, i64 1083, !12, i64 1084, !10, i64 1088, !11, i64 1096, !11, i64 1104, !8, i64 1112, !7, i64 1128, !7, i64 1136, !13, i64 1144, !13, i64 1192, !13, i64 1240, !13, i64 1288, !10, i64 1336, !10, i64 1340, !7, i64 1344}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!"_Bool", !8, i64 0}
!13 = !{!"_agooAtom", !7, i64 0, !8, i64 8}
!14 = !{i8 0, i8 2}
!15 = !{}
!16 = !{!6, !7, i64 1048}
!17 = !{!6, !7, i64 1344}
!18 = !{!6, !11, i64 1064}
!19 = !{!6, !10, i64 1060}


; 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._agooLog = type { ptr, [1024 x i8], [16 x i8], ptr, i32, i32, i64, i64, i8, i8, i8, i8, i8, i32, i64, i64, [16 x i8], ptr, ptr, %struct._agooAtom, %struct._agooAtom, %struct._agooAtom, %struct._agooAtom, i32, i32, ptr }
%struct._agooAtom = type { ptr, %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._agooErr = type { i32, [256 x i8] }

@agoo_log = external dso_local global %struct._agooLog, align 8
@.str.4 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [13 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [23 x i8], align 1

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

; Function Attrs: mustprogress 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 @agoo_log_rotate() local_unnamed_addr #1

; Function Attrs: nounwind
declare i32 @getpid() local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @snprintf(ptr noalias nocapture noundef writeonly, i64 noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noalias noundef ptr @fopen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local void @agoo_log_open_file() local_unnamed_addr #1 {
  %1 = alloca [1500 x i8], align 16
  %2 = alloca %struct._agooErr, align 4
  call void @llvm.lifetime.start.p0(i64 1500, ptr nonnull %1) #5
  %3 = load i8, ptr getelementptr inbounds (i8, ptr @agoo_log, i64 1084), align 4, !tbaa !5, !range !14, !noundef !15
  %4 = trunc nuw i8 %3 to i1
  br i1 %4, label %5, label %8

5:                                                ; preds = %0
  %6 = tail call i32 @getpid() #5
  %7 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef nonnull dereferenceable(1) %1, i64 noundef 1500, ptr noundef nonnull @.str.6, ptr noundef nonnull getelementptr inbounds (i8, ptr @agoo_log, i64 8), ptr noundef nonnull getelementptr inbounds (i8, ptr @agoo_log, i64 1032), i32 noundef %6) #5
  br label %10

8:                                                ; preds = %0
  %9 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef nonnull dereferenceable(1) %1, i64 noundef 1500, ptr noundef nonnull @.str.4, ptr noundef nonnull getelementptr inbounds (i8, ptr @agoo_log, i64 8), ptr noundef nonnull getelementptr inbounds (i8, ptr @agoo_log, i64 1032)) #5
  br label %10

10:                                               ; preds = %8, %5
  %11 = call noalias ptr @fopen(ptr noundef nonnull %1, ptr noundef nonnull @.str.7)
  store ptr %11, ptr getelementptr inbounds (i8, ptr @agoo_log, i64 1048), align 8, !tbaa !16
  %12 = icmp eq ptr %11, null
  br i1 %12, label %13, label %16

13:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 260, ptr nonnull %2) #5
  %14 = call i32 (ptr, ptr, ...) @agoo_err_no(ptr noundef nonnull %2, ptr noundef nonnull @.str.8, ptr noundef nonnull %1) #5
  %15 = load ptr, ptr getelementptr inbounds (i8, ptr @agoo_log, i64 1344), align 8, !tbaa !17
  call void %15(ptr noundef nonnull %2) #5
  call void @llvm.lifetime.end.p0(i64 260, ptr nonnull %2) #5
  %.pre = load ptr, ptr getelementptr inbounds (i8, ptr @agoo_log, i64 1048), align 8, !tbaa !16
  br label %16

16:                                               ; preds = %13, %10
  %17 = phi ptr [ %.pre, %13 ], [ %11, %10 ]
  %18 = call i64 @ftell(ptr noundef %17)
  store i64 %18, ptr getelementptr inbounds (i8, ptr @agoo_log, i64 1064), align 8, !tbaa !18
  %19 = load i32, ptr getelementptr inbounds (i8, ptr @agoo_log, i64 1060), align 4, !tbaa !19
  %20 = sext i32 %19 to i64
  %.not = icmp slt i64 %18, %20
  br i1 %.not, label %22, label %21

21:                                               ; preds = %16
  call void @agoo_log_rotate()
  br label %22

22:                                               ; preds = %21, %16
  call void @llvm.lifetime.end.p0(i64 1500, ptr nonnull %1) #5
  ret void
}

declare i32 @agoo_err_no(ptr noundef, ptr noundef, ...) local_unnamed_addr #4

; Function Attrs: nofree nounwind
declare noundef i64 @ftell(ptr nocapture noundef) local_unnamed_addr #3

attributes #0 = { mustprogress 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 "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 = { "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, !12, i64 1084}
!6 = !{!"_agooLog", !7, i64 0, !8, i64 8, !8, i64 1032, !7, i64 1048, !10, i64 1056, !10, i64 1060, !11, i64 1064, !11, i64 1072, !12, i64 1080, !12, i64 1081, !12, i64 1082, !12, i64 1083, !12, i64 1084, !10, i64 1088, !11, i64 1096, !11, i64 1104, !8, i64 1112, !7, i64 1128, !7, i64 1136, !13, i64 1144, !13, i64 1192, !13, i64 1240, !13, i64 1288, !10, i64 1336, !10, i64 1340, !7, i64 1344}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!"_Bool", !8, i64 0}
!13 = !{!"_agooAtom", !7, i64 0, !8, i64 8}
!14 = !{i8 0, i8 2}
!15 = !{}
!16 = !{!6, !7, i64 1048}
!17 = !{!6, !7, i64 1344}
!18 = !{!6, !11, i64 1064}
!19 = !{!6, !10, i64 1060}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,TailCallElimPass