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

; 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: nounwind uwtable
define dso_local i32 @dh_onewire_reset(i32 noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  call void @system_soft_wdt_feed()
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %12 = call i32 @gpio_input_get()
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = and i32 %12, %13
  %15 = icmp ne i32 %14, 0
  %16 = zext i1 %15 to i32
  store i32 %16, ptr %8, align 4, !tbaa !5
  %17 = load i32, ptr %5, align 4, !tbaa !5
  call void @dh_gpio_open_drain(i32 noundef %17, i32 noundef 0)
  %18 = load i32, ptr %5, align 4, !tbaa !5
  call void @dh_gpio_pull_up(i32 noundef %18, i32 noundef 0)
  %19 = load i32, ptr %5, align 4, !tbaa !5
  call void @dh_gpio_prepare_pins(i32 noundef %19, i32 noundef 1)
  %20 = load i32, ptr %8, align 4, !tbaa !5
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %25, label %22

22:                                               ; preds = %3
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = load i32, ptr %5, align 4, !tbaa !5
  call void @gpio_output_set(i32 noundef %23, i32 noundef 0, i32 noundef %24, i32 noundef 0)
  call void @ets_delay_us(i32 noundef 500)
  br label %25

25:                                               ; preds = %22, %3
  %26 = call i32 @gpio_input_get()
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = and i32 %26, %27
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %31, label %30

30:                                               ; preds = %25
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %80

31:                                               ; preds = %25
  %32 = load i32, ptr %5, align 4, !tbaa !5
  %33 = load i32, ptr %5, align 4, !tbaa !5
  call void @gpio_output_set(i32 noundef 0, i32 noundef %32, i32 noundef %33, i32 noundef 0)
  %34 = load i32, ptr %6, align 4, !tbaa !5
  call void @ets_delay_us(i32 noundef %34)
  %35 = load i32, ptr %5, align 4, !tbaa !5
  %36 = load i32, ptr %5, align 4, !tbaa !5
  call void @gpio_output_set(i32 noundef %35, i32 noundef 0, i32 noundef %36, i32 noundef 0)
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !5
  store i32 0, ptr %10, align 4, !tbaa !5
  br label %37

37:                                               ; preds = %74, %31
  %38 = load i32, ptr %10, align 4, !tbaa !5
  %39 = icmp slt i32 %38, 480
  br i1 %39, label %40, label %77

40:                                               ; preds = %37
  %41 = load i32, ptr %11, align 4, !tbaa !5
  %42 = icmp ne i32 %41, 0
  br i1 %42, label %57, label %43

43:                                               ; preds = %40
  %44 = load i32, ptr %10, align 4, !tbaa !5
  %45 = icmp slt i32 %44, 240
  br i1 %45, label %46, label %57

46:                                               ; preds = %43
  %47 = load i32, ptr %10, align 4, !tbaa !5
  %48 = icmp sgt i32 %47, 15
  br i1 %48, label %49, label %56

49:                                               ; preds = %46
  %50 = call i32 @gpio_input_get()
  %51 = load i32, ptr %5, align 4, !tbaa !5
  %52 = and i32 %50, %51
  %53 = icmp ne i32 %52, 0
  br i1 %53, label %55, label %54

54:                                               ; preds = %49
  store i32 1, ptr %11, align 4, !tbaa !5
  br label %55

55:                                               ; preds = %54, %49
  br label %56

56:                                               ; preds = %55, %46
  br label %73

57:                                               ; preds = %43, %40
  %58 = load i32, ptr %7, align 4, !tbaa !5
  %59 = icmp ne i32 %58, 0
  br i1 %59, label %60, label %72

60:                                               ; preds = %57
  %61 = load i32, ptr %11, align 4, !tbaa !5
  %62 = icmp ne i32 %61, 0
  br i1 %62, label %63, label %70

63:                                               ; preds = %60
  %64 = call i32 @gpio_input_get()
  %65 = load i32, ptr %5, align 4, !tbaa !5
  %66 = and i32 %64, %65
  %67 = icmp ne i32 %66, 0
  br i1 %67, label %68, label %69

68:                                               ; preds = %63
  store i32 1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %79

69:                                               ; preds = %63
  br label %71

70:                                               ; preds = %60
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %79

71:                                               ; preds = %69
  br label %72

72:                                               ; preds = %71, %57
  br label %73

73:                                               ; preds = %72, %56
  call void @ets_delay_us(i32 noundef 1)
  br label %74

74:                                               ; preds = %73
  %75 = load i32, ptr %10, align 4, !tbaa !5
  %76 = add nsw i32 %75, 1
  store i32 %76, ptr %10, align 4, !tbaa !5
  br label %37, !llvm.loop !9

77:                                               ; preds = %37
  %78 = load i32, ptr %11, align 4, !tbaa !5
  store i32 %78, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %79

79:                                               ; preds = %77, %70, %68
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %80

80:                                               ; preds = %79, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %81 = load i32, ptr %4, align 4
  ret i32 %81
}

declare void @system_soft_wdt_feed() #1

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

declare i32 @gpio_input_get() #1

declare void @dh_gpio_open_drain(i32 noundef, i32 noundef) #1

declare void @dh_gpio_pull_up(i32 noundef, i32 noundef) #1

declare void @dh_gpio_prepare_pins(i32 noundef, i32 noundef) #1

