; LLVM IR from devicehive/esp8266-firmware :: firmware-src/sources (dhterminal)

; 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"

@mDebugBuff = external hidden global [1024 x i8], align 16
@mMode = external hidden global i32, align 4
@mDebugBuffPos = external hidden global i32, align 4

; 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 void @dhterminal_debug(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %7 = load i32, ptr @mDebugBuffPos, align 4, !tbaa !9
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds [1024 x i8], ptr @mDebugBuff, i64 0, i64 %8
  %10 = load i32, ptr @mDebugBuffPos, align 4, !tbaa !9
  %11 = sext i32 %10 to i64
  %12 = sub i64 1024, %11
  %13 = sub i64 %12, 2
  %14 = trunc i64 %13 to i32
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = call i32 @vsnprintf(ptr noundef %9, i32 noundef %14, ptr noundef %15, ptr noundef %16)
  store i32 %17, ptr %5, align 4, !tbaa !9
  %18 = load i32, ptr @mMode, align 4, !tbaa !9
  %19 = icmp eq i32 %18, 1
  br i1 %19, label %20, label %24

20:                                               ; preds = %2
  %21 = load i32, ptr @mDebugBuffPos, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [1024 x i8], ptr @mDebugBuff, i64 0, i64 %22
  call void @dh_uart_send_line(ptr noundef %23)
  br label %72

24:                                               ; preds = %2
  %25 = load i32, ptr %5, align 4, !tbaa !9
  %26 = load i32, ptr @mDebugBuffPos, align 4, !tbaa !9
  %27 = add nsw i32 %26, %25
  store i32 %27, ptr @mDebugBuffPos, align 4, !tbaa !9
  %28 = load i32, ptr @mDebugBuffPos, align 4, !tbaa !9
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr @mDebugBuffPos, align 4, !tbaa !9
  %30 = sext i32 %28 to i64
  %31 = getelementptr inbounds [1024 x i8], ptr @mDebugBuff, i64 0, i64 %30
  store i8 13, ptr %31, align 1, !tbaa !11
  %32 = load i32, ptr @mDebugBuffPos, align 4, !tbaa !9
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr @mDebugBuffPos, align 4, !tbaa !9
  %34 = sext i32 %32 to i64
  %35 = getelementptr inbounds [1024 x i8], ptr @mDebugBuff, i64 0, i64 %34
  store i8 10, ptr %35, align 1, !tbaa !11
  %36 = load i32, ptr @mDebugBuffPos, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [1024 x i8], ptr @mDebugBuff, i64 0, i64 %37
  store i8 0, ptr %38, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %39 = load i32, ptr @mDebugBuffPos, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = sub i64 1024, %40
  %42 = sub i64 %41, 2
  %43 = sub i64 128, %42
  %44 = trunc i64 %43 to i32
  store i32 %44, ptr %6, align 4, !tbaa !9
  %45 = load i32, ptr %6, align 4, !tbaa !9
  %46 = icmp sgt i32 %45, 0
  br i1 %46, label %47, label %71

47:                                               ; preds = %24
  br label %48

48:                                               ; preds = %56, %47
  %49 = load i32, ptr %6, align 4, !tbaa !9
  %50 = sub nsw i32 %49, 1
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [1024 x i8], ptr @mDebugBuff, i64 0, i64 %51
  %53 = load i8, ptr %52, align 1, !tbaa !11
  %54 = sext i8 %53 to i32
  %55 = icmp ne i32 %54, 10
  br i1 %55, label %56, label %59

56:                                               ; preds = %48
  %57 = load i32, ptr %6, align 4, !tbaa !9
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %6, align 4, !tbaa !9
  br label %48, !llvm.loop !12

59:                                               ; preds = %48
  %60 = load i32, ptr %6, align 4, !tbaa !9
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [1024 x i8], ptr @mDebugBuff, i64 0, i64 %61
  %63 = load i32, ptr %6, align 4, !tbaa !9
  %64 = sext i32 %63 to i64
  %65 = sub i64 1024, %64
  %66 = trunc i64 %65 to i32
  %67 = call ptr @ets_memmove(ptr noundef @mDebugBuff, ptr noundef %62, i32 noundef %66)
  %68 = load i32, ptr %6, align 4, !tbaa !9
  %69 = load i32, ptr @mDebugBuffPos, align 4, !tbaa !9
  %70 = sub nsw i32 %69, %68
  store i32 %70, ptr @mDebugBuffPos, align 4, !tbaa !9
  br label %71

71:                                               ; preds = %59, %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %72

72:                                               ; preds = %71, %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret void
}

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

declare void @dh_uart_send_line(ptr noundef) #2

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

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 = { "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 = !{!"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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

@mDebugBuff = external hidden global [1024 x i8], align 16
@mMode = external hidden local_unnamed_addr global i32, align 4
@mDebugBuffPos = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @dhterminal_debug(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = load i32, ptr @mDebugBuffPos, align 4, !tbaa !5
  %4 = sext i32 %3 to i64
  %5 = getelementptr inbounds [1024 x i8], ptr @mDebugBuff, i64 0, i64 %4
  %6 = sub i32 1022, %3
  %7 = tail call i32 @vsnprintf(ptr noundef nonnull %5, i32 noundef %6, ptr noundef %0, ptr noundef %1) #2
  %8 = load i32, ptr @mMode, align 4, !tbaa !5
  %9 = icmp eq i32 %8, 1
  %10 = load i32, ptr @mDebugBuffPos, align 4, !tbaa !5
  br i1 %9, label %11, label %14

11:                                               ; preds = %2
  %12 = sext i32 %10 to i64
  %13 = getelementptr inbounds [1024 x i8], ptr @mDebugBuff, i64 0, i64 %12
  tail call void @dh_uart_send_line(ptr noundef nonnull %13) #2
  br label %39

14:                                               ; preds = %2
  %15 = add nsw i32 %10, %7
  %16 = add nsw i32 %15, 1
  %17 = sext i32 %15 to i64
  %18 = getelementptr inbounds [1024 x i8], ptr @mDebugBuff, i64 0, i64 %17
  store i8 13, ptr %18, align 1, !tbaa !9
  %19 = add nsw i32 %15, 2
  store i32 %19, ptr @mDebugBuffPos, align 4, !tbaa !5
  %20 = sext i32 %16 to i64
  %21 = getelementptr inbounds [1024 x i8], ptr @mDebugBuff, i64 0, i64 %20
  store i8 10, ptr %21, align 1, !tbaa !9
  %22 = sext i32 %19 to i64
  %23 = getelementptr inbounds [1024 x i8], ptr @mDebugBuff, i64 0, i64 %22
  store i8 0, ptr %23, align 1, !tbaa !9
  %24 = add i32 %15, -892
  %25 = icmp sgt i32 %24, 0
  br i1 %25, label %.preheader.preheader, label %39

.preheader.preheader:                             ; preds = %14
  %26 = zext nneg i32 %24 to i64
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %.preheader
  %indvars.iv = phi i64 [ %26, %.preheader.preheader ], [ %indvars.iv.next, %.preheader ]
  %27 = add nuw i64 %indvars.iv, 4294967295
  %28 = and i64 %27, 4294967295
  %29 = getelementptr inbounds [1024 x i8], ptr @mDebugBuff, i64 0, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !9
  %.not = icmp eq i8 %30, 10
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  br i1 %.not, label %31, label %.preheader, !llvm.loop !10

31:                                               ; preds = %.preheader
  %32 = trunc nuw i64 %indvars.iv to i32
  %33 = and i64 %indvars.iv, 4294967295
  %34 = getelementptr inbounds [1024 x i8], ptr @mDebugBuff, i64 0, i64 %33
  %35 = sub i32 1024, %32
  %36 = tail call ptr @ets_memmove(ptr noundef nonnull @mDebugBuff, ptr noundef nonnull %34, i32 noundef %35) #2
  %37 = load i32, ptr @mDebugBuffPos, align 4, !tbaa !5
  %38 = sub nsw i32 %37, %32
  store i32 %38, ptr @mDebugBuffPos, align 4, !tbaa !5
  br label %39

39:                                               ; preds = %14, %31, %11
  ret void
}

declare i32 @vsnprintf(ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

declare void @dh_uart_send_line(ptr noundef) local_unnamed_addr #1

declare ptr @ets_memmove(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

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 = { "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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