diff --git a/gcc/doc/sourcebuild.texi b/gcc/doc/sourcebuild.texi
index 1a31f00fb65..f7128f445cf 100644
--- a/gcc/doc/sourcebuild.texi
+++ b/gcc/doc/sourcebuild.texi
@@ -3530,7 +3530,7 @@ assembly output.
 Passes if @var{symbol} is not defined as a hidden symbol in the test's
 assembly output.
 
-@item check-function-bodies @var{prefix} @var{terminator} [@var{options} [@{ target/xfail @var{selector} @}]]
+@item check-function-bodies @var{prefix} @var{terminator} [@var{options} [@{ target/xfail @var{selector} @} [@var{matched}]]]
 Looks through the source file for comments that give the expected assembly
 output for selected functions.  Each line of expected output starts with the
 prefix string @var{prefix} and the expected output for a function as a whole
@@ -3544,6 +3544,10 @@ command line.  This can help if a source file is compiled both with
 and without optimization, since it is rarely useful to check the full
 function body for unoptimized code.
 
+@var{matched}, if specified, is a regular expression which matches a
+line of the function body.  If @var{matched} isn't specified, lines
+beginning with labels, directives and comments are ignored.
+
 The first line of the expected output for a function @var{fn} has the form:
 
 @smallexample
diff --git a/gcc/testsuite/gcc.target/i386/pr116174.c b/gcc/testsuite/gcc.target/i386/pr116174.c
index 8877d0b51af..91ec3288786 100644
--- a/gcc/testsuite/gcc.target/i386/pr116174.c
+++ b/gcc/testsuite/gcc.target/i386/pr116174.c
@@ -1,6 +1,20 @@
 /* { dg-do compile { target *-*-linux* } } */
-/* { dg-options "-O2 -fcf-protection=branch" } */
+/* { dg-options "-O2 -g0 -fcf-protection=branch" } */
+/* Keep labels and directives ('.p2align', '.cfi_startproc').
+/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } {.*} } } */
 
+/*
+**foo:
+**.LFB0:
+**	.cfi_startproc
+** (
+**	endbr64
+**	.p2align 5
+** |
+**	endbr32
+** )
+**...
+*/
 char *
 foo (char *dest, const char *src)
 {
@@ -8,5 +22,3 @@ foo (char *dest, const char *src)
     /* nothing */;
   return --dest;
 }
-
-/* { dg-final { scan-assembler "\t\.cfi_startproc\n\tendbr(32|64)\n" } } */
diff --git a/gcc/testsuite/lib/scanasm.exp b/gcc/testsuite/lib/scanasm.exp
index 42c719c512c..9dc99836762 100644
--- a/gcc/testsuite/lib/scanasm.exp
+++ b/gcc/testsuite/lib/scanasm.exp
@@ -952,6 +952,8 @@ proc parse_function_bodies { config filename result } {
 		verbose "parse_function_bodies: $function_name:\n$function_body"
 		set up_result($function_name) $function_body
 		set in_function 0
+	    } elseif { $up_config(matched) ne "" && [regexp $up_config(matched) $line] } {
+		append function_body $line "\n"
 	    } elseif { [regexp $up_config(fluff) $line] } {
 		verbose "parse_function_bodies: $function_name: ignoring fluff line: $line"
 	    } else {
@@ -982,7 +984,7 @@ proc check_function_body { functions name body_regexp } {
 
 # Check the implementations of functions against expected output.  Used as:
 #
-# { dg-do { check-function-bodies PREFIX TERMINATOR[ OPTION[ SELECTOR]] } }
+# { dg-do { check-function-bodies PREFIX TERMINATOR[ OPTION[ SELECTOR [MATCHED]]] } }
 #
 # See sourcebuild.texi for details.
 
@@ -990,7 +992,7 @@ proc check-function-bodies { args } {
     if { [llength $args] < 2 } {
 	error "too few arguments to check-function-bodies"
     }
-    if { [llength $args] > 4 } {
+    if { [llength $args] > 5 } {
 	error "too many arguments to check-function-bodies"
     }
 
@@ -1029,6 +1031,11 @@ proc check-function-bodies { args } {
 	}
     }
 
+    set matched ""
+    if { [llength $args] >= 5 } {
+	set matched [lindex $args 4]
+    }
+
     set testcase [testname-for-summary]
     # The name might include a list of options; extract the file name.
     set filename [lindex $testcase 0]
@@ -1048,6 +1055,7 @@ proc check-function-bodies { args } {
     # (name in \1).  This may be different from '$config(start)'.
     set start_expected {^(\S+):$}
 
+    set config(matched) $matched
     configure_check-function-bodies config
     set have_bodies 0
     if { [is_remote host] } {
@@ -1090,6 +1098,8 @@ proc check-function-bodies { args } {
 		append function_regexp ")"
 	    } elseif { [string equal $line "..."] } {
 		append function_regexp ".*"
+	    } elseif { [regexp "^.L.*" $line] } {
+		append function_regexp $line "\n"
 	    } else {
 		append function_regexp $config(line_prefix) $line "\n"
 	    }
