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

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

; 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 i32 @dht_read(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %12 = call i32 @dh_onewire_get_pin()
  %13 = zext i32 %12 to i64
  %14 = shl i64 1, %13
  %15 = trunc i64 %14 to i32
  store i32 %15, ptr %6, align 4, !tbaa !9
  call void @ets_intr_lock()
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = call i32 @dh_onewire_reset(i32 noundef %16, i32 noundef 25000, i32 noundef 1)
  %18 = icmp eq i32 0, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %2
  call void @ets_intr_unlock()
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %84

20:                                               ; preds = %2
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = call i32 @dht_measure_high(i32 noundef %21)
  %23 = icmp uge i32 %22, 200
  br i1 %23, label %24, label %25

24:                                               ; preds = %20
  call void @ets_intr_unlock()
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %84

25:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %26, ptr %9, align 8, !tbaa !5
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %27

27:                                               ; preds = %78, %25
  %28 = load i32, ptr %8, align 4, !tbaa !9
  %29 = udiv i32 %28, 8
  %30 = load i32, ptr %5, align 4, !tbaa !9
  %31 = icmp ult i32 %29, %30
  br i1 %31, label %32, label %81

32:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %33 = load i32, ptr %6, align 4, !tbaa !9
  %34 = call i32 @dht_measure_high(i32 noundef %33)
  store i32 %34, ptr %10, align 4, !tbaa !9
  %35 = load i32, ptr %10, align 4, !tbaa !9
  %36 = icmp uge i32 %35, 200
  br i1 %36, label %40, label %37

37:                                               ; preds = %32
  %38 = load i32, ptr %10, align 4, !tbaa !9
  %39 = icmp ule i32 %38, 10
  br i1 %39, label %40, label %41

40:                                               ; preds = %37, %32
  store i32 2, ptr %7, align 4
  br label %75

41:                                               ; preds = %37
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %42 = load i32, ptr %8, align 4, !tbaa !9
  %43 = urem i32 %42, 8
  %44 = sub i32 7, %43
  %45 = zext i32 %44 to i64
  %46 = shl i64 1, %45
  %47 = trunc i64 %46 to i32
  store i32 %47, ptr %11, align 4, !tbaa !9
  %48 = load i32, ptr %10, align 4, !tbaa !9
  %49 = icmp ugt i32 %48, 25
  br i1 %49, label %50, label %61

50:                                               ; preds = %41
  %51 = load i32, ptr %11, align 4, !tbaa !9
  %52 = load ptr, ptr %9, align 8, !tbaa !5
  %53 = load i32, ptr %8, align 4, !tbaa !9
  %54 = udiv i32 %53, 8
  %55 = zext i32 %54 to i64
  %56 = getelementptr inbounds i8, ptr %52, i64 %55
  %57 = load i8, ptr %56, align 1, !tbaa !11
  %58 = zext i8 %57 to i32
  %59 = or i32 %58, %51
  %60 = trunc i32 %59 to i8
  store i8 %60, ptr %56, align 1, !tbaa !11
  br label %73

61:                                               ; preds = %41
  %62 = load i32, ptr %11, align 4, !tbaa !9
  %63 = xor i32 %62, -1
  %64 = load ptr, ptr %9, align 8, !tbaa !5
  %65 = load i32, ptr %8, align 4, !tbaa !9
  %66 = udiv i32 %65, 8
  %67 = zext i32 %66 to i64
  %68 = getelementptr inbounds i8, ptr %64, i64 %67
  %69 = load i8, ptr %68, align 1, !tbaa !11
  %70 = zext i8 %69 to i32
  %71 = and i32 %70, %63
  %72 = trunc i32 %71 to i8
  store i8 %72, ptr %68, align 1, !tbaa !11
  br label %73

73:                                               ; preds = %61, %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  br label %74

74:                                               ; preds = %73
  call void @system_soft_wdt_feed()
  store i32 0, ptr %7, align 4
  br label %75

75:                                               ; preds = %74, %40
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  %76 = load i32, ptr %7, align 4
  switch i32 %76, label %86 [
    i32 0, label %77
    i32 2, label %81
  ]

77:                                               ; preds = %75
  br label %78

78:                                               ; preds = %77
  %79 = load i32, ptr %8, align 4, !tbaa !9
  %80 = add i32 %79, 1
  store i32 %80, ptr %8, align 4, !tbaa !9
  br label %27, !llvm.loop !12

81:                                               ; preds = %75, %27
  call void @ets_intr_unlock()
  %82 = load i32, ptr %8, align 4, !tbaa !9
  %83 = udiv i32 %82, 8
  store i32 %83, ptr %3, align 4
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %84

84:                                               ; preds = %81, %24, %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %85 = load i32, ptr %3, align 4
  ret i32 %85

86:                                               ; preds = %75
  unreachable
}

declare i32 @dh_onewire_get_pin() #2

declare void @ets_intr_lock() #2

declare i32 @dh_onewire_reset(i32 noundef, i32 noundef, i32 noundef) #2

declare void @ets_intr_unlock() #2

; Function Attrs: nounwind uwtable
declare hidden i32 @dht_measure_high(i32 noundef) #1

declare void @system_soft_wdt_feed() #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"

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 536870912) i32 @dht_read(ptr nocapture noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call i32 @dh_onewire_get_pin() #2
  %4 = zext nneg i32 %3 to i64
  %5 = shl nuw i64 1, %4
  %6 = trunc i64 %5 to i32
  tail call void @ets_intr_lock() #2
  %7 = tail call i32 @dh_onewire_reset(i32 noundef %6, i32 noundef 25000, i32 noundef 1) #2
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %._crit_edge, label %9

9:                                                ; preds = %2
  %10 = tail call i32 @dht_measure_high(i32 noundef %6)
  %11 = icmp ugt i32 %10, 199
  %.not = icmp eq i32 %1, 0
  %or.cond29 = or i1 %11, %.not
  br i1 %or.cond29, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %9, %15
  %12 = phi i32 [ %28, %15 ], [ 0, %9 ]
  %.02126 = phi i32 [ %27, %15 ], [ 0, %9 ]
  %13 = tail call i32 @dht_measure_high(i32 noundef %6)
  %14 = add i32 %13, -200
  %or.cond = icmp ult i32 %14, -189
  br i1 %or.cond, label %._crit_edge, label %15

15:                                               ; preds = %.lr.ph
  %16 = and i32 %.02126, 7
  %17 = xor i32 %16, 7
  %18 = shl nuw nsw i32 1, %17
  %19 = icmp ugt i32 %13, 25
  %20 = zext nneg i32 %12 to i64
  %21 = getelementptr inbounds i8, ptr %0, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !5
  %23 = trunc nuw i32 %18 to i8
  %24 = or i8 %22, %23
  %25 = xor i8 %23, -1
  %26 = and i8 %22, %25
  %.sink = select i1 %19, i8 %24, i8 %26
  store i8 %.sink, ptr %21, align 1, !tbaa !5
  tail call void @system_soft_wdt_feed() #2
  %27 = add i32 %.02126, 1
  %28 = lshr i32 %27, 3
  %29 = icmp ult i32 %28, %1
  br i1 %29, label %.lr.ph, label %._crit_edge, !llvm.loop !8

._crit_edge:                                      ; preds = %.lr.ph, %15, %9, %2
  %.0 = phi i32 [ 0, %2 ], [ 0, %9 ], [ %12, %.lr.ph ], [ %28, %15 ]
  tail call void @ets_intr_unlock() #2
  ret i32 %.0
}

declare i32 @dh_onewire_get_pin() local_unnamed_addr #1

declare void @ets_intr_lock() local_unnamed_addr #1

declare i32 @dh_onewire_reset(i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

declare void @ets_intr_unlock() local_unnamed_addr #1

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

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


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