; LLVM IR from ctlst-tech/catpilot :: os/freertos/services/misc (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.log_module_t = type { ptr, i64, i32, i32, ptr, i32 }

@log = external hidden global %struct.log_module_t, align 8
@FreeRTOS_errno = external global i32, align 4
@.str = external hidden unnamed_addr constant [13 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @log_init(ptr noundef %0, ptr noundef %1, i32 noundef %2, i64 noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca [32 x i8], align 16
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i64 %3, ptr %9, align 8, !tbaa !11
  %13 = load ptr, ptr %7, align 8, !tbaa !5
  %14 = icmp eq ptr %13, null
  br i1 %14, label %18, label %15

15:                                               ; preds = %4
  %16 = load i32, ptr getelementptr inbounds (%struct.log_module_t, ptr @log, i32 0, i32 3), align 4, !tbaa !13
  %17 = icmp sgt i32 %16, 0
  br i1 %17, label %18, label %19

18:                                               ; preds = %15, %4
  store i32 -1, ptr %5, align 4
  br label %77

19:                                               ; preds = %15
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = call i64 @strlen(ptr noundef %20) #5
  %22 = load ptr, ptr %7, align 8, !tbaa !5
  %23 = call i64 @strlen(ptr noundef %22) #5
  %24 = add i64 %21, %23
  %25 = icmp ugt i64 %24, 32
  br i1 %25, label %26, label %27

26:                                               ; preds = %19
  store i32 -1, ptr %5, align 4
  br label %77

27:                                               ; preds = %19
  %28 = load ptr, ptr %7, align 8, !tbaa !5
  %29 = call i32 @mkdir(ptr noundef %28, i32 noundef 0)
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %35

31:                                               ; preds = %27
  %32 = load i32, ptr @FreeRTOS_errno, align 4, !tbaa !9
  %33 = icmp ne i32 %32, 17
  br i1 %33, label %34, label %35

34:                                               ; preds = %31
  store i32 -1, ptr %5, align 4
  br label %77

35:                                               ; preds = %31, %27
  %36 = call ptr @xQueueCreateMutex(i8 noundef zeroext 1)
  store ptr %36, ptr getelementptr inbounds (%struct.log_module_t, ptr @log, i32 0, i32 4), align 8, !tbaa !15
  %37 = load ptr, ptr getelementptr inbounds (%struct.log_module_t, ptr @log, i32 0, i32 4), align 8, !tbaa !15
  %38 = icmp eq ptr %37, null
  br i1 %38, label %39, label %40

39:                                               ; preds = %35
  store i32 -1, ptr %5, align 4
  br label %77

40:                                               ; preds = %35
  %41 = load i64, ptr %9, align 8, !tbaa !11
  %42 = call noalias ptr @calloc(i64 noundef %41, i64 noundef 1) #6
  store ptr %42, ptr @log, align 8, !tbaa !16
  %43 = load ptr, ptr @log, align 8, !tbaa !16
  %44 = icmp eq ptr %43, null
  br i1 %44, label %45, label %46

45:                                               ; preds = %40
  store i32 -1, ptr %5, align 4
  br label %77

46:                                               ; preds = %40
  %47 = load i64, ptr %9, align 8, !tbaa !11
  store i64 %47, ptr getelementptr inbounds (%struct.log_module_t, ptr @log, i32 0, i32 1), align 8, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 32, ptr %10) #7
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #7
  store i32 0, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #7
  store i32 0, ptr %12, align 4, !tbaa !9
  %48 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %48, ptr getelementptr inbounds (%struct.log_module_t, ptr @log, i32 0, i32 5), align 8, !tbaa !18
  %49 = load i32, ptr getelementptr inbounds (%struct.log_module_t, ptr @log, i32 0, i32 5), align 8, !tbaa !18
  %50 = icmp eq i32 %49, 1
  br i1 %50, label %51, label %76

51:                                               ; preds = %46
  br label %52

52:                                               ; preds = %68, %51
  %53 = getelementptr inbounds [32 x i8], ptr %10, i64 0, i64 0
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = load i32, ptr %11, align 4, !tbaa !9
  %57 = call i32 (ptr, i64, ptr, ...) @snprintf_(ptr noundef %53, i64 noundef 32, ptr noundef @.str, ptr noundef %54, ptr noundef %55, i32 noundef %56)
  %58 = getelementptr inbounds [32 x i8], ptr %10, i64 0, i64 0
  %59 = call i32 @open(ptr noundef %58, i32 noundef 40970)
  store i32 %59, ptr %12, align 4, !tbaa !9
  %60 = load i32, ptr %11, align 4, !tbaa !9
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %11, align 4, !tbaa !9
  br label %62

62:                                               ; preds = %52
  %63 = load i32, ptr %12, align 4, !tbaa !9
  %64 = icmp slt i32 %63, 0
  br i1 %64, label %65, label %68

65:                                               ; preds = %62
  %66 = load i32, ptr @FreeRTOS_errno, align 4, !tbaa !9
  %67 = icmp eq i32 %66, 17
  br label %68

68:                                               ; preds = %65, %62
  %69 = phi i1 [ false, %62 ], [ %67, %65 ]
  br i1 %69, label %52, label %70, !llvm.loop !19

70:                                               ; preds = %68
  %71 = load i32, ptr %12, align 4, !tbaa !9
  %72 = icmp sgt i32 %71, 0
  br i1 %72, label %73, label %75

73:                                               ; preds = %70
  %74 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %74, ptr getelementptr inbounds (%struct.log_module_t, ptr @log, i32 0, i32 3), align 4, !tbaa !13
  br label %75

75:                                               ; preds = %73, %70
  br label %76

76:                                               ; preds = %75, %46
  store i32 0, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #7
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #7
  call void @llvm.lifetime.end.p0(i64 32, ptr %10) #7
  br label %77

77:                                               ; preds = %76, %45, %39, %34, %26, %18
  %78 = load i32, ptr %5, align 4
  ret i32 %78
}

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #1

declare i32 @mkdir(ptr noundef, i32 noundef) #2

declare ptr @xQueueCreateMutex(i8 noundef zeroext) #2

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #3

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

declare i32 @snprintf_(ptr noundef, i64 noundef, ptr noundef, ...) #2

declare i32 @open(ptr noundef, i32 noundef) #2

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

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" }
attributes #1 = { nounwind willreturn memory(read) "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 allocsize(0,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 #4 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { nounwind willreturn memory(read) }
attributes #6 = { nounwind allocsize(0,1) }
attributes #7 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !10, i64 20}
!14 = !{!"", !6, i64 0, !12, i64 8, !10, i64 16, !10, i64 20, !6, i64 24, !10, i64 32}
!15 = !{!14, !6, i64 24}
!16 = !{!14, !6, i64 0}
!17 = !{!14, !12, i64 8}
!18 = !{!14, !10, i64 32}
!19 = distinct !{!19, !20}
!20 = !{!"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.log_module_t = type { ptr, i64, i32, i32, ptr, i32 }

@log = external hidden local_unnamed_addr global %struct.log_module_t, align 8
@FreeRTOS_errno = external local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [13 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @log_init(ptr noundef %0, ptr noundef %1, i32 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = alloca [32 x i8], align 16
  %6 = icmp eq ptr %1, null
  %7 = load i32, ptr getelementptr inbounds (i8, ptr @log, i64 20), align 4
  %8 = icmp sgt i32 %7, 0
  %or.cond = select i1 %6, i1 true, i1 %8
  br i1 %or.cond, label %38, label %9

9:                                                ; preds = %4
  %10 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #6
  %11 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %1) #6
  %12 = add i64 %11, %10
  %13 = icmp ugt i64 %12, 32
  br i1 %13, label %38, label %14

14:                                               ; preds = %9
  %15 = tail call i32 @mkdir(ptr noundef nonnull %1, i32 noundef 0)
  %16 = icmp ne i32 %15, 0
  %17 = load i32, ptr @FreeRTOS_errno, align 4
  %18 = icmp ne i32 %17, 17
  %or.cond3 = select i1 %16, i1 %18, i1 false
  br i1 %or.cond3, label %38, label %19

19:                                               ; preds = %14
  %20 = tail call ptr @xQueueCreateMutex(i8 noundef zeroext 1) #7
  store ptr %20, ptr getelementptr inbounds (i8, ptr @log, i64 24), align 8, !tbaa !5
  %21 = icmp eq ptr %20, null
  br i1 %21, label %38, label %22

22:                                               ; preds = %19
  %23 = tail call noalias ptr @calloc(i64 noundef %3, i64 noundef 1) #8
  store ptr %23, ptr @log, align 8, !tbaa !12
  %24 = icmp eq ptr %23, null
  br i1 %24, label %38, label %25

25:                                               ; preds = %22
  store i64 %3, ptr getelementptr inbounds (i8, ptr @log, i64 8), align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %5) #7
  store i32 %2, ptr getelementptr inbounds (i8, ptr @log, i64 32), align 8, !tbaa !14
  %26 = icmp eq i32 %2, 1
  br i1 %26, label %.preheader, label %37

.preheader:                                       ; preds = %25, %.preheader
  %.0 = phi i32 [ %29, %.preheader ], [ 0, %25 ]
  %27 = call i32 (ptr, i64, ptr, ...) @snprintf_(ptr noundef nonnull %5, i64 noundef 32, ptr noundef nonnull @.str, ptr noundef %1, ptr noundef %0, i32 noundef %.0) #7
  %28 = call i32 @open(ptr noundef nonnull %5, i32 noundef 40970) #7
  %29 = add nuw nsw i32 %.0, 1
  %30 = icmp slt i32 %28, 0
  %31 = load i32, ptr @FreeRTOS_errno, align 4
  %32 = icmp eq i32 %31, 17
  %33 = select i1 %30, i1 %32, i1 false
  br i1 %33, label %.preheader, label %34, !llvm.loop !15

34:                                               ; preds = %.preheader
  %35 = icmp sgt i32 %28, 0
  br i1 %35, label %36, label %37

36:                                               ; preds = %34
  store i32 %28, ptr getelementptr inbounds (i8, ptr @log, i64 20), align 4, !tbaa !17
  br label %37

37:                                               ; preds = %34, %36, %25
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %5) #7
  br label %38

38:                                               ; preds = %22, %19, %14, %9, %4, %37
  %.017 = phi i32 [ 0, %37 ], [ -1, %4 ], [ -1, %9 ], [ -1, %14 ], [ -1, %19 ], [ -1, %22 ]
  ret i32 %.017
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #1

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

declare ptr @xQueueCreateMutex(i8 noundef zeroext) local_unnamed_addr #3

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @calloc(i64 noundef, i64 noundef) local_unnamed_addr #4

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

declare i32 @snprintf_(ptr noundef, i64 noundef, ptr noundef, ...) local_unnamed_addr #3

declare i32 @open(ptr noundef, i32 noundef) local_unnamed_addr #3

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

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" }
attributes #1 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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" }
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 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(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 #5 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #6 = { nounwind willreturn memory(read) }
attributes #7 = { nounwind }
attributes #8 = { nounwind allocsize(0,1) }

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass