In this paper, we merge the heterogenous firm trade model of Melitz (2003) with the Ricardian model of Dornbusch, Fisher and Samuelson (DFS 1977) to explain how the pattern of international specialization and trade is determined by the interaction of comparative advantage, economies of scale, country sizes and trade barriers. The model is able to capture the existence of inter-industry trade and intra-industry trade in a single unified framework. It explains how trade openness affects the pattern of international specialization and trade. It generalizes Melitz's firm selection effect in the face of trade liberalization to a setting where the patterns of inter-industry trade and intra-industry are endogenous. Although opening to trade is unambiguously welfare-improving in both countries, trade liberalization can lead to an counter-Melitz effect in the larger country if it is insufficiently competitive in the sectors where it has the strongest comparative disadvantage but still produces. In this case, the operating productivity cutoff is lowered while the exporting cutoff increases in the face of trade liberalization. This is because the intersectoral resource allocation (IRA) effect dominates the Melitz effect in these sectors. Consequently, the larger country can lose from trade liberalization. Some hypotheses related to firms' exporting behavior across sectors upon opening up to trade and upon trade liberalization are also derived. Analyses of firm-level data of Chinese manufacturing sectors confirm these hypotheses.