It's unclear what you are doing here. Is your process root, then you setgid + setuid and expect getgroups to give you a list similar to what id provided?
First off, getgroups deals with credentials of the current process which must not be confused with credentials configured in /etc/group or other places. E.g. when the user in question logs in whatever deals with it has to explicitly set all the groups by hand, something you did not do. In particular that program does setuid and setgid, but also does other stuff to properly set credentials.
So you need to run a func which provides groups configured for given user. You already know a tool which does the job - it is id. Since this is opensource, you can just check what it does.
In this particular case the keyword you are looking for is getgrouplist. Note there is 0 use for setuid/setgid to find out what the group list is.