; 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

; 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

declare i64 @xQueueSemaphoreTake(ptr noundef, i32 noundef) #1

declare i32 @write(i32 noundef, ptr noundef, i64 noundef) #1

declare i64 @xQueueGenericSend(ptr noundef, ptr noundef, i32 noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @log_print(i32 noundef %0, ptr noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  %6 = load ptr, ptr getelementptr inbounds (%struct.log_module_t, ptr @log, i32 0, i32 4), align 8, !tbaa !11
  %7 = call i64 @xQueueSemaphoreTake(ptr noundef %6, i32 noundef -1)
  %8 = load i32, ptr getelementptr inbounds (%struct.log_module_t, ptr @log, i32 0, i32 5), align 8, !tbaa !14
  %9 = icmp eq i32 %8, 1
  br i1 %9, label %10, label %34

10:                                               ; preds = %2
  %11 = load i32, ptr getelementptr inbounds (%struct.log_module_t, ptr @log, i32 0, i32 3), align 4, !tbaa !15
  %12 = call i64 @lseek(i32 noundef %11, i64 noundef 0, i32 noundef 0)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %26, %10
  %14 = load i32, ptr getelementptr inbounds (%struct.log_module_t, ptr @log, i32 0, i32 3), align 4, !tbaa !15
  %15 = load ptr, ptr @log, align 8, !tbaa !16
  %16 = load i64, ptr getelementptr inbounds (%struct.log_module_t, ptr @log, i32 0, i32 1), align 8, !tbaa !17
  %17 = call i32 @read(i32 noundef %14, ptr noundef %15, i64 noundef %16)
  store i32 %17, ptr %5, align 4, !tbaa !5
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = icmp sgt i32 %18, 0
  br i1 %19, label %20, label %25

20:                                               ; preds = %13
  %21 = load ptr, ptr @log, align 8, !tbaa !16
  %22 = load i32, ptr %5, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = call i32 @write(i32 noundef 1, ptr noundef %21, i64 noundef %23)
  br label %25

25:                                               ; preds = %20, %13
  br label %26

26:                                               ; preds = %25
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = load i64, ptr getelementptr inbounds (%struct.log_module_t, ptr @log, i32 0, i32 1), align 8, !tbaa !17
  %30 = icmp eq i64 %28, %29
  br i1 %30, label %13, label %31, !llvm.loop !18

31:                                               ; preds = %26
  %32 = load i32, ptr getelementptr inbounds (%struct.log_module_t, ptr @log, i32 0, i32 3), align 4, !tbaa !15
  %33 = call i64 @lseek(i32 noundef %32, i64 noundef 0, i32 noundef 2)
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %39

34:                                               ; preds = %2
  %35 = load ptr, ptr @log, align 8, !tbaa !16
  %36 = load i32, ptr getelementptr inbounds (%struct.log_module_t, ptr @log, i32 0, i32 2), align 8, !tbaa !20
  %37 = zext i32 %36 to i64
  %38 = call i32 @write(i32 noundef 1, ptr noundef %35, i64 noundef %37)
  br label %39

39:                                               ; preds = %34, %31
  %40 = load ptr, ptr getelementptr inbounds (%struct.log_module_t, ptr @log, i32 0, i32 4), align 8, !tbaa !11
  %41 = call i64 @xQueueGenericSend(ptr noundef %40, ptr noundef null, i32 noundef 0, i64 noundef 0)
  ret i32 0
}

declare i64 @lseek(i32 noundef, i64 noundef, i32 noundef) #1

declare i32 @read(i32 noundef, ptr noundef, i64 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !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 24}
!12 = !{!"", !10, i64 0, !13, i64 8, !6, i64 16, !6, i64 20, !10, i64 24, !6, i64 32}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !6, i64 32}
!15 = !{!12, !6, i64 20}
!16 = !{!12, !10, i64 0}
!17 = !{!12, !13, i64 8}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!12, !6, i64 16}


; 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

declare i64 @xQueueSemaphoreTake(ptr noundef, i32 noundef) local_unnamed_addr #0

declare i32 @write(i32 noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

declare i64 @xQueueGenericSend(ptr noundef, ptr noundef, i32 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @log_print(i32 noundef %0, ptr nocapture noundef readnone %1) local_unnamed_addr #1 {
  %3 = load ptr, ptr getelementptr inbounds (i8, ptr @log, i64 24), align 8, !tbaa !5
  %4 = tail call i64 @xQueueSemaphoreTake(ptr noundef %3, i32 noundef -1) #2
  %5 = load i32, ptr getelementptr inbounds (i8, ptr @log, i64 32), align 8, !tbaa !12
  %6 = icmp eq i32 %5, 1
  br i1 %6, label %7, label %27

7:                                                ; preds = %2
  %8 = load i32, ptr getelementptr inbounds (i8, ptr @log, i64 20), align 4, !tbaa !13
  %9 = tail call i64 @lseek(i32 noundef %8, i64 noundef 0, i32 noundef 0) #2
  %.pre = load i64, ptr getelementptr inbounds (i8, ptr @log, i64 8), align 8, !tbaa !14
  br label %10

10:                                               ; preds = %20, %7
  %11 = phi i64 [ %21, %20 ], [ %.pre, %7 ]
  %12 = load i32, ptr getelementptr inbounds (i8, ptr @log, i64 20), align 4, !tbaa !13
  %13 = load ptr, ptr @log, align 8, !tbaa !15
  %14 = tail call i32 @read(i32 noundef %12, ptr noundef %13, i64 noundef %11) #2
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %16, label %20

16:                                               ; preds = %10
  %17 = load ptr, ptr @log, align 8, !tbaa !15
  %18 = zext nneg i32 %14 to i64
  %19 = tail call i32 @write(i32 noundef 1, ptr noundef %17, i64 noundef %18) #2
  br label %20

20:                                               ; preds = %10, %16
  %21 = sext i32 %14 to i64
  %22 = load i64, ptr getelementptr inbounds (i8, ptr @log, i64 8), align 8, !tbaa !14
  %23 = icmp eq i64 %22, %21
  br i1 %23, label %10, label %24, !llvm.loop !16

24:                                               ; preds = %20
  %25 = load i32, ptr getelementptr inbounds (i8, ptr @log, i64 20), align 4, !tbaa !13
  %26 = tail call i64 @lseek(i32 noundef %25, i64 noundef 0, i32 noundef 2) #2
  br label %32

27:                                               ; preds = %2
  %28 = load ptr, ptr @log, align 8, !tbaa !15
  %29 = load i32, ptr getelementptr inbounds (i8, ptr @log, i64 16), align 8, !tbaa !18
  %30 = zext i32 %29 to i64
  %31 = tail call i32 @write(i32 noundef 1, ptr noundef %28, i64 noundef %30) #2
  br label %32

32:                                               ; preds = %27, %24
  %33 = load ptr, ptr getelementptr inbounds (i8, ptr @log, i64 24), align 8, !tbaa !5
  %34 = tail call i64 @xQueueGenericSend(ptr noundef %33, ptr noundef null, i32 noundef 0, i64 noundef 0) #2
  ret i32 0
}

declare i64 @lseek(i32 noundef, i64 noundef, i32 noundef) local_unnamed_addr #0

declare i32 @read(i32 noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

attributes #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 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, !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, !11, i64 32}
!13 = !{!6, !11, i64 20}
!14 = !{!6, !10, i64 8}
!15 = !{!6, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!6, !11, i64 16}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass