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

; 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._ETSTIMER_ = type { ptr, i32, i32, ptr, ptr }

@mKeepLED = external hidden global i32, align 4
@mDataMode = external hidden global i32, align 4
@mRecoverLEDTimer = external hidden global %struct._ETSTIMER_, align 8

; Function Attrs: nounwind uwtable
declare hidden void @led_recover(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @led_off() #0

declare void @ets_timer_disarm(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @send_char(i32 noundef) #0

declare void @ets_timer_setfn(ptr noundef, ptr noundef, ptr noundef) #1

declare void @ets_timer_arm_new(ptr noundef, i32 noundef, i8 noundef zeroext, i8 noundef zeroext) #1

; Function Attrs: nounwind uwtable
define dso_local void @dh_uart_send_buf(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %6 = load i32, ptr @mDataMode, align 4, !tbaa !9
  %7 = icmp ne i32 %6, 2
  br i1 %7, label %8, label %9

8:                                                ; preds = %2
  br label %29

9:                                                ; preds = %2
  %10 = load i32, ptr @mKeepLED, align 4, !tbaa !9
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %9
  call void @ets_timer_disarm(ptr noundef @mRecoverLEDTimer)
  call void @led_off()
  br label %13

13:                                               ; preds = %12, %9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %14, ptr %5, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %19, %13
  %16 = load i32, ptr %4, align 4, !tbaa !9
  %17 = add i32 %16, -1
  store i32 %17, ptr %4, align 4, !tbaa !9
  %18 = icmp ne i32 %16, 0
  br i1 %18, label %19, label %24

19:                                               ; preds = %15
  call void @system_soft_wdt_feed()
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %20, i32 1
  store ptr %21, ptr %5, align 8, !tbaa !5
  %22 = load i8, ptr %20, align 1, !tbaa !11
  %23 = sext i8 %22 to i32
  call void @send_char(i32 noundef %23)
  br label %15, !llvm.loop !12

24:                                               ; preds = %15
  %25 = load i32, ptr @mKeepLED, align 4, !tbaa !9
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %28

27:                                               ; preds = %24
  call void @ets_timer_setfn(ptr noundef @mRecoverLEDTimer, ptr noundef @led_recover, ptr noundef null)
  call void @ets_timer_arm_new(ptr noundef @mRecoverLEDTimer, i32 noundef 20, i8 noundef zeroext 0, i8 noundef zeroext 1)
  br label %28

28:                                               ; preds = %27, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  br label %29

29:                                               ; preds = %28, %8
  ret void
}

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

declare void @system_soft_wdt_feed() #1

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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"

%struct._ETSTIMER_ = type { ptr, i32, i32, ptr, ptr }

@mKeepLED = external hidden local_unnamed_addr global i32, align 4
@mDataMode = external hidden local_unnamed_addr global i32, align 4
@mRecoverLEDTimer = external hidden global %struct._ETSTIMER_, align 8

; Function Attrs: nounwind uwtable
declare hidden void @led_recover(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @led_off() local_unnamed_addr #0

declare void @ets_timer_disarm(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @send_char(i32 noundef) local_unnamed_addr #0

declare void @ets_timer_setfn(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

declare void @ets_timer_arm_new(ptr noundef, i32 noundef, i8 noundef zeroext, i8 noundef zeroext) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @dh_uart_send_buf(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load i32, ptr @mDataMode, align 4, !tbaa !5
  %.not = icmp eq i32 %3, 2
  br i1 %.not, label %4, label %14

4:                                                ; preds = %2
  %5 = load i32, ptr @mKeepLED, align 4, !tbaa !5
  %.not3 = icmp eq i32 %5, 0
  br i1 %.not3, label %7, label %6

6:                                                ; preds = %4
  tail call void @ets_timer_disarm(ptr noundef nonnull @mRecoverLEDTimer) #2
  tail call void @led_off()
  br label %7

7:                                                ; preds = %6, %4
  %.not46 = icmp eq i32 %1, 0
  br i1 %.not46, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %7, %.lr.ph
  %.08 = phi ptr [ %9, %.lr.ph ], [ %0, %7 ]
  %.027 = phi i32 [ %8, %.lr.ph ], [ %1, %7 ]
  %8 = add i32 %.027, -1
  tail call void @system_soft_wdt_feed() #2
  %9 = getelementptr inbounds i8, ptr %.08, i64 1
  %10 = load i8, ptr %.08, align 1, !tbaa !9
  %11 = sext i8 %10 to i32
  tail call void @send_char(i32 noundef %11)
  %.not4 = icmp eq i32 %8, 0
  br i1 %.not4, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph, %7
  %12 = load i32, ptr @mKeepLED, align 4, !tbaa !5
  %.not5 = icmp eq i32 %12, 0
  br i1 %.not5, label %14, label %13

13:                                               ; preds = %._crit_edge
  tail call void @ets_timer_setfn(ptr noundef nonnull @mRecoverLEDTimer, ptr noundef nonnull @led_recover, ptr noundef null) #2
  tail call void @ets_timer_arm_new(ptr noundef nonnull @mRecoverLEDTimer, i32 noundef 20, i8 noundef zeroext 0, i8 noundef zeroext 1) #2
  br label %14

14:                                               ; preds = %._crit_edge, %13, %2
  ret void
}

declare void @system_soft_wdt_feed() 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: GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass