+ msgtype = ldap_msgtype( res );
+ if ( msgtype != LDAP_RES_INTERMEDIATE ) {
+ int err;
+ char *text = NULL, *matched = NULL, **refs = NULL;
+ LDAPControl **ctrls = NULL;
+
+ rc = ldap_parse_result( ld, res, &err, &matched, &text, &refs, &ctrls, 1 );
+ if ( rc == LDAP_SUCCESS ) {
+ rc = err;
+ }
+ if ( rc != LDAP_SUCCESS ) {
+ tool_perror( res2str( op ), rc, NULL, matched, text, refs );
+ } else if ( msgtype != op ) {
+ fprintf( stderr, "%s: msgtype: expected %d got %d\n",
+ res2str( op ), op, msgtype );
+ rc = LDAP_OTHER;
+ }
+ if ( text ) {
+ ldap_memfree( text );
+ }
+ if ( matched ) {
+ ldap_memfree( matched );
+ }
+ if ( text ) {
+ ber_memvfree( (void **)refs );
+ }
+ if ( ctrls != NULL ) {
+ tool_print_ctrls( ld, ctrls );
+ ldap_controls_free( ctrls );
+ }
+ return rc;