; LLVM IR from koshkin-sergey/mbOS :: Middleware/I2C (i2c_master)

; 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.i2c_com = type { i32, i32, i32, i32, %struct.osSemaphore_s }
%struct.osSemaphore_s = type { i8, i8, i8, i8, %struct.queue_s, %struct.queue_s, i16, i16, ptr }
%struct.queue_s = type { ptr, ptr }
%struct.I2C_PortResources = type { ptr, ptr, ptr }
%struct.I2C_PortInfo = type { %struct.I2C_PortStatus, ptr, %struct.osSemaphore_s, %struct.osEventFlags_s }
%struct.I2C_PortStatus = type { i32 }
%struct.osEventFlags_s = type { i8, i8, i8, i8, %struct.queue_s, %struct.queue_s, i32, ptr }
%struct._DRIVER_I2C = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }

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

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

declare i32 @osSemaphoreRelease(ptr noundef) #2

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @I2C_Write(ptr noundef %0, ptr noundef %1, i32 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %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
  %12 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = icmp eq ptr %13, null
  br i1 %14, label %21, label %15

15:                                               ; preds = %3
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = icmp eq ptr %16, null
  br i1 %17, label %21, label %18

18:                                               ; preds = %15
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %22

21:                                               ; preds = %18, %15, %3
  store i32 -4, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %115

22:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.i2c_com, ptr %23, i32 0, i32 0
  %25 = load i32, ptr %24, align 8, !tbaa !11
  %26 = call ptr @GetPort(i32 noundef %25)
  store ptr %26, ptr %12, align 8, !tbaa !5
  %27 = load ptr, ptr %12, align 8, !tbaa !5
  %28 = icmp eq ptr %27, null
  br i1 %28, label %29, label %30

29:                                               ; preds = %22
  store i32 -3, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %114

30:                                               ; preds = %22
  %31 = load ptr, ptr %12, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.I2C_PortResources, ptr %31, i32 0, i32 2
  %33 = load ptr, ptr %32, align 8, !tbaa !16
  %34 = getelementptr inbounds %struct.I2C_PortInfo, ptr %33, i32 0, i32 2
  %35 = call i32 @osSemaphoreAcquire(ptr noundef %34, i32 noundef -1)
  %36 = icmp eq i32 %35, 0
  br i1 %36, label %37, label %111

37:                                               ; preds = %30
  %38 = load ptr, ptr %12, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.I2C_PortResources, ptr %38, i32 0, i32 2
  %40 = load ptr, ptr %39, align 8, !tbaa !16
  %41 = getelementptr inbounds %struct.I2C_PortInfo, ptr %40, i32 0, i32 1
  %42 = load ptr, ptr %41, align 8, !tbaa !18
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = icmp ne ptr %42, %43
  br i1 %44, label %45, label %60

45:                                               ; preds = %37
  %46 = load ptr, ptr %12, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.I2C_PortResources, ptr %46, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8, !tbaa !22
  %49 = getelementptr inbounds %struct._DRIVER_I2C, ptr %48, i32 0, i32 10
  %50 = load ptr, ptr %49, align 8, !tbaa !23
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.i2c_com, ptr %51, i32 0, i32 3
  %53 = load i32, ptr %52, align 4, !tbaa !25
  %54 = call i32 %50(i32 noundef 2, i32 noundef %53)
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = load ptr, ptr %12, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.I2C_PortResources, ptr %56, i32 0, i32 2
  %58 = load ptr, ptr %57, align 8, !tbaa !16
  %59 = getelementptr inbounds %struct.I2C_PortInfo, ptr %58, i32 0, i32 1
  store ptr %55, ptr %59, align 8, !tbaa !18
  br label %60

60:                                               ; preds = %45, %37
  %61 = call i32 @osKernelGetTickCount()
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.i2c_com, ptr %62, i32 0, i32 2
  %64 = load i32, ptr %63, align 8, !tbaa !26
  %65 = add i32 %61, %64
  store i32 %65, ptr %10, align 4, !tbaa !9
  br label %66

66:                                               ; preds = %90, %60
  %67 = load ptr, ptr %12, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.I2C_PortResources, ptr %67, i32 0, i32 0
  %69 = load ptr, ptr %68, align 8, !tbaa !22
  %70 = getelementptr inbounds %struct._DRIVER_I2C, ptr %69, i32 0, i32 5
  %71 = load ptr, ptr %70, align 8, !tbaa !27
  %72 = load ptr, ptr %5, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.i2c_com, ptr %72, i32 0, i32 1
  %74 = load i32, ptr %73, align 4, !tbaa !28
  %75 = load ptr, ptr %6, align 8, !tbaa !5
  %76 = load i32, ptr %7, align 4, !tbaa !9
  %77 = call i32 %71(i32 noundef %74, ptr noundef %75, i32 noundef %76, i1 noundef zeroext false)
  store i32 %77, ptr %9, align 4, !tbaa !9
  %78 = load ptr, ptr %12, align 8, !tbaa !5
  %79 = load i32, ptr %9, align 4, !tbaa !9
  %80 = load ptr, ptr %5, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.i2c_com, ptr %80, i32 0, i32 2
  %82 = load i32, ptr %81, align 8, !tbaa !26
  %83 = call i32 @WaitOperation(ptr noundef %78, i32 noundef %79, i32 noundef %82)
  store i32 %83, ptr %8, align 4, !tbaa !9
  %84 = load i32, ptr %8, align 4, !tbaa !9
  %85 = icmp eq i32 %84, -2
  br i1 %85, label %86, label %88

86:                                               ; preds = %66
  %87 = call i32 @osDelay(i32 noundef 1)
  br label %89

88:                                               ; preds = %66
  br label %95

89:                                               ; preds = %86
  br label %90

90:                                               ; preds = %89
  %91 = call i32 @osKernelGetTickCount()
  %92 = load i32, ptr %10, align 4, !tbaa !9
  %93 = sub nsw i32 %91, %92
  %94 = icmp slt i32 %93, 0
  br i1 %94, label %66, label %95, !llvm.loop !29

95:                                               ; preds = %90, %88
  %96 = load i32, ptr %8, align 4, !tbaa !9
  %97 = icmp eq i32 %96, -2
  br i1 %97, label %98, label %105

98:                                               ; preds = %95
  %99 = load ptr, ptr %12, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct.I2C_PortResources, ptr %99, i32 0, i32 0
  %101 = load ptr, ptr %100, align 8, !tbaa !22
  %102 = getelementptr inbounds %struct._DRIVER_I2C, ptr %101, i32 0, i32 10
  %103 = load ptr, ptr %102, align 8, !tbaa !23
  %104 = call i32 %103(i32 noundef 4, i32 noundef 0)
  br label %105

105:                                              ; preds = %98, %95
  %106 = load ptr, ptr %12, align 8, !tbaa !5
  %107 = getelementptr inbounds %struct.I2C_PortResources, ptr %106, i32 0, i32 2
  %108 = load ptr, ptr %107, align 8, !tbaa !16
  %109 = getelementptr inbounds %struct.I2C_PortInfo, ptr %108, i32 0, i32 2
  %110 = call i32 @osSemaphoreRelease(ptr noundef %109)
  br label %112

111:                                              ; preds = %30
  store i32 -3, ptr %8, align 4, !tbaa !9
  br label %112

112:                                              ; preds = %111, %105
  %113 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %113, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %114

114:                                              ; preds = %112, %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  br label %115

115:                                              ; preds = %114, %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %116 = load i32, ptr %4, align 4
  ret i32 %116
}

declare i32 @osKernelGetTickCount() #2

; Function Attrs: nounwind uwtable
declare hidden i32 @WaitOperation(ptr noundef, i32 noundef, i32 noundef) #1

declare i32 @osDelay(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 = !{!12, !10, i64 0}
!12 = !{!"i2c_com", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !13, i64 16}
!13 = !{!"osSemaphore_s", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !14, i64 8, !14, i64 24, !15, i64 40, !15, i64 42, !6, i64 48}
!14 = !{!"queue_s", !6, i64 0, !6, i64 8}
!15 = !{!"short", !7, i64 0}
!16 = !{!17, !6, i64 16}
!17 = !{!"I2C_PortResources", !6, i64 0, !6, i64 8, !6, i64 16}
!18 = !{!19, !6, i64 8}
!19 = !{!"I2C_PortInfo", !20, i64 0, !6, i64 8, !13, i64 16, !21, i64 72}
!20 = !{!"I2C_PortStatus", !10, i64 0, !10, i64 0}
!21 = !{!"osEventFlags_s", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !14, i64 8, !14, i64 24, !10, i64 40, !6, i64 48}
!22 = !{!17, !6, i64 0}
!23 = !{!24, !6, i64 80}
!24 = !{!"_DRIVER_I2C", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88}
!25 = !{!12, !10, i64 12}
!26 = !{!12, !10, i64 8}
!27 = !{!24, !6, i64 40}
!28 = !{!12, !10, i64 4}
!29 = distinct !{!29, !30}
!30 = !{!"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
declare hidden ptr @GetPort(i32 noundef) local_unnamed_addr #0

declare i32 @osSemaphoreRelease(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @I2C_Write(ptr noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq ptr %0, null
  %5 = icmp eq ptr %1, null
  %or.cond = or i1 %4, %5
  %6 = icmp eq i32 %2, 0
  %or.cond3 = or i1 %or.cond, %6
  br i1 %or.cond3, label %57, label %7

7:                                                ; preds = %3
  %8 = load i32, ptr %0, align 8, !tbaa !5
  %9 = tail call ptr @GetPort(i32 noundef %8)
  %10 = icmp eq ptr %9, null
  br i1 %10, label %57, label %11

11:                                               ; preds = %7
  %12 = getelementptr inbounds i8, ptr %9, i64 16
  %13 = load ptr, ptr %12, align 8, !tbaa !14
  %14 = getelementptr inbounds i8, ptr %13, i64 16
  %15 = tail call i32 @osSemaphoreAcquire(ptr noundef nonnull %14, i32 noundef -1) #2
  %16 = icmp eq i32 %15, 0
  br i1 %16, label %17, label %57

17:                                               ; preds = %11
  %18 = load ptr, ptr %12, align 8, !tbaa !14
  %19 = getelementptr inbounds i8, ptr %18, i64 8
  %20 = load ptr, ptr %19, align 8, !tbaa !16
  %.not = icmp eq ptr %20, %0
  br i1 %.not, label %30, label %21

21:                                               ; preds = %17
  %22 = load ptr, ptr %9, align 8, !tbaa !20
  %23 = getelementptr inbounds i8, ptr %22, i64 80
  %24 = load ptr, ptr %23, align 8, !tbaa !21
  %25 = getelementptr inbounds i8, ptr %0, i64 12
  %26 = load i32, ptr %25, align 4, !tbaa !23
  %27 = tail call i32 %24(i32 noundef 2, i32 noundef %26) #2
  %28 = load ptr, ptr %12, align 8, !tbaa !14
  %29 = getelementptr inbounds i8, ptr %28, i64 8
  store ptr %0, ptr %29, align 8, !tbaa !16
  br label %30

30:                                               ; preds = %21, %17
  %31 = tail call i32 @osKernelGetTickCount() #2
  %32 = getelementptr inbounds i8, ptr %0, i64 8
  %33 = load i32, ptr %32, align 8, !tbaa !24
  %34 = add i32 %33, %31
  %35 = getelementptr inbounds i8, ptr %0, i64 4
  br label %36

36:                                               ; preds = %45, %30
  %37 = load ptr, ptr %9, align 8, !tbaa !20
  %38 = getelementptr inbounds i8, ptr %37, i64 40
  %39 = load ptr, ptr %38, align 8, !tbaa !25
  %40 = load i32, ptr %35, align 4, !tbaa !26
  %41 = tail call i32 %39(i32 noundef %40, ptr noundef %1, i32 noundef %2, i1 noundef zeroext false) #2
  %42 = load i32, ptr %32, align 8, !tbaa !24
  %43 = tail call i32 @WaitOperation(ptr noundef nonnull %9, i32 noundef %41, i32 noundef %42)
  %44 = icmp eq i32 %43, -2
  br i1 %44, label %45, label %.critedge

45:                                               ; preds = %36
  %46 = tail call i32 @osDelay(i32 noundef 1) #2
  %47 = tail call i32 @osKernelGetTickCount() #2
  %48 = icmp slt i32 %47, %34
  br i1 %48, label %36, label %49, !llvm.loop !27

49:                                               ; preds = %45
  %50 = load ptr, ptr %9, align 8, !tbaa !20
  %51 = getelementptr inbounds i8, ptr %50, i64 80
  %52 = load ptr, ptr %51, align 8, !tbaa !21
  %53 = tail call i32 %52(i32 noundef 4, i32 noundef 0) #2
  br label %.critedge

.critedge:                                        ; preds = %36, %49
  %54 = load ptr, ptr %12, align 8, !tbaa !14
  %55 = getelementptr inbounds i8, ptr %54, i64 16
  %56 = tail call i32 @osSemaphoreRelease(ptr noundef nonnull %55) #2
  br label %57

57:                                               ; preds = %7, %11, %.critedge, %3
  %.1 = phi i32 [ -4, %3 ], [ -3, %7 ], [ %43, %.critedge ], [ -3, %11 ]
  ret i32 %.1
}

declare i32 @osKernelGetTickCount() local_unnamed_addr #1

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

declare i32 @osDelay(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, !7, i64 0}
!6 = !{!"i2c_com", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"osSemaphore_s", !8, i64 0, !8, i64 1, !8, i64 2, !8, i64 3, !11, i64 8, !11, i64 24, !13, i64 40, !13, i64 42, !12, i64 48}
!11 = !{!"queue_s", !12, i64 0, !12, i64 8}
!12 = !{!"any pointer", !8, i64 0}
!13 = !{!"short", !8, i64 0}
!14 = !{!15, !12, i64 16}
!15 = !{!"I2C_PortResources", !12, i64 0, !12, i64 8, !12, i64 16}
!16 = !{!17, !12, i64 8}
!17 = !{!"I2C_PortInfo", !18, i64 0, !12, i64 8, !10, i64 16, !19, i64 72}
!18 = !{!"I2C_PortStatus", !7, i64 0, !7, i64 0}
!19 = !{!"osEventFlags_s", !8, i64 0, !8, i64 1, !8, i64 2, !8, i64 3, !11, i64 8, !11, i64 24, !7, i64 40, !12, i64 48}
!20 = !{!15, !12, i64 0}
!21 = !{!22, !12, i64 80}
!22 = !{!"_DRIVER_I2C", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !12, i64 40, !12, i64 48, !12, i64 56, !12, i64 64, !12, i64 72, !12, i64 80, !12, i64 88}
!23 = !{!6, !7, i64 12}
!24 = !{!6, !7, i64 8}
!25 = !{!22, !12, i64 40}
!26 = !{!6, !7, i64 4}
!27 = distinct !{!27, !28}
!28 = !{!"llvm.loop.mustprogress"}


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