Revert "Revert "Cleanup getService""

Test: pass

This reverts commit 5f009cbdba69964b5b4709c8d5193bfac7423a86.

Change-Id: I8d606cddea3f387580564f8a40ee2538d1f126e7
diff --git a/generateCpp.cpp b/generateCpp.cpp
index 3a1a200..a89a567 100644
--- a/generateCpp.cpp
+++ b/generateCpp.cpp
@@ -187,13 +187,8 @@
         //     }
         // }
 
-        out.sFor("bool tried = false; "
-                 "!getStub && (vintfHwbinder || (vintfEmpty && !tried)); "
-                 "tried = true", [&] {
-
-            // Because this is a for loop, a "continue" statement means
-            // setting tried, and hence "break" for vintfEmpty and
-            // "retry" for vintfHwBinder
+        out << "bool tried = false;\n";
+        out.sWhile("!getStub && (vintfHwbinder || (vintfEmpty && !tried))", [&] {
 
             out.sIf("tried", [&] {
                 // sleep only after the first trial.
@@ -201,6 +196,8 @@
                     << "sleep(1);\n";
             }).endl();
 
+            out << "tried = true;\n";
+
             out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n";
             out.indent(2, [&] {
                 out << "= ::android::hardware::defaultServiceManager();\n";