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

; 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_spi_state_t = type { [12 x i8], [12 x i8], [12 x i8], [12 x i8], [12 x i32], [12 x i32] }

@stderr = external global ptr, align 8
@state = external hidden global [6 x %struct.rc_spi_state_t], align 16
@.str.24 = external hidden unnamed_addr constant [54 x i8], align 1
@.str.25 = external hidden unnamed_addr constant [56 x i8], align 1
@.str.26 = external hidden unnamed_addr constant [50 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @rc_spi_get_fd(i32 noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  %6 = load i32, ptr %4, align 4, !tbaa !5
  %7 = icmp slt i32 %6, 0
  br i1 %7, label %11, label %8

8:                                                ; preds = %2
  %9 = load i32, ptr %4, align 4, !tbaa !5
  %10 = icmp sgt i32 %9, 5
  br i1 %10, label %11, label %14

11:                                               ; preds = %8, %2
  %12 = load ptr, ptr @stderr, align 8, !tbaa !9
  %13 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %12, ptr noundef @.str.24, i32 noundef 5) #2
  store i32 -1, ptr %3, align 4
  br label %46

14:                                               ; preds = %8
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = icmp slt i32 %15, 0
  br i1 %16, label %20, label %17

17:                                               ; preds = %14
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = icmp sge i32 %18, 12
  br i1 %19, label %20, label %23

20:                                               ; preds = %17, %14
  %21 = load ptr, ptr @stderr, align 8, !tbaa !9
  %22 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %21, ptr noundef @.str.25, i32 noundef 11) #2
  store i32 -1, ptr %3, align 4
  br label %46

23:                                               ; preds = %17
  %24 = load i32, ptr %4, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [6 x %struct.rc_spi_state_t], ptr @state, i64 0, i64 %25
  %27 = getelementptr inbounds %struct.rc_spi_state_t, ptr %26, i32 0, i32 0
  %28 = load i32, ptr %5, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [12 x i8], ptr %27, i64 0, i64 %29
  %31 = load i8, ptr %30, align 1, !tbaa !11
  %32 = zext i8 %31 to i32
  %33 = icmp eq i32 %32, 0
  br i1 %33, label %34, label %37

34:                                               ; preds = %23
  %35 = load ptr, ptr @stderr, align 8, !tbaa !9
  %36 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %35, ptr noundef @.str.26) #2
  store i32 -1, ptr %3, align 4
  br label %46

37:                                               ; preds = %23
  %38 = load i32, ptr %4, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [6 x %struct.rc_spi_state_t], ptr @state, i64 0, i64 %39
  %41 = getelementptr inbounds %struct.rc_spi_state_t, ptr %40, i32 0, i32 5
  %42 = load i32, ptr %5, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [12 x i32], ptr %41, i64 0, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !5
  store i32 %45, ptr %3, align 4
  br label %46

46:                                               ; preds = %37, %34, %20, %11
  %47 = load i32, ptr %3, align 4
  ret i32 %47
}

attributes #0 = { 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 #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 = { 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 = !{!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"

%struct.rc_spi_state_t = type { [12 x i8], [12 x i8], [12 x i8], [12 x i8], [12 x i32], [12 x i32] }

@stderr = external local_unnamed_addr global ptr, align 8
@state = external hidden local_unnamed_addr global [6 x %struct.rc_spi_state_t], align 16
@.str.24 = external hidden unnamed_addr constant [54 x i8], align 1
@.str.25 = external hidden unnamed_addr constant [56 x i8], align 1
@.str.26 = external hidden unnamed_addr constant [50 x i8], align 1

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

; Function Attrs: nofree nounwind uwtable
define dso_local i32 @rc_spi_get_fd(i32 noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %or.cond = icmp ugt i32 %0, 5
  br i1 %or.cond, label %3, label %6

3:                                                ; preds = %2
  %4 = load ptr, ptr @stderr, align 8, !tbaa !5
  %5 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %4, ptr noundef nonnull @.str.24, i32 noundef 5) #2
  br label %24

6:                                                ; preds = %2
  %or.cond3 = icmp ugt i32 %1, 11
  br i1 %or.cond3, label %7, label %10

7:                                                ; preds = %6
  %8 = load ptr, ptr @stderr, align 8, !tbaa !5
  %9 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %8, ptr noundef nonnull @.str.25, i32 noundef 11) #2
  br label %24

10:                                               ; preds = %6
  %11 = zext nneg i32 %0 to i64
  %12 = getelementptr inbounds [6 x %struct.rc_spi_state_t], ptr @state, i64 0, i64 %11
  %13 = zext nneg i32 %1 to i64
  %14 = getelementptr inbounds [12 x i8], ptr %12, i64 0, i64 %13
  %15 = load i8, ptr %14, align 1, !tbaa !9
  %16 = icmp eq i8 %15, 0
  br i1 %16, label %17, label %20

17:                                               ; preds = %10
  %18 = load ptr, ptr @stderr, align 8, !tbaa !5
  %19 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %18, ptr noundef nonnull @.str.26) #2
  br label %24

20:                                               ; preds = %10
  %21 = getelementptr inbounds i8, ptr %12, i64 96
  %22 = getelementptr inbounds [12 x i32], ptr %21, i64 0, i64 %13
  %23 = load i32, ptr %22, align 4, !tbaa !10
  br label %24

24:                                               ; preds = %20, %17, %7, %3
  %.0 = phi i32 [ -1, %3 ], [ -1, %7 ], [ -1, %17 ], [ %23, %20 ]
  ret i32 %.0
}

attributes #0 = { 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 #1 = { nofree 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 = { cold 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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}


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