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

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

@mAddress = external hidden global i32, align 4
@mVoltage = external hidden global float, align 4
@pcf8591_write.str = external hidden constant [31 x i8], align 4
@pcf8591_write.str.4 = external hidden constant [25 x i8], align 4

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

declare i32 @dh_i2c_init(i32 noundef, i32 noundef) #1

declare void @dhdebug_ram(ptr noundef, ...) #1

declare i32 @dh_i2c_write(i32 noundef, ptr noundef, i32 noundef, i32 noundef) #1

; 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 @pcf8591_write(i32 noundef %0, i32 noundef %1, float noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca float, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i8, align 1
  %11 = alloca [2 x i8], align 1
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store float %2, ptr %7, align 4, !tbaa !9
  %12 = load float, ptr %7, align 4, !tbaa !9
  %13 = fcmp olt float %12, 0.000000e+00
  br i1 %13, label %18, label %14

14:                                               ; preds = %3
  %15 = load float, ptr %7, align 4, !tbaa !9
  %16 = load float, ptr @mVoltage, align 4, !tbaa !9
  %17 = fcmp ogt float %15, %16
  br i1 %17, label %18, label %19

18:                                               ; preds = %14, %3
  store i32 2, ptr %4, align 4
  br label %57

19:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = icmp ne i32 %20, -1
  br i1 %21, label %22, label %36

22:                                               ; preds = %19
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = icmp ne i32 %23, -1
  br i1 %24, label %25, label %36

25:                                               ; preds = %22
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = call i32 @dh_i2c_init(i32 noundef %26, i32 noundef %27)
  store i32 %28, ptr %8, align 4, !tbaa !5
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %35

30:                                               ; preds = %25
  br label %31

31:                                               ; preds = %30
  call void (ptr, ...) @dhdebug_ram(ptr noundef @pcf8591_write.str)
  br label %32

32:                                               ; preds = %31
  br label %33

33:                                               ; preds = %32
  %34 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %34, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %56

35:                                               ; preds = %25
  br label %36

36:                                               ; preds = %35, %22, %19
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #3
  %37 = load float, ptr %7, align 4, !tbaa !9
  %38 = load float, ptr @mVoltage, align 4, !tbaa !9
  %39 = fdiv float %37, %38
  %40 = fmul float %39, 2.550000e+02
  %41 = fptoui float %40 to i8
  store i8 %41, ptr %10, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 2, ptr %11) #3
  %42 = getelementptr inbounds [2 x i8], ptr %11, i64 0, i64 0
  store i8 68, ptr %42, align 1, !tbaa !11
  %43 = load i8, ptr %10, align 1, !tbaa !11
  %44 = getelementptr inbounds [2 x i8], ptr %11, i64 0, i64 1
  store i8 %43, ptr %44, align 1, !tbaa !11
  %45 = load i32, ptr @mAddress, align 4, !tbaa !5
  %46 = getelementptr inbounds [2 x i8], ptr %11, i64 0, i64 0
  %47 = call i32 @dh_i2c_write(i32 noundef %45, ptr noundef %46, i32 noundef 2, i32 noundef 1)
  store i32 %47, ptr %8, align 4, !tbaa !5
  %48 = icmp ne i32 %47, 0
  br i1 %48, label %49, label %54

49:                                               ; preds = %36
  br label %50

50:                                               ; preds = %49
  call void (ptr, ...) @dhdebug_ram(ptr noundef @pcf8591_write.str.4)
  br label %51

51:                                               ; preds = %50
  br label %52

52:                                               ; preds = %51
  %53 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %53, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %55

54:                                               ; preds = %36
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %55

55:                                               ; preds = %54, %52
  call void @llvm.lifetime.end.p0(i64 2, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #3
  br label %56

56:                                               ; preds = %55, %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %57

57:                                               ; preds = %56, %18
  %58 = load i32, ptr %4, align 4
  ret i32 %58
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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 = !{!10, !10, i64 0}
!10 = !{!"float", !7, i64 0}
!11 = !{!7, !7, i64 0}


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

@mAddress = external hidden local_unnamed_addr global i32, align 4
@mVoltage = external hidden local_unnamed_addr global float, align 4
@pcf8591_write.str = external hidden constant [31 x i8], align 4
@pcf8591_write.str.4 = external hidden constant [25 x i8], align 4

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

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

declare void @dhdebug_ram(ptr noundef, ...) local_unnamed_addr #1

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

; Function Attrs: mustprogress 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 @pcf8591_write(i32 noundef %0, i32 noundef %1, float noundef %2) local_unnamed_addr #2 {
  %4 = alloca [2 x i8], align 1
  %5 = fcmp olt float %2, 0.000000e+00
  %6 = load float, ptr @mVoltage, align 4
  %7 = fcmp olt float %6, %2
  %or.cond18 = select i1 %5, i1 true, i1 %7
  br i1 %or.cond18, label %24, label %8

8:                                                ; preds = %3
  %9 = icmp ne i32 %0, -1
  %10 = icmp ne i32 %1, -1
  %or.cond = and i1 %9, %10
  br i1 %or.cond, label %11, label %14

11:                                               ; preds = %8
  %12 = tail call i32 @dh_i2c_init(i32 noundef %0, i32 noundef %1) #3
  %.not = icmp eq i32 %12, 0
  br i1 %.not, label %._crit_edge, label %13

._crit_edge:                                      ; preds = %11
  %.pre = load float, ptr @mVoltage, align 4, !tbaa !5
  br label %14

13:                                               ; preds = %11
  tail call void (ptr, ...) @dhdebug_ram(ptr noundef nonnull @pcf8591_write.str) #3
  br label %24

14:                                               ; preds = %._crit_edge, %8
  %15 = phi float [ %.pre, %._crit_edge ], [ %6, %8 ]
  %16 = fdiv float %2, %15
  %17 = fmul float %16, 2.550000e+02
  %18 = fptoui float %17 to i8
  call void @llvm.lifetime.start.p0(i64 2, ptr nonnull %4) #3
  store i8 68, ptr %4, align 1, !tbaa !9
  %19 = getelementptr inbounds i8, ptr %4, i64 1
  store i8 %18, ptr %19, align 1, !tbaa !9
  %20 = load i32, ptr @mAddress, align 4, !tbaa !10
  %21 = call i32 @dh_i2c_write(i32 noundef %20, ptr noundef nonnull %4, i32 noundef 2, i32 noundef 1) #3
  %.not16 = icmp eq i32 %21, 0
  br i1 %.not16, label %23, label %22

22:                                               ; preds = %14
  call void (ptr, ...) @dhdebug_ram(ptr noundef nonnull @pcf8591_write.str.4) #3
  br label %23

23:                                               ; preds = %14, %22
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %4) #3
  br label %24

24:                                               ; preds = %13, %23, %3
  %.2 = phi i32 [ 2, %3 ], [ %12, %13 ], [ %21, %23 ]
  ret i32 %.2
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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 = !{!"float", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass