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

; 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.gpioevent_data = type { i64, i32 }
%struct.pollfd = type { i32, i16, i16 }

@stderr = external global ptr, align 8
@event_fd = external hidden global [6 x [64 x i32]], align 16
@.str.16 = external hidden unnamed_addr constant [43 x i8], align 1
@.str.17 = external hidden unnamed_addr constant [42 x i8], align 1
@.str.18 = external hidden unnamed_addr constant [35 x i8], align 1
@.str.19 = external hidden unnamed_addr constant [42 x i8], align 1
@.str.20 = external hidden unnamed_addr constant [46 x i8], align 1

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

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

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

declare void @perror(ptr noundef) #3

; 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 @rc_gpio_poll(i32 noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3) #4 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca %struct.gpioevent_data, align 8
  %12 = alloca [1 x %struct.pollfd], align 4
  %13 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.start.p0(i64 16, ptr %11) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  %14 = load i32, ptr %6, align 4, !tbaa !5
  %15 = icmp slt i32 %14, 0
  br i1 %15, label %19, label %16

16:                                               ; preds = %4
  %17 = load i32, ptr %6, align 4, !tbaa !5
  %18 = icmp sge i32 %17, 6
  br i1 %18, label %19, label %22

19:                                               ; preds = %16, %4
  %20 = load ptr, ptr @stderr, align 8, !tbaa !9
  %21 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %20, ptr noundef @.str.16) #5
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %99

22:                                               ; preds = %16
  %23 = load i32, ptr %7, align 4, !tbaa !5
  %24 = icmp slt i32 %23, 0
  br i1 %24, label %28, label %25

25:                                               ; preds = %22
  %26 = load i32, ptr %7, align 4, !tbaa !5
  %27 = icmp sge i32 %26, 64
  br i1 %27, label %28, label %31

28:                                               ; preds = %25, %22
  %29 = load ptr, ptr @stderr, align 8, !tbaa !9
  %30 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %29, ptr noundef @.str.17) #5
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %99

31:                                               ; preds = %25
  %32 = load i32, ptr %6, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [6 x [64 x i32]], ptr @event_fd, i64 0, i64 %33
  %35 = load i32, ptr %7, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [64 x i32], ptr %34, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = getelementptr inbounds [1 x %struct.pollfd], ptr %12, i64 0, i64 0
  %40 = getelementptr inbounds %struct.pollfd, ptr %39, i32 0, i32 0
  store i32 %38, ptr %40, align 4, !tbaa !11
  %41 = getelementptr inbounds [1 x %struct.pollfd], ptr %12, i64 0, i64 0
  %42 = getelementptr inbounds %struct.pollfd, ptr %41, i32 0, i32 1
  store i16 3, ptr %42, align 4, !tbaa !14
  %43 = getelementptr inbounds [1 x %struct.pollfd], ptr %12, i64 0, i64 0
  %44 = getelementptr inbounds %struct.pollfd, ptr %43, i32 0, i32 2
  store i16 0, ptr %44, align 2, !tbaa !15
  %45 = getelementptr inbounds [1 x %struct.pollfd], ptr %12, i64 0, i64 0
  %46 = load i32, ptr %8, align 4, !tbaa !5
  %47 = call i32 @poll(ptr noundef %45, i64 noundef 1, i32 noundef %46)
  store i32 %47, ptr %10, align 4, !tbaa !5
  %48 = load i32, ptr %10, align 4, !tbaa !5
  %49 = icmp eq i32 %48, -1
  %50 = xor i1 %49, true
  %51 = xor i1 %50, true
  %52 = zext i1 %51 to i32
  %53 = sext i32 %52 to i64
  %54 = call i64 @llvm.expect.i64(i64 %53, i64 0)
  %55 = icmp ne i64 %54, 0
  br i1 %55, label %56, label %57

56:                                               ; preds = %31
  call void @perror(ptr noundef @.str.18)
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %99

57:                                               ; preds = %31
  %58 = load i32, ptr %10, align 4, !tbaa !5
  %59 = icmp eq i32 %58, 0
  br i1 %59, label %60, label %61

60:                                               ; preds = %57
  store i32 0, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %99

61:                                               ; preds = %57
  br label %62

62:                                               ; preds = %61
  %63 = load i32, ptr %6, align 4, !tbaa !5
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [6 x [64 x i32]], ptr @event_fd, i64 0, i64 %64
  %66 = load i32, ptr %7, align 4, !tbaa !5
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [64 x i32], ptr %65, i64 0, i64 %67
  %69 = load i32, ptr %68, align 4, !tbaa !5
  %70 = call i64 @read(i32 noundef %69, ptr noundef %11, i64 noundef 16)
  %71 = trunc i64 %70 to i32
  store i32 %71, ptr %10, align 4, !tbaa !5
  %72 = load i32, ptr %10, align 4, !tbaa !5
  %73 = icmp eq i32 %72, -1
  br i1 %73, label %74, label %75

74:                                               ; preds = %62
  call void @perror(ptr noundef @.str.19)
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %99

75:                                               ; preds = %62
  %76 = getelementptr inbounds %struct.gpioevent_data, ptr %11, i32 0, i32 1
  %77 = load i32, ptr %76, align 8, !tbaa !16
  %78 = icmp ne i32 %77, 1
  br i1 %78, label %79, label %86

79:                                               ; preds = %75
  %80 = getelementptr inbounds %struct.gpioevent_data, ptr %11, i32 0, i32 1
  %81 = load i32, ptr %80, align 8, !tbaa !16
  %82 = icmp ne i32 %81, 2
  br i1 %82, label %83, label %86

83:                                               ; preds = %79
  %84 = load ptr, ptr @stderr, align 8, !tbaa !9
  %85 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %84, ptr noundef @.str.20) #5
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %99

86:                                               ; preds = %79, %75
  %87 = load ptr, ptr %9, align 8, !tbaa !9
  %88 = icmp ne ptr %87, null
  br i1 %88, label %89, label %93

89:                                               ; preds = %86
  %90 = getelementptr inbounds %struct.gpioevent_data, ptr %11, i32 0, i32 0
  %91 = load i64, ptr %90, align 8, !tbaa !19
  %92 = load ptr, ptr %9, align 8, !tbaa !9
  store i64 %91, ptr %92, align 8, !tbaa !20
  br label %93

93:                                               ; preds = %89, %86
  %94 = getelementptr inbounds %struct.gpioevent_data, ptr %11, i32 0, i32 1
  %95 = load i32, ptr %94, align 8, !tbaa !16
  %96 = icmp eq i32 %95, 1
  br i1 %96, label %97, label %98

97:                                               ; preds = %93
  store i32 1, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %99

98:                                               ; preds = %93
  store i32 2, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %99

99:                                               ; preds = %98, %97, %83, %74, %60, %56, %28, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  %100 = load i32, ptr %5, align 4
  ret i32 %100
}

declare i32 @poll(ptr noundef, i64 noundef, i32 noundef) #3

declare i64 @read(i32 noundef, ptr noundef, i64 noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #3 = { "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 = { 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 #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 = !{!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, !6, i64 0}
!12 = !{!"pollfd", !6, i64 0, !13, i64 4, !13, i64 6}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !13, i64 4}
!15 = !{!12, !13, i64 6}
!16 = !{!17, !6, i64 8}
!17 = !{!"gpioevent_data", !18, i64 0, !6, i64 8}
!18 = !{!"long long", !7, i64 0}
!19 = !{!17, !18, i64 0}
!20 = !{!21, !21, i64 0}
!21 = !{!"long", !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.gpioevent_data = type { i64, i32 }
%struct.pollfd = type { i32, i16, i16 }

@stderr = external local_unnamed_addr global ptr, align 8
@event_fd = external hidden local_unnamed_addr global [6 x [64 x i32]], align 16
@.str.16 = external hidden unnamed_addr constant [43 x i8], align 1
@.str.17 = external hidden unnamed_addr constant [42 x i8], align 1
@.str.18 = external hidden unnamed_addr constant [35 x i8], align 1
@.str.19 = external hidden unnamed_addr constant [42 x i8], align 1
@.str.20 = external hidden unnamed_addr constant [46 x i8], align 1

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

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

; Function Attrs: nofree nounwind
declare void @perror(ptr nocapture noundef readonly) local_unnamed_addr #1

; 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 -1, 3) i32 @rc_gpio_poll(i32 noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef writeonly %3) local_unnamed_addr #2 {
  %5 = alloca %struct.gpioevent_data, align 8
  %6 = alloca [1 x %struct.pollfd], align 4
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %5) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #5
  %or.cond = icmp ugt i32 %0, 5
  br i1 %or.cond, label %7, label %10

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

