; LLVM IR from beagleboard/librobotcontrol :: library/src/io (i2c)

; 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.rc_i2c_state_t = type { i8, i32, i32, i32 }

@i2c = external hidden global [6 x %struct.rc_i2c_state_t], align 16
@stderr = external global ptr, align 8
@.str.11 = external hidden unnamed_addr constant [55 x i8], align 1
@.str.12 = external hidden unnamed_addr constant [63 x i8], align 1

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare i64 @llvm.expect.i64(i64, i64) #1

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

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #3

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

declare i64 @write(i32 noundef, ptr noundef, i64 noundef) #4

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #5

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #5

; Function Attrs: nounwind uwtable
define dso_local i32 @rc_i2c_write_bytes(i32 noundef %0, i8 noundef zeroext %1, i64 noundef %2, ptr noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i8, align 1
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i64, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i64, align 8
  %15 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i8 %1, ptr %7, align 1, !tbaa !9
  store i64 %2, ptr %8, align 8, !tbaa !10
  store ptr %3, ptr %9, align 8, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #6
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #6
  %16 = load i64, ptr %8, align 8, !tbaa !10
  %17 = add i64 %16, 1
  %18 = call ptr @llvm.stacksave.p0()
  store ptr %18, ptr %13, align 8
  %19 = alloca i8, i64 %17, align 16
  store i64 %17, ptr %14, align 8
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = call i32 @__check_bus_range(i32 noundef %20)
  %22 = icmp ne i32 %21, 0
  %23 = xor i1 %22, true
  %24 = xor i1 %23, true
  %25 = zext i1 %24 to i32
  %26 = sext i32 %25 to i64
  %27 = call i64 @llvm.expect.i64(i64 %26, i64 0)
  %28 = icmp ne i64 %27, 0
  br i1 %28, label %29, label %30

29:                                               ; preds = %4
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %15, align 4
  br label %111

30:                                               ; preds = %4
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [6 x %struct.rc_i2c_state_t], ptr @i2c, i64 0, i64 %32
  %34 = getelementptr inbounds %struct.rc_i2c_state_t, ptr %33, i32 0, i32 2
  %35 = load i32, ptr %34, align 8, !tbaa !14
  %36 = icmp eq i32 %35, 0
  %37 = xor i1 %36, true
  %38 = xor i1 %37, true
  %39 = zext i1 %38 to i32
  %40 = sext i32 %39 to i64
  %41 = call i64 @llvm.expect.i64(i64 %40, i64 0)
  %42 = icmp ne i64 %41, 0
  br i1 %42, label %43, label %46

43:                                               ; preds = %30
  %44 = load ptr, ptr @stderr, align 8, !tbaa !12
  %45 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %44, ptr noundef @.str.11) #6
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %15, align 4
  br label %111

46:                                               ; preds = %30
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [6 x %struct.rc_i2c_state_t], ptr @i2c, i64 0, i64 %48
  %50 = getelementptr inbounds %struct.rc_i2c_state_t, ptr %49, i32 0, i32 3
  %51 = load i32, ptr %50, align 4, !tbaa !16
  store i32 %51, ptr %11, align 4, !tbaa !5
  %52 = load i32, ptr %6, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [6 x %struct.rc_i2c_state_t], ptr @i2c, i64 0, i64 %53
  %55 = getelementptr inbounds %struct.rc_i2c_state_t, ptr %54, i32 0, i32 3
  store i32 1, ptr %55, align 4, !tbaa !16
  %56 = load i8, ptr %7, align 1, !tbaa !9
  %57 = getelementptr inbounds i8, ptr %19, i64 0
  store i8 %56, ptr %57, align 16, !tbaa !9
  store i64 0, ptr %12, align 8, !tbaa !10
  br label %58

58:                                               ; preds = %70, %46
  %59 = load i64, ptr %12, align 8, !tbaa !10
  %60 = load i64, ptr %8, align 8, !tbaa !10
  %61 = icmp ult i64 %59, %60
  br i1 %61, label %62, label %73

