; 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.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.i2c_com = type { i32, i32, i32, i32, %struct.osSemaphore_s }

; 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 ptr @osSemaphoreNew(i32 noundef, i32 noundef, 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) #0

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

20:                                               ; preds = %5
  store i32 -4, ptr %6, align 4
  store i32 1, ptr %14, align 4
  br label %101

21:                                               ; preds = %5
  %22 = load ptr, ptr %12, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.I2C_PortResources, ptr %22, i32 0, i32 2
  %24 = load ptr, ptr %23, align 8, !tbaa !11
  %25 = getelementptr inbounds %struct.I2C_PortInfo, ptr %24, i32 0, i32 0
  %26 = load i32, ptr %25, align 8
  %27 = and i32 %26, 1
  %28 = icmp eq i32 %27, 0
  br i1 %28, label %29, label %30

29:                                               ; preds = %21
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %14, align 4
  br label %101

30:                                               ; preds = %21
  %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 !11
  %34 = getelementptr inbounds %struct.I2C_PortInfo, ptr %33, i32 0, i32 2
  %35 = call i32 @osSemaphoreAcquire(ptr noundef %34, i32 noundef -1)
  %36 = icmp ne i32 %35, 0
  br i1 %36, label %37, label %38

37:                                               ; preds = %30
  store i32 -3, ptr %6, align 4
  store i32 1, ptr %14, align 4
  br label %101

38:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 32, ptr %15) #3
  %39 = getelementptr inbounds %struct.osSemaphoreAttr_t, ptr %15, i32 0, i32 0
  store ptr null, ptr %39, align 8, !tbaa !13
  %40 = getelementptr inbounds %struct.osSemaphoreAttr_t, ptr %15, i32 0, i32 1
  store i32 0, ptr %40, align 8, !tbaa !15
  %41 = getelementptr inbounds %struct.osSemaphoreAttr_t, ptr %15, i32 0, i32 2
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.i2c_com, ptr %42, i32 0, i32 4
  store ptr %43, ptr %41, align 8, !tbaa !16
  %44 = getelementptr inbounds %struct.osSemaphoreAttr_t, ptr %15, i32 0, i32 3
  store i32 56, ptr %44, align 8, !tbaa !17
  %45 = call ptr @osSemaphoreNew(i32 noundef 1, i32 noundef 0, ptr noundef %15)
  %46 = icmp ne ptr %45, null
  br i1 %46, label %47, label %93

47:                                               ; preds = %38
  %48 = load i32, ptr %8, align 4, !tbaa !9
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.i2c_com, ptr %49, i32 0, i32 0
  store i32 %48, ptr %50, align 8, !tbaa !18
  %51 = load i32, ptr %9, align 4, !tbaa !9
  %52 = load ptr, ptr %7, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.i2c_com, ptr %52, i32 0, i32 3
  store i32 %51, ptr %53, align 4, !tbaa !23
  %54 = load i32, ptr %11, align 4, !tbaa !9
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.i2c_com, ptr %55, i32 0, i32 2
  store i32 %54, ptr %56, align 8, !tbaa !24
  %57 = load i32, ptr %10, align 4, !tbaa !9
  %58 = zext i32 %57 to i64
  %59 = and i64 %58, 1024
  %60 = icmp ne i64 %59, 0
  br i1 %60, label %61, label %69

61:                                               ; preds = %47
  %62 = load i32, ptr %10, align 4, !tbaa !9
  %63 = lshr i32 %62, 1
  %64 = zext i32 %63 to i64
  %65 = and i64 %64, 127
  %66 = trunc i64 %65 to i32
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.i2c_com, ptr %67, i32 0, i32 1
  store i32 %66, ptr %68, align 4, !tbaa !25
  br label %89

69:                                               ; preds = %47
  %70 = load i32, ptr %10, align 4, !tbaa !9
  %71 = zext i32 %70 to i64
  %72 = and i64 %71, 2048
  %73 = icmp ne i64 %72, 0
  br i1 %73, label %74, label %81

74:                                               ; preds = %69
  %75 = load i32, ptr %10, align 4, !tbaa !9
  %76 = zext i32 %75 to i64
  %77 = and i64 %76, 1023
  %78 = trunc i64 %77 to i32
  %79 = load ptr, ptr %7, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.i2c_com, ptr %79, i32 0, i32 1
  store i32 %78, ptr %80, align 4, !tbaa !25
  br label %88

81:                                               ; preds = %69
  %82 = load i32, ptr %10, align 4, !tbaa !9
  %83 = zext i32 %82 to i64
  %84 = and i64 %83, 127
  %85 = trunc i64 %84 to i32
  %86 = load ptr, ptr %7, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.i2c_com, ptr %86, i32 0, i32 1
  store i32 %85, ptr %87, align 4, !tbaa !25
  br label %88

88:                                               ; preds = %81, %74
  br label %89

89:                                               ; preds = %88, %61
  %90 = load ptr, ptr %7, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.i2c_com, ptr %90, i32 0, i32 4
  %92 = call i32 @osSemaphoreRelease(ptr noundef %91)
  br label %94

93:                                               ; preds = %38
  store i32 -3, ptr %13, align 4, !tbaa !9
  br label %94

94:                                               ; preds = %93, %89
  %95 = load ptr, ptr %12, align 8, !tbaa !5
  %96 = getelementptr inbounds %struct.I2C_PortResources, ptr %95, i32 0, i32 2
  %97 = load ptr, ptr %96, align 8, !tbaa !11
  %98 = getelementptr inbounds %struct.I2C_PortInfo, ptr %97, i32 0, i32 2
  %99 = call i32 @osSemaphoreRelease(ptr noundef %98)
  %100 = load i32, ptr %13, align 4, !tbaa !9
  store i32 %100, ptr %6, align 4
  store i32 1, ptr %14, align 4
  call void @llvm.lifetime.end.p0(i64 32, ptr %15) #3
  br label %101

101:                                              ; preds = %94, %37, %29, %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  %102 = load i32, ptr %6, align 4
  ret i32 %102
}

declare i32 @osSemaphoreAcquire(ptr noundef, 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, !6, i64 16}
!12 = !{!"I2C_PortResources", !6, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!14, !6, i64 0}
!14 = !{!"", !6, i64 0, !10, i64 8, !6, i64 16, !10, i64 24}
!15 = !{!14, !10, i64 8}
!16 = !{!14, !6, i64 16}
!17 = !{!14, !10, i64 24}
!18 = !{!19, !10, i64 0}
!19 = !{!"i2c_com", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !20, i64 16}
!20 = !{!"osSemaphore_s", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !21, i64 8, !21, i64 24, !22, i64 40, !22, i64 42, !6, i64 48}
!21 = !{!"queue_s", !6, i64 0, !6, i64 8}
!22 = !{!"short", !7, i64 0}
!23 = !{!19, !10, i64 12}
!24 = !{!19, !10, i64 8}
!25 = !{!19, !10, i64 4}


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

; Function Attrs: mustprogress 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) local_unnamed_addr #1

declare ptr @osSemaphoreNew(i32 noundef, i32 noundef, 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) #0

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

9:                                                ; preds = %5
  %10 = getelementptr inbounds i8, ptr %7, i64 16
  %11 = load ptr, ptr %10, align 8, !tbaa !5
  %12 = load i32, ptr %11, align 8
  %13 = and i32 %12, 1
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %45, label %15

15:                                               ; preds = %9
  %16 = getelementptr inbounds i8, ptr %11, i64 16
  %17 = tail call i32 @osSemaphoreAcquire(ptr noundef nonnull %16, i32 noundef -1) #3
  %.not = icmp eq i32 %17, 0
  br i1 %.not, label %18, label %45

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

24:                                               ; preds = %18
  store i32 %1, ptr %0, align 8, !tbaa !16
  %25 = getelementptr inbounds i8, ptr %0, i64 12
  store i32 %2, ptr %25, align 4, !tbaa !21
  %26 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 %4, ptr %26, align 8, !tbaa !22
  %27 = zext i32 %3 to i64
  %28 = and i64 %27, 1024
  %.not25 = icmp eq i64 %28, 0
  %29 = getelementptr inbounds i8, ptr %0, i64 4
  br i1 %.not25, label %33, label %30

30:                                               ; preds = %24
  %31 = lshr i32 %3, 1
  %32 = and i32 %31, 127
  br label %39

33:                                               ; preds = %24
  %34 = and i64 %27, 2048
  %.not26 = icmp eq i64 %34, 0
  br i1 %.not26, label %37, label %35

35:                                               ; preds = %33
  %36 = and i32 %3, 1023
  br label %39

37:                                               ; preds = %33
  %38 = and i32 %3, 127
  br label %39

39:                                               ; preds = %35, %37, %30
  %.sink = phi i32 [ %36, %35 ], [ %38, %37 ], [ %32, %30 ]
  store i32 %.sink, ptr %29, align 4, !tbaa !23
  %40 = call i32 @osSemaphoreRelease(ptr noundef nonnull %21) #3
  br label %41

41:                                               ; preds = %18, %39
  %.0 = phi i32 [ 0, %39 ], [ -3, %18 ]
  %42 = load ptr, ptr %10, align 8, !tbaa !5
  %43 = getelementptr inbounds i8, ptr %42, i64 16
  %44 = call i32 @osSemaphoreRelease(ptr noundef nonnull %43) #3
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %6) #3
  br label %45

45:                                               ; preds = %15, %9, %5, %41
  %.022 = phi i32 [ %.0, %41 ], [ -4, %5 ], [ -1, %9 ], [ -3, %15 ]
  ret i32 %.022
}

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

attributes #0 = { mustprogress 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, !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 = !{!17, !12, i64 0}
!17 = !{!"i2c_com", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !18, i64 16}
!18 = !{!"osSemaphore_s", !8, i64 0, !8, i64 1, !8, i64 2, !8, i64 3, !19, i64 8, !19, i64 24, !20, i64 40, !20, i64 42, !7, i64 48}
!19 = !{!"queue_s", !7, i64 0, !7, i64 8}
!20 = !{!"short", !8, i64 0}
!21 = !{!17, !12, i64 12}
!22 = !{!17, !12, i64 8}
!23 = !{!17, !12, i64 4}


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