10:                                               ; preds = %4
  %or.cond3 = icmp ugt i32 %1, 63
  br i1 %or.cond3, label %11, label %14

11:                                               ; preds = %10
  %12 = load ptr, ptr @stderr, align 8, !tbaa !5
  %13 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %12, ptr noundef nonnull @.str.17) #6
  br label %41

14:                                               ; preds = %10
  %15 = zext nneg i32 %0 to i64
  %16 = zext nneg i32 %1 to i64
  %17 = getelementptr inbounds [6 x [64 x i32]], ptr @event_fd, i64 0, i64 %15, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !9
  store i32 %18, ptr %6, align 4, !tbaa !11
  %19 = getelementptr inbounds i8, ptr %6, i64 4
  store i16 3, ptr %19, align 4, !tbaa !14
  %20 = getelementptr inbounds i8, ptr %6, i64 6
  store i16 0, ptr %20, align 2, !tbaa !15
  %21 = call i32 @poll(ptr noundef nonnull %6, i64 noundef 1, i32 noundef %2) #5
  switch i32 %21, label %23 [
    i32 -1, label %22
    i32 0, label %41
  ], !prof !16

22:                                               ; preds = %14
  call void @perror(ptr noundef nonnull @.str.18) #7
  br label %41

23:                                               ; preds = %14
  %24 = load i32, ptr %17, align 4, !tbaa !9
  %25 = call i64 @read(i32 noundef %24, ptr noundef nonnull %5, i64 noundef 16) #5
  %26 = and i64 %25, 4294967295
  %27 = icmp eq i64 %26, 4294967295
  br i1 %27, label %28, label %29

28:                                               ; preds = %23
  call void @perror(ptr noundef nonnull @.str.19) #7
  br label %41

29:                                               ; preds = %23
  %30 = getelementptr inbounds i8, ptr %5, i64 8
  %31 = load i32, ptr %30, align 8
  %32 = add i32 %31, -3
  %or.cond6 = icmp ult i32 %32, -2
  br i1 %or.cond6, label %33, label %36

33:                                               ; preds = %29
  %34 = load ptr, ptr @stderr, align 8, !tbaa !5
  %35 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %34, ptr noundef nonnull @.str.20) #6
  br label %41

36:                                               ; preds = %29
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %39, label %37

37:                                               ; preds = %36
  %38 = load i64, ptr %5, align 8, !tbaa !17
  store i64 %38, ptr %3, align 8, !tbaa !20
  br label %39

39:                                               ; preds = %37, %36
  %40 = icmp eq i32 %31, 1
  %. = select i1 %40, i32 1, i32 2
  br label %41

41:                                               ; preds = %39, %14, %33, %28, %22, %11, %7
  %.0 = phi i32 [ -1, %7 ], [ -1, %11 ], [ -1, %22 ], [ -1, %28 ], [ -1, %33 ], [ %21, %14 ], [ %., %39 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %5) #5
  ret i32 %.0
}

declare i32 @poll(ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #3

; Function Attrs: nofree
declare noundef i64 @read(i32 noundef, ptr nocapture noundef, i64 noundef) local_unnamed_addr #4

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #3 = { "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 = { 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 #5 = { nounwind }
attributes #6 = { cold nounwind }
attributes #7 = { cold }

!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 = !{!"pollfd", !10, i64 0, !13, i64 4, !13, i64 6}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !13, i64 4}
!15 = !{!12, !13, i64 6}
!16 = !{!"branch_weights", i32 2000, i32 2, i32 2000}
!17 = !{!18, !19, i64 0}
!18 = !{!"gpioevent_data", !19, i64 0, !10, i64 8}
!19 = !{!"long long", !7, i64 0}
!20 = !{!21, !21, i64 0}
!21 = !{!"long", !7, i64 0}


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