62:                                               ; preds = %58
  %63 = load ptr, ptr %9, align 8, !tbaa !12
  %64 = load i64, ptr %12, align 8, !tbaa !10
  %65 = getelementptr inbounds i8, ptr %63, i64 %64
  %66 = load i8, ptr %65, align 1, !tbaa !9
  %67 = load i64, ptr %12, align 8, !tbaa !10
  %68 = add i64 %67, 1
  %69 = getelementptr inbounds i8, ptr %19, i64 %68
  store i8 %66, ptr %69, align 1, !tbaa !9
  br label %70

70:                                               ; preds = %62
  %71 = load i64, ptr %12, align 8, !tbaa !10
  %72 = add i64 %71, 1
  store i64 %72, ptr %12, align 8, !tbaa !10
  br label %58, !llvm.loop !17

73:                                               ; preds = %58
  %74 = load i32, ptr %6, align 4, !tbaa !5
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [6 x %struct.rc_i2c_state_t], ptr @i2c, i64 0, i64 %75
  %77 = getelementptr inbounds %struct.rc_i2c_state_t, ptr %76, i32 0, i32 1
  %78 = load i32, ptr %77, align 4, !tbaa !19
  %79 = load i64, ptr %8, align 8, !tbaa !10
  %80 = add i64 %79, 1
  %81 = call i64 @write(i32 noundef %78, ptr noundef %19, i64 noundef %80)
  %82 = trunc i64 %81 to i32
  store i32 %82, ptr %10, align 4, !tbaa !5
  %83 = load i32, ptr %10, align 4, !tbaa !5
  %84 = load i64, ptr %8, align 8, !tbaa !10
  %85 = add i64 %84, 1
  %86 = trunc i64 %85 to i32
  %87 = icmp ne i32 %83, %86
  %88 = xor i1 %87, true
  %89 = xor i1 %88, true
  %90 = zext i1 %89 to i32
  %91 = sext i32 %90 to i64
  %92 = call i64 @llvm.expect.i64(i64 %91, i64 0)
  %93 = icmp ne i64 %92, 0
  br i1 %93, label %94, label %105

94:                                               ; preds = %73
  %95 = load ptr, ptr @stderr, align 8, !tbaa !12
  %96 = load i32, ptr %10, align 4, !tbaa !5
  %97 = load i64, ptr %8, align 8, !tbaa !10
  %98 = add i64 %97, 1
  %99 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %95, ptr noundef @.str.12, i32 noundef %96, i64 noundef %98) #6
  %100 = load i32, ptr %11, align 4, !tbaa !5
  %101 = load i32, ptr %6, align 4, !tbaa !5
  %102 = sext i32 %101 to i64
  %103 = getelementptr inbounds [6 x %struct.rc_i2c_state_t], ptr @i2c, i64 0, i64 %102
  %104 = getelementptr inbounds %struct.rc_i2c_state_t, ptr %103, i32 0, i32 3
  store i32 %100, ptr %104, align 4, !tbaa !16
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %15, align 4
  br label %111

105:                                              ; preds = %73
  %106 = load i32, ptr %11, align 4, !tbaa !5
  %107 = load i32, ptr %6, align 4, !tbaa !5
  %108 = sext i32 %107 to i64
  %109 = getelementptr inbounds [6 x %struct.rc_i2c_state_t], ptr @i2c, i64 0, i64 %108
  %110 = getelementptr inbounds %struct.rc_i2c_state_t, ptr %109, i32 0, i32 3
  store i32 %106, ptr %110, align 4, !tbaa !16
  store i32 0, ptr %5, align 4
  store i32 1, ptr %15, align 4
  br label %111

111:                                              ; preds = %105, %94, %43, %29
  %112 = load ptr, ptr %13, align 8
  call void @llvm.stackrestore.p0(ptr %112)
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #6
  %113 = load i32, ptr %5, align 4
  ret i32 %113
}

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(none) }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nounwind "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 #4 = { "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 #5 = { nocallback nofree nosync nounwind willreturn }
attributes #6 = { 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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!15, !6, i64 8}
!15 = !{!"rc_i2c_state_t", !7, i64 0, !6, i64 4, !6, i64 8, !6, i64 12}
!16 = !{!15, !6, i64 12}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!15, !6, 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.rc_i2c_state_t = type { i8, i32, i32, i32 }

@i2c = external hidden local_unnamed_addr global [6 x %struct.rc_i2c_state_t], align 16
@stderr = external local_unnamed_addr global ptr, align 8
@.str.11 = external hidden unnamed_addr constant [55 x i8], align 1
@.str.12 = external hidden unnamed_addr constant [63 x i8], align 1

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

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nofree
declare noundef i64 @write(i32 noundef, ptr nocapture noundef readonly, i64 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @rc_i2c_write_bytes(i32 noundef %0, i8 noundef zeroext %1, i64 noundef %2, ptr nocapture noundef readonly %3) local_unnamed_addr #0 {
  %5 = add i64 %2, 1
  %6 = alloca i8, i64 %5, align 16
  %7 = tail call i32 @__check_bus_range(i32 noundef %0)
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %8, label %29, !prof !5

8:                                                ; preds = %4
  %9 = sext i32 %0 to i64
  %10 = getelementptr inbounds [6 x %struct.rc_i2c_state_t], ptr @i2c, i64 0, i64 %9
  %11 = getelementptr inbounds i8, ptr %10, i64 8
  %12 = load i32, ptr %11, align 8, !tbaa !6
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %17, !prof !11

14:                                               ; preds = %8
  %15 = load ptr, ptr @stderr, align 8, !tbaa !12
  %16 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %15, ptr noundef nonnull @.str.11) #4
  br label %29

17:                                               ; preds = %8
  %18 = getelementptr inbounds i8, ptr %10, i64 12
  %19 = load i32, ptr %18, align 4, !tbaa !14
  store i32 1, ptr %18, align 4, !tbaa !14
  store i8 %1, ptr %6, align 16, !tbaa !15
  %.not27 = icmp eq i64 %2, 0
  br i1 %.not27, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %17
  %scevgep = getelementptr i8, ptr %6, i64 1
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %scevgep, ptr align 1 %3, i64 %2, i1 false), !tbaa !15
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.preheader, %17
  %20 = getelementptr inbounds i8, ptr %10, i64 4
  %21 = load i32, ptr %20, align 4, !tbaa !16
  %22 = call i64 @write(i32 noundef %21, ptr noundef nonnull %6, i64 noundef %5) #5
  %23 = trunc i64 %22 to i32
  %24 = trunc i64 %5 to i32
  %.not25 = icmp eq i32 %23, %24
  br i1 %.not25, label %28, label %25, !prof !5

25:                                               ; preds = %._crit_edge
  %26 = load ptr, ptr @stderr, align 8, !tbaa !12
  %27 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %26, ptr noundef nonnull @.str.12, i32 noundef %23, i64 noundef %5) #4
  store i32 %19, ptr %18, align 4, !tbaa !14
  br label %29

28:                                               ; preds = %._crit_edge
  store i32 %19, ptr %18, align 4, !tbaa !14
  br label %29

29:                                               ; preds = %4, %28, %25, %14
  %.0 = phi i32 [ -1, %14 ], [ -1, %25 ], [ 0, %28 ], [ -1, %4 ]
  ret i32 %.0
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

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 = { nofree nounwind "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 = { nofree "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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { cold nounwind }
attributes #5 = { 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 = !{!"branch_weights", !"expected", i32 2000, i32 1}
!6 = !{!7, !10, i64 8}
!7 = !{!"rc_i2c_state_t", !8, i64 0, !10, i64 4, !10, i64 8, !10, i64 12}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!12 = !{!13, !13, i64 0}
!13 = !{!"any pointer", !8, i64 0}
!14 = !{!7, !10, i64 12}
!15 = !{!8, !8, i64 0}
!16 = !{!7, !10, i64 4}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,LowerExpectIntrinsicPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass