; LLVM IR from mhsitu/welding_robot :: common/sys_log.ll (ZN9LogBuff_t12write_stringEPKc)

; 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.LogBuff_t = type { [258 x i8], i16 }

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_ZN9LogBuff_t12write_stringEPKc(ptr noundef nonnull align 2 dereferenceable(260) %0, ptr noundef %1) #0 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %5 = load ptr, ptr %3, align 8
  br label %6

6:                                                ; preds = %29, %2
  %7 = load ptr, ptr %4, align 8, !tbaa !5
  %8 = load i8, ptr %7, align 1, !tbaa !9
  %9 = sext i8 %8 to i32
  %10 = icmp ne i32 %9, 0
  br i1 %10, label %11, label %30

11:                                               ; preds = %6
  %12 = getelementptr inbounds %struct.LogBuff_t, ptr %5, i32 0, i32 1
  %13 = load i16, ptr %12, align 2, !tbaa !10
  %14 = zext i16 %13 to i32
  %15 = icmp sle i32 %14, 256
  br i1 %15, label %16, label %28

16:                                               ; preds = %11
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %17, i32 1
  store ptr %18, ptr %4, align 8, !tbaa !5
  %19 = load i8, ptr %17, align 1, !tbaa !9
  %20 = getelementptr inbounds %struct.LogBuff_t, ptr %5, i32 0, i32 0
  %21 = getelementptr inbounds %struct.LogBuff_t, ptr %5, i32 0, i32 1
  %22 = load i16, ptr %21, align 2, !tbaa !10
  %23 = zext i16 %22 to i64
  %24 = getelementptr inbounds [258 x i8], ptr %20, i64 0, i64 %23
  store i8 %19, ptr %24, align 1, !tbaa !9
  %25 = getelementptr inbounds %struct.LogBuff_t, ptr %5, i32 0, i32 1
  %26 = load i16, ptr %25, align 2, !tbaa !10
  %27 = add i16 %26, 1
  store i16 %27, ptr %25, align 2, !tbaa !10
  br label %29

28:                                               ; preds = %11
  br label %30

29:                                               ; preds = %16
  br label %6, !llvm.loop !13

30:                                               ; preds = %28, %6
  %31 = getelementptr inbounds %struct.LogBuff_t, ptr %5, i32 0, i32 1
  %32 = load i16, ptr %31, align 2, !tbaa !10
  %33 = zext i16 %32 to i64
  ret i64 %33
}

attributes #0 = { mustprogress 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !12, i64 258}
!11 = !{!"_ZTS9LogBuff_t", !7, i64 0, !12, i64 258}
!12 = !{!"short", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local noundef range(i64 0, 65536) i64 @_ZN9LogBuff_t12write_stringEPKc(ptr nocapture noundef nonnull align 2 dereferenceable(260) %0, ptr nocapture noundef readonly %1) local_unnamed_addr #0 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 258
  %4 = load i8, ptr %1, align 1, !tbaa !5
  %.not3 = icmp eq i8 %4, 0
  %.pre6 = load i16, ptr %3, align 2, !tbaa !8
  br i1 %.not3, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2, %8
  %5 = phi i16 [ %13, %8 ], [ %.pre6, %2 ]
  %6 = phi i8 [ %14, %8 ], [ %4, %2 ]
  %.04 = phi ptr [ %9, %8 ], [ %1, %2 ]
  %7 = icmp ult i16 %5, 257
  br i1 %7, label %8, label %._crit_edge

8:                                                ; preds = %.lr.ph
  %9 = getelementptr inbounds i8, ptr %.04, i64 1
  %10 = zext nneg i16 %5 to i64
  %11 = getelementptr inbounds [258 x i8], ptr %0, i64 0, i64 %10
  store i8 %6, ptr %11, align 1, !tbaa !5
  %12 = load i16, ptr %3, align 2, !tbaa !8
  %13 = add i16 %12, 1
  store i16 %13, ptr %3, align 2, !tbaa !8
  %14 = load i8, ptr %9, align 1, !tbaa !5
  %.not = icmp eq i8 %14, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !11

._crit_edge:                                      ; preds = %8, %.lr.ph, %2
  %15 = phi i16 [ %.pre6, %2 ], [ %13, %8 ], [ %5, %.lr.ph ]
  %16 = zext i16 %15 to i64
  ret i64 %16
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) 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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !10, i64 258}
!9 = !{!"_ZTS9LogBuff_t", !6, i64 0, !10, i64 258}
!10 = !{!"short", !6, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass