; 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.osSemaphoreAttr_t = type { ptr, i32, ptr, i32 }
%struct.osEventFlagsAttr_t = type { ptr, i32, ptr, i32 }
%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.osSemaphore_s = type { i8, i8, i8, i8, %struct.queue_s, %struct.queue_s, i16, i16, ptr }
%struct.queue_s = type { ptr, ptr }
%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: nounwind uwtable
define dso_local i32 @I2C_PortOpen(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca %struct.osSemaphoreAttr_t, align 8
  %7 = alloca %struct.osEventFlagsAttr_t, align 8
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = call ptr @GetPort(i32 noundef %8)
  store ptr %9, ptr %4, align 8, !tbaa !9
  %10 = load ptr, ptr %4, align 8, !tbaa !9
  %11 = icmp eq ptr %10, null
  br i1 %11, label %12, label %13

12:                                               ; preds = %1
  store i32 -4, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %80

13:                                               ; preds = %1
  %14 = load ptr, ptr %4, align 8, !tbaa !9
  %15 = getelementptr inbounds %struct.I2C_PortResources, ptr %14, i32 0, i32 2
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  %17 = getelementptr inbounds %struct.I2C_PortInfo, ptr %16, i32 0, i32 0
  %18 = load i32, ptr %17, align 8
  %19 = and i32 %18, 1
  %20 = icmp eq i32 %19, 1
  br i1 %20, label %21, label %22

21:                                               ; preds = %13
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %80

22:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 32, ptr %6) #3
  %23 = getelementptr inbounds %struct.osSemaphoreAttr_t, ptr %6, i32 0, i32 0
  store ptr null, ptr %23, align 8, !tbaa !13
  %24 = getelementptr inbounds %struct.osSemaphoreAttr_t, ptr %6, i32 0, i32 1
  store i32 0, ptr %24, align 8, !tbaa !15
  %25 = getelementptr inbounds %struct.osSemaphoreAttr_t, ptr %6, i32 0, i32 2
  %26 = load ptr, ptr %4, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.I2C_PortResources, ptr %26, i32 0, i32 2
  %28 = load ptr, ptr %27, align 8, !tbaa !11
  %29 = getelementptr inbounds %struct.I2C_PortInfo, ptr %28, i32 0, i32 2
  store ptr %29, ptr %25, align 8, !tbaa !16
  %30 = getelementptr inbounds %struct.osSemaphoreAttr_t, ptr %6, i32 0, i32 3
  store i32 56, ptr %30, align 8, !tbaa !17
  %31 = call ptr @osSemaphoreNew(i32 noundef 1, i32 noundef 0, ptr noundef %6)
  %32 = icmp eq ptr %31, null
  br i1 %32, label %33, label %34

33:                                               ; preds = %22
  store i32 -3, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %79

34:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 32, ptr %7) #3
  %35 = getelementptr inbounds %struct.osEventFlagsAttr_t, ptr %7, i32 0, i32 0
  store ptr null, ptr %35, align 8, !tbaa !13
  %36 = getelementptr inbounds %struct.osEventFlagsAttr_t, ptr %7, i32 0, i32 1
  store i32 0, ptr %36, align 8, !tbaa !15
  %37 = getelementptr inbounds %struct.osEventFlagsAttr_t, ptr %7, i32 0, i32 2
  %38 = load ptr, ptr %4, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.I2C_PortResources, ptr %38, i32 0, i32 2
  %40 = load ptr, ptr %39, align 8, !tbaa !11
  %41 = getelementptr inbounds %struct.I2C_PortInfo, ptr %40, i32 0, i32 3
  store ptr %41, ptr %37, align 8, !tbaa !16
  %42 = getelementptr inbounds %struct.osEventFlagsAttr_t, ptr %7, i32 0, i32 3
  store i32 56, ptr %42, align 8, !tbaa !17
  %43 = call ptr @osEventFlagsNew(ptr noundef %7)
  %44 = icmp eq ptr %43, null
  br i1 %44, label %45, label %46

45:                                               ; preds = %34
  store i32 -3, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %78

46:                                               ; preds = %34
  %47 = load ptr, ptr %4, align 8, !tbaa !9
  %48 = getelementptr inbounds %struct.I2C_PortResources, ptr %47, i32 0, i32 0
  %49 = load ptr, ptr %48, align 8, !tbaa !18
  %50 = getelementptr inbounds %struct._DRIVER_I2C, ptr %49, i32 0, i32 2
  %51 = load ptr, ptr %50, align 8, !tbaa !19
  %52 = load ptr, ptr %4, align 8, !tbaa !9
  %53 = getelementptr inbounds %struct.I2C_PortResources, ptr %52, i32 0, i32 1
  %54 = load ptr, ptr %53, align 8, !tbaa !21
  %55 = call i32 %51(ptr noundef %54)
  %56 = load ptr, ptr %4, align 8, !tbaa !9
  %57 = getelementptr inbounds %struct.I2C_PortResources, ptr %56, i32 0, i32 0
  %58 = load ptr, ptr %57, align 8, !tbaa !18
  %59 = getelementptr inbounds %struct._DRIVER_I2C, ptr %58, i32 0, i32 4
  %60 = load ptr, ptr %59, align 8, !tbaa !22
  %61 = call i32 %60(i32 noundef 2)
  %62 = load ptr, ptr %4, align 8, !tbaa !9
  %63 = getelementptr inbounds %struct.I2C_PortResources, ptr %62, i32 0, i32 2
  %64 = load ptr, ptr %63, align 8, !tbaa !11
  %65 = getelementptr inbounds %struct.I2C_PortInfo, ptr %64, i32 0, i32 1
  store ptr null, ptr %65, align 8, !tbaa !23
  %66 = load ptr, ptr %4, align 8, !tbaa !9
  %67 = getelementptr inbounds %struct.I2C_PortResources, ptr %66, i32 0, i32 2
  %68 = load ptr, ptr %67, align 8, !tbaa !11
  %69 = getelementptr inbounds %struct.I2C_PortInfo, ptr %68, i32 0, i32 0
  %70 = load i32, ptr %69, align 8
  %71 = and i32 %70, -2
  %72 = or i32 %71, 1
  store i32 %72, ptr %69, align 8
  %73 = load ptr, ptr %4, align 8, !tbaa !9
  %74 = getelementptr inbounds %struct.I2C_PortResources, ptr %73, i32 0, i32 2
  %75 = load ptr, ptr %74, align 8, !tbaa !11
  %76 = getelementptr inbounds %struct.I2C_PortInfo, ptr %75, i32 0, i32 2
  %77 = call i32 @osSemaphoreRelease(ptr noundef %76)
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %78

78:                                               ; preds = %46, %45
  call void @llvm.lifetime.end.p0(i64 32, ptr %7) #3
  br label %79

79:                                               ; preds = %78, %33
  call void @llvm.lifetime.end.p0(i64 32, ptr %6) #3
  br label %80

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

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

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

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

declare ptr @osEventFlagsNew(ptr noundef) #2

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) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 16}
!12 = !{!"I2C_PortResources", !10, i64 0, !10, i64 8, !10, i64 16}
!13 = !{!14, !10, i64 0}
!14 = !{!"", !10, i64 0, !6, i64 8, !10, i64 16, !6, i64 24}
!15 = !{!14, !6, i64 8}
!16 = !{!14, !10, i64 16}
!17 = !{!14, !6, i64 24}
!18 = !{!12, !10, i64 0}
!19 = !{!20, !10, i64 16}
!20 = !{!"_DRIVER_I2C", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !10, i64 64, !10, i64 72, !10, i64 80, !10, i64 88}
!21 = !{!12, !10, i64 8}
!22 = !{!20, !10, i64 32}
!23 = !{!24, !10, i64 8}
!24 = !{!"I2C_PortInfo", !25, i64 0, !10, i64 8, !26, i64 16, !29, i64 72}
!25 = !{!"I2C_PortStatus", !6, i64 0, !6, i64 0}
!26 = !{!"osSemaphore_s", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !27, i64 8, !27, i64 24, !28, i64 40, !28, i64 42, !10, i64 48}
!27 = !{!"queue_s", !10, i64 0, !10, i64 8}
!28 = !{!"short", !7, i64 0}
!29 = !{!"osEventFlags_s", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !27, i64 8, !27, i64 24, !6, i64 40, !10, i64 48}


; 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.osSemaphoreAttr_t = type { ptr, i32, ptr, i32 }
%struct.osEventFlagsAttr_t = type { ptr, i32, ptr, i32 }

; Function Attrs: nounwind uwtable
define dso_local range(i32 -4, 1) i32 @I2C_PortOpen(i32 noundef %0) local_unnamed_addr #0 {
  %2 = alloca %struct.osSemaphoreAttr_t, align 8
  %3 = alloca %struct.osEventFlagsAttr_t, align 8
  %4 = tail call ptr @GetPort(i32 noundef %0)
  %5 = icmp eq ptr %4, null
  br i1 %5, label %46, label %6

6:                                                ; preds = %1
  %7 = getelementptr inbounds i8, ptr %4, i64 16
  %8 = load ptr, ptr %7, align 8, !tbaa !5
  %9 = load i32, ptr %8, align 8
  %10 = and i32 %9, 1
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %11, label %46

11:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %2) #3
  store ptr null, ptr %2, align 8, !tbaa !10
  %12 = getelementptr inbounds i8, ptr %2, i64 8
  store i32 0, ptr %12, align 8, !tbaa !13
  %13 = getelementptr inbounds i8, ptr %2, i64 16
  %14 = getelementptr inbounds i8, ptr %8, i64 16
  store ptr %14, ptr %13, align 8, !tbaa !14
  %15 = getelementptr inbounds i8, ptr %2, i64 24
  store i32 56, ptr %15, align 8, !tbaa !15
  %16 = call ptr @osSemaphoreNew(i32 noundef 1, i32 noundef 0, ptr noundef nonnull %2) #3
  %17 = icmp eq ptr %16, null
  br i1 %17, label %45, label %18

18:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %3) #3
  store ptr null, ptr %3, align 8, !tbaa !10
  %19 = getelementptr inbounds i8, ptr %3, i64 8
  store i32 0, ptr %19, align 8, !tbaa !13
  %20 = getelementptr inbounds i8, ptr %3, i64 16
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  %22 = getelementptr inbounds i8, ptr %21, i64 72
  store ptr %22, ptr %20, align 8, !tbaa !14
  %23 = getelementptr inbounds i8, ptr %3, i64 24
  store i32 56, ptr %23, align 8, !tbaa !15
  %24 = call ptr @osEventFlagsNew(ptr noundef nonnull %3) #3
  %25 = icmp eq ptr %24, null
  br i1 %25, label %44, label %26

26:                                               ; preds = %18
  %27 = load ptr, ptr %4, align 8, !tbaa !16
  %28 = getelementptr inbounds i8, ptr %27, i64 16
  %29 = load ptr, ptr %28, align 8, !tbaa !17
  %30 = getelementptr inbounds i8, ptr %4, i64 8
  %31 = load ptr, ptr %30, align 8, !tbaa !19
  %32 = call i32 %29(ptr noundef %31) #3
  %33 = load ptr, ptr %4, align 8, !tbaa !16
  %34 = getelementptr inbounds i8, ptr %33, i64 32
  %35 = load ptr, ptr %34, align 8, !tbaa !20
  %36 = call i32 %35(i32 noundef 2) #3
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  %38 = getelementptr inbounds i8, ptr %37, i64 8
  store ptr null, ptr %38, align 8, !tbaa !21
  %39 = load i32, ptr %37, align 8
  %40 = or i32 %39, 1
  store i32 %40, ptr %37, align 8
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = getelementptr inbounds i8, ptr %41, i64 16
  %43 = call i32 @osSemaphoreRelease(ptr noundef nonnull %42) #3
  br label %44

44:                                               ; preds = %18, %26
  %.0 = phi i32 [ 0, %26 ], [ -3, %18 ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %3) #3
  br label %45

45:                                               ; preds = %11, %44
  %.1 = phi i32 [ %.0, %44 ], [ -3, %11 ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %2) #3
  br label %46

46:                                               ; preds = %6, %1, %45
  %.2 = phi i32 [ %.1, %45 ], [ -4, %1 ], [ 0, %6 ]
  ret i32 %.2
}

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

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

declare ptr @osSemaphoreNew(i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

declare ptr @osEventFlagsNew(ptr noundef) local_unnamed_addr #2

declare i32 @osSemaphoreRelease(ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !7, i64 16}
!6 = !{!"I2C_PortResources", !7, i64 0, !7, i64 8, !7, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !7, i64 0}
!11 = !{!"", !7, i64 0, !12, i64 8, !7, i64 16, !12, i64 24}
!12 = !{!"int", !8, i64 0}
!13 = !{!11, !12, i64 8}
!14 = !{!11, !7, i64 16}
!15 = !{!11, !12, i64 24}
!16 = !{!6, !7, i64 0}
!17 = !{!18, !7, i64 16}
!18 = !{!"_DRIVER_I2C", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !7, i64 88}
!19 = !{!6, !7, i64 8}
!20 = !{!18, !7, i64 32}
!21 = !{!22, !7, i64 8}
!22 = !{!"I2C_PortInfo", !23, i64 0, !7, i64 8, !24, i64 16, !27, i64 72}
!23 = !{!"I2C_PortStatus", !12, i64 0, !12, i64 0}
!24 = !{!"osSemaphore_s", !8, i64 0, !8, i64 1, !8, i64 2, !8, i64 3, !25, i64 8, !25, i64 24, !26, i64 40, !26, i64 42, !7, i64 48}
!25 = !{!"queue_s", !7, i64 0, !7, i64 8}
!26 = !{!"short", !8, i64 0}
!27 = !{!"osEventFlags_s", !8, i64 0, !8, i64 1, !8, i64 2, !8, i64 3, !25, i64 8, !25, i64 24, !12, i64 40, !7, i64 48}


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