; LLVM IR from cyberbotics/webots_ros2 :: webots_ros2_driver/webots/src/controller/c (joystick)

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

@joystick = external hidden global %struct.WbJoystick, align 8
@stderr = external global ptr, align 8
@.str = external hidden unnamed_addr constant [77 x i8], align 1
@__FUNCTION__.wb_joystick_get_pressed_button = external hidden unnamed_addr constant [31 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

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

; Function Attrs: nounwind uwtable
define dso_local i32 @wb_joystick_get_pressed_button() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = load i32, ptr @joystick, align 8, !tbaa !5
  %4 = icmp sle i32 %3, 0
  br i1 %4, label %5, label %8

5:                                                ; preds = %0
  %6 = load ptr, ptr @stderr, align 8, !tbaa !12
  %7 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %6, ptr noundef @.str, ptr noundef @__FUNCTION__.wb_joystick_get_pressed_button) #3
  br label %8

8:                                                ; preds = %5, %0
  %9 = load i32, ptr getelementptr inbounds (%struct.WbJoystick, ptr @joystick, i32 0, i32 2), align 8, !tbaa !13
  %10 = icmp eq i32 %9, -1
  br i1 %10, label %14, label %11

11:                                               ; preds = %8
  %12 = load ptr, ptr getelementptr inbounds (%struct.WbJoystick, ptr @joystick, i32 0, i32 6), align 8, !tbaa !14
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %15

14:                                               ; preds = %11, %8
  store i32 -1, ptr %1, align 4
  br label %28

15:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  %16 = load ptr, ptr getelementptr inbounds (%struct.WbJoystick, ptr @joystick, i32 0, i32 6), align 8, !tbaa !14
  %17 = load i32, ptr getelementptr inbounds (%struct.WbJoystick, ptr @joystick, i32 0, i32 2), align 8, !tbaa !13
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i32, ptr %16, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !15
  store i32 %20, ptr %2, align 4, !tbaa !15
  %21 = load i32, ptr %2, align 4, !tbaa !15
  %22 = icmp sge i32 %21, 0
  br i1 %22, label %23, label %26

23:                                               ; preds = %15
  %24 = load i32, ptr getelementptr inbounds (%struct.WbJoystick, ptr @joystick, i32 0, i32 2), align 8, !tbaa !13
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr getelementptr inbounds (%struct.WbJoystick, ptr @joystick, i32 0, i32 2), align 8, !tbaa !13
  br label %26

26:                                               ; preds = %23, %15
  %27 = load i32, ptr %2, align 4, !tbaa !15
  store i32 %27, ptr %1, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %28

28:                                               ; preds = %26, %14
  %29 = load i32, ptr %1, align 4
  ret i32 %29
}

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 = { 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 = { 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 0}
!6 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !7, i64 20, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !7, i64 56, !11, i64 64, !11, i64 72, !11, i64 80, !7, i64 88, !8, i64 92, !8, i64 93, !8, i64 94, !8, i64 95, !8, i64 96}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"double", !8, i64 0}
!12 = !{!10, !10, i64 0}
!13 = !{!6, !7, i64 8}
!14 = !{!6, !10, i64 24}
!15 = !{!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.WbJoystick = type { i32, i32, i32, i32, i32, i32, ptr, ptr, ptr, ptr, i32, double, double, double, i32, i8, i8, i8, i8, i8 }

@joystick = external hidden local_unnamed_addr global %struct.WbJoystick, align 8
@stderr = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [77 x i8], align 1
@__FUNCTION__.wb_joystick_get_pressed_button = external hidden unnamed_addr constant [31 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 @wb_joystick_get_pressed_button() local_unnamed_addr #1 {
  %1 = load i32, ptr @joystick, align 8, !tbaa !5
  %2 = icmp slt i32 %1, 1
  br i1 %2, label %3, label %6

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

6:                                                ; preds = %3, %0
  %7 = load i32, ptr getelementptr inbounds (i8, ptr @joystick, i64 8), align 8, !tbaa !13
  %8 = icmp eq i32 %7, -1
  %9 = load ptr, ptr getelementptr inbounds (i8, ptr @joystick, i64 24), align 8
  %10 = icmp eq ptr %9, null
  %or.cond = select i1 %8, i1 true, i1 %10
  br i1 %or.cond, label %18, label %11

11:                                               ; preds = %6
  %12 = sext i32 %7 to i64
  %13 = getelementptr inbounds i32, ptr %9, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !14
  %15 = icmp sgt i32 %14, -1
  br i1 %15, label %16, label %18

16:                                               ; preds = %11
  %17 = add nuw nsw i32 %7, 1
  store i32 %17, ptr getelementptr inbounds (i8, ptr @joystick, i64 8), align 8, !tbaa !13
  br label %18

18:                                               ; preds = %11, %16, %6
  %.0 = phi i32 [ -1, %6 ], [ %14, %16 ], [ %14, %11 ]
  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, !7, i64 0}
!6 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !7, i64 20, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !7, i64 56, !11, i64 64, !11, i64 72, !11, i64 80, !7, i64 88, !8, i64 92, !8, i64 93, !8, i64 94, !8, i64 95, !8, i64 96}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"double", !8, i64 0}
!12 = !{!10, !10, i64 0}
!13 = !{!6, !7, i64 8}
!14 = !{!7, !7, i64 0}


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