declare void @gpio_output_set(i32 noundef, i32 noundef, i32 noundef, i32 noundef) #1

declare void @ets_delay_us(i32 noundef) #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"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, 2) i32 @dh_onewire_reset(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  tail call void @system_soft_wdt_feed() #2
  %4 = tail call i32 @gpio_input_get() #2
  %5 = and i32 %4, %0
  %.not = icmp eq i32 %5, 0
  tail call void @dh_gpio_open_drain(i32 noundef %0, i32 noundef 0) #2
  tail call void @dh_gpio_pull_up(i32 noundef %0, i32 noundef 0) #2
  tail call void @dh_gpio_prepare_pins(i32 noundef %0, i32 noundef 1) #2
  br i1 %.not, label %6, label %7

6:                                                ; preds = %3
  tail call void @gpio_output_set(i32 noundef %0, i32 noundef 0, i32 noundef %0, i32 noundef 0) #2
  tail call void @ets_delay_us(i32 noundef 500) #2
  br label %7

7:                                                ; preds = %6, %3
  %8 = tail call i32 @gpio_input_get() #2
  %9 = and i32 %8, %0
  %.not28 = icmp eq i32 %9, 0
  br i1 %.not28, label %.loopexit, label %10

10:                                               ; preds = %7
  tail call void @gpio_output_set(i32 noundef 0, i32 noundef %0, i32 noundef %0, i32 noundef 0) #2
  tail call void @ets_delay_us(i32 noundef %1) #2
  tail call void @gpio_output_set(i32 noundef %0, i32 noundef 0, i32 noundef %0, i32 noundef 0) #2
  %.not29 = icmp eq i32 %2, 0
  br i1 %.not29, label %.split.us, label %.split

.split.us:                                        ; preds = %10, %18
  %.034.us = phi i32 [ %.1.us, %18 ], [ 0, %10 ]
  %.02533.us = phi i32 [ %19, %18 ], [ 0, %10 ]
  %11 = icmp eq i32 %.034.us, 0
  %12 = icmp ult i32 %.02533.us, 240
  %or.cond.us = and i1 %12, %11
  br i1 %or.cond.us, label %13, label %18

13:                                               ; preds = %.split.us
  %14 = icmp ugt i32 %.02533.us, 15
  br i1 %14, label %15, label %18

15:                                               ; preds = %13
  %16 = tail call i32 @gpio_input_get() #2
  %17 = and i32 %16, %0
  %.not32.us = icmp eq i32 %17, 0
  %spec.select.us = zext i1 %.not32.us to i32
  br label %18

18:                                               ; preds = %.split.us, %15, %13
  %.1.us = phi i32 [ 0, %13 ], [ %spec.select.us, %15 ], [ %.034.us, %.split.us ]
  tail call void @ets_delay_us(i32 noundef 1) #2
  %19 = add nuw nsw i32 %.02533.us, 1
  %exitcond36.not = icmp eq i32 %19, 480
  br i1 %exitcond36.not, label %.loopexit, label %.split.us, !llvm.loop !5

.split:                                           ; preds = %10, %31
  %.034 = phi i32 [ %.1, %31 ], [ 0, %10 ]
  %.02533 = phi i32 [ %32, %31 ], [ 0, %10 ]
  %20 = icmp eq i32 %.034, 0
  %21 = icmp ult i32 %.02533, 240
  %or.cond = and i1 %21, %20
  br i1 %or.cond, label %22, label %27

22:                                               ; preds = %.split
  %23 = icmp ugt i32 %.02533, 15
  br i1 %23, label %24, label %31

24:                                               ; preds = %22
  %25 = tail call i32 @gpio_input_get() #2
  %26 = and i32 %25, %0
  %.not32 = icmp eq i32 %26, 0
  %spec.select = zext i1 %.not32 to i32
  br label %31

27:                                               ; preds = %.split
  br i1 %20, label %.loopexit, label %28

28:                                               ; preds = %27
  %29 = tail call i32 @gpio_input_get() #2
  %30 = and i32 %29, %0
  %.not31 = icmp eq i32 %30, 0
  br i1 %.not31, label %31, label %.loopexit

31:                                               ; preds = %24, %28, %22
  %.1 = phi i32 [ 0, %22 ], [ 1, %28 ], [ %spec.select, %24 ]
  tail call void @ets_delay_us(i32 noundef 1) #2
  %32 = add nuw nsw i32 %.02533, 1
  %exitcond.not = icmp eq i32 %32, 480
  br i1 %exitcond.not, label %.loopexit, label %.split, !llvm.loop !5

.loopexit:                                        ; preds = %31, %27, %28, %18, %7
  %.127 = phi i32 [ 0, %7 ], [ %.1.us, %18 ], [ %.1, %31 ], [ 0, %27 ], [ 1, %28 ]
  ret i32 %.127
}

declare void @system_soft_wdt_feed() local_unnamed_addr #1

declare i32 @gpio_input_get() local_unnamed_addr #1

declare void @dh_gpio_open_drain(i32 noundef, i32 noundef) local_unnamed_addr #1

declare void @dh_gpio_pull_up(i32 noundef, i32 noundef) local_unnamed_addr #1

declare void @dh_gpio_prepare_pins(i32 noundef, i32 noundef) local_unnamed_addr #1

declare void @gpio_output_set(i32 noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

declare void @ets_delay_us(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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


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