; LLVM IR from sca-secure-library-sca25519/sca25519 :: libopencm3/lib/pac55xx (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"

; 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 i32 @get_ccs_port_base(i32 noundef) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.cttz.i32(i32, i1 immarg) #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 void @gpio_set_af(i32 noundef %0, i32 noundef %1, i16 noundef zeroext %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i16, align 2
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  store i16 %2, ptr %6, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = call i32 @get_ccs_port_base(i32 noundef %12)
  store i32 %13, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %14 = load i32, ptr %7, align 4, !tbaa !5
  %15 = zext i32 %14 to i64
  %16 = inttoptr i64 %15 to ptr
  %17 = load volatile i32, ptr %16, align 4, !tbaa !5
  store i32 %17, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %18 = load i16, ptr %6, align 2, !tbaa !9
  %19 = zext i16 %18 to i32
  %20 = call i32 @llvm.cttz.i32(i32 %19, i1 true)
  %21 = add i32 %20, 1
  %22 = icmp eq i32 %19, 0
  %23 = select i1 %22, i32 0, i32 %21
  store i32 %23, ptr %9, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %27, %3
  %25 = load i32, ptr %9, align 4, !tbaa !5
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %53

27:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %28 = load i32, ptr %9, align 4, !tbaa !5
  %29 = sub nsw i32 %28, 1
  store i32 %29, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %30 = load i32, ptr %10, align 4, !tbaa !5
  %31 = mul nsw i32 %30, 4
  store i32 %31, ptr %11, align 4, !tbaa !5
  %32 = load i32, ptr %11, align 4, !tbaa !5
  %33 = shl i32 7, %32
  %34 = load i32, ptr %8, align 4, !tbaa !5
  %35 = and i32 %34, %33
  store i32 %35, ptr %8, align 4, !tbaa !5
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = load i32, ptr %11, align 4, !tbaa !5
  %38 = shl i32 %36, %37
  %39 = load i32, ptr %8, align 4, !tbaa !5
  %40 = or i32 %39, %38
  store i32 %40, ptr %8, align 4, !tbaa !5
  %41 = load i32, ptr %10, align 4, !tbaa !5
  %42 = shl i32 1, %41
  %43 = load i16, ptr %6, align 2, !tbaa !9
  %44 = zext i16 %43 to i32
  %45 = xor i32 %44, %42
  %46 = trunc i32 %45 to i16
  store i16 %46, ptr %6, align 2, !tbaa !9
  %47 = load i16, ptr %6, align 2, !tbaa !9
  %48 = zext i16 %47 to i32
  %49 = call i32 @llvm.cttz.i32(i32 %48, i1 true)
  %50 = add i32 %49, 1
  %51 = icmp eq i32 %48, 0
  %52 = select i1 %51, i32 0, i32 %50
  store i32 %52, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %24, !llvm.loop !11

53:                                               ; preds = %24
  %54 = load i32, ptr %8, align 4, !tbaa !5
  %55 = load i32, ptr %7, align 4, !tbaa !5
  %56 = zext i32 %55 to i64
  %57 = inttoptr i64 %56 to ptr
  store volatile i32 %54, ptr %57, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret void
}

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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

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

; Function Attrs: nounwind uwtable
define dso_local void @gpio_set_af(i32 noundef %0, i32 noundef %1, i16 noundef zeroext %2) local_unnamed_addr #0 {
  %4 = tail call i32 @get_ccs_port_base(i32 noundef %0)
  %5 = zext i32 %4 to i64
  %6 = inttoptr i64 %5 to ptr
  %7 = load volatile i32, ptr %6, align 4, !tbaa !5
  %8 = icmp eq i16 %2, 0
  br i1 %8, label %select.unfold._crit_edge, label %select.unfold.preheader

select.unfold.preheader:                          ; preds = %3
  %9 = tail call range(i16 0, 17) i16 @llvm.cttz.i16(i16 %2, i1 true)
  br label %select.unfold

select.unfold:                                    ; preds = %select.unfold.preheader, %select.unfold
  %.026 = phi i16 [ %17, %select.unfold ], [ %2, %select.unfold.preheader ]
  %.01625 = phi i32 [ %15, %select.unfold ], [ %7, %select.unfold.preheader ]
  %.017.in24.in = phi i16 [ %18, %select.unfold ], [ %9, %select.unfold.preheader ]
  %10 = shl nuw nsw i16 %.017.in24.in, 2
  %11 = zext nneg i16 %10 to i32
  %12 = shl i32 7, %11
  %13 = and i32 %12, %.01625
  %14 = shl i32 %1, %11
  %15 = or i32 %13, %14
  %16 = shl nuw i16 1, %.017.in24.in
  %17 = xor i16 %.026, %16
  %18 = tail call range(i16 0, 17) i16 @llvm.cttz.i16(i16 %17, i1 true)
  %19 = icmp eq i16 %.026, %16
  br i1 %19, label %select.unfold._crit_edge, label %select.unfold

select.unfold._crit_edge:                         ; preds = %select.unfold, %3
  %.016.lcssa = phi i32 [ %7, %3 ], [ %15, %select.unfold ]
  store volatile i32 %.016.lcssa, ptr %6, align 4, !tbaa !5
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.cttz.i16(i16, i1 immarg) #1

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 speculatable willreturn memory(none) }

!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"}


; Effective O3 passes: ADCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass