Understanding entropy and efficiency A few years have passed since the last time I studied thermodynamics, and I'd like to give me a chance of asking some questions, maybe trivial, I don't know, but I want to clear matters up.
Since my knowledge of thermodynamics has faded away, my questions are not even clear to me and I'd appreciate any reference for further consultation on the subjects.
First question: The entropy is classically defined through its differential as $dS = \delta Q_{rev}/T$, where $\delta Q_{rev}$ is the heat transferred reversibly. For simplicity let me drop the subscript $rev$. This definition of entropy makes sense because $T^{-1}$ is an integrating factor for $\delta Q$, hence the entropy depends only on the current state of the system, up to a constant chosen as reference. 
My confusion begins when a consider an ideal gas undergoing a process with transfer of matter. I'll restrict myself to processes with variable volume and amount of matter. The change of heat is 
$$\delta Q=\Big(\frac{\delta Q}{\partial N}\Big)_{p,V}dN+\Big(\frac{\delta Q}{\partial V}\Big)_{p,N}dV$$
For the first term in the RHS, I can calculate the change of temperature using $pV=kNT$, hence $dT=-pV/(kN^2)dN$. Denoting by $C_p$ the heat capacity at constant pressure we have $\delta Q = -C_{p}pV/(kN^2)dN$. For the second term in the RHS, similar calculations yield $\delta Q = C_{p}p/(kN)dV$. Replacing the specific heat by its value in case of an ideal gas, i.e. $C_{p}=\gamma kN$, where $\gamma$ is a constante depending on the gas (monoatomic, diatomic, and so on), we have at the end
$$\delta Q=-\gamma\frac{pV}{N}dN+\gamma pdV.$$
Hence the entropy should be 
$$dS=-\gamma kdN+\gamma\frac{Nk}{V}dV,$$
where I replaced $T^{-1}$ using the ideal gas equation.
My  trouble here is that this last expresion for "$dS$" isn't an exact differential, in other words, the entropy doesn't depend only on the current state of the system, but also on the path for reaching it.
That "$dS$" isn't an exact differential follows from 
$$\frac{\partial Nk\gamma/V}{\partial N}\neq -\frac{\partial \gamma k}{\partial V}.$$
It's strange that in this case $1/(NT)$ is an integrating factor of $\delta Q$, and it's for me even more surprising that the quantity obtained by integrating $\delta Q/(NT)$ equals the entropy of an ideal gas, up to a constant of reference.
Second question: I was told that reversible processes are more efficient than irreversible ones, and that this fact is expressed in the inequality $\delta Q_{rev}\ge \delta Q_{ir}$ or $\delta W_{rev}\le \delta W_{ir}$, where $W$ stands for work. Again I'll write $\delta Q$ instead of $\delta Q_{rev}$.
My question is how do I compare a reversible process with an irreversible one? Let me explain what I mean. Textbooks say that the work of expansion of a gas against zero pressure is zero, hence $\delta W\le \delta W_{ir}$ implies by integration that the work done by a reversible process should be negative. But between to states there are infinitely many processes, what are the processes that do negative work. Say the initial state is $(p_i,V_i)$ and the final $(p_f,V_f)$, where $V_f>V_i$, then the process depicted below does positive work.

Basically, you must expand the gas at very low temperature and compress it at high temperature. Even more, given any number $A$, there is a path exchanging $A$ Joules of work. 
In general, given an irreversible process, how can I say what are the more efficient reversible processes?

Since people works all over the time with systems exchanging matter and calculating the efficiency of machines, I think my question should have a simple answer, so I apologize for the incredibly and unnecessary length of my question.
I'd appreciate any help.
Edit (2016-12-18):
Following the suggestion of pppqqq I have placed the second question in a new post, so every comment or answer to the second question should be posted there. Thanks.
 A: Regarding your first question, it seems like you are making some mess in (1) using consistently a set of independent variables and (2) writing the complete expression of $\delta Q _{\text {rev}}$. 
It works like this: general arguments imply that $\frac{\delta Q}{T}$ is a  differential. We can use this fact to obtain some relations between thermodynamic functions (Maxwell's relations), which, given some data on the system, like e.g. the explicit function $U(N,V,T)$, allow us to constrain the form of the other functions such as $p,\mu,\dots$. Let's see how it works for the ideal gas.
The first and second principles of thermodynamics, for an open ideal gas, give: $$T~\text d S = \text d U +p~\text d V-\mu ~\text dN.$$
Tautologically, $\mu$ is the chemical potential, defined by $$\mu =\left(\frac{\partial U}{\partial N}\right)_{V,S}.$$ In the following, I'll choose as independent variables, $T$, $V$ and $N$. This is accomplished using the state equation $$p=\frac{NkT}{V}$$ and the experimental fact that $U$ is extensive and doesn't depend on the volume:$$U=Nc_VT.$$ The chemical potential is almost completely determined by the requirement that $\text dS$ be an exact differential. Let us compute: $$0=\text d \text d S= (\cdots) \text d V \wedge \text d T +\\+\left[\frac{c_V}{T}+\frac{1}{T}\left(\frac{\partial \mu }{\partial T}\right)_{V,N}-\frac{\mu}{T^2}\right]\text d N \wedge \text d T+\\+\left[\frac{k}{V}+\frac{1}{T}\left(\frac{\partial \mu }{\partial V}\right)_{T,N}\right]\text d N \wedge \text d V.$$
The two parenthesis must separately be zero. The second parenthesis gives: $$\mu =-kT \ln V -c_VT\ln T+ f(T),$$ while the first yields $$ f'(T)=\frac{1}{T}f(T).$$ which has the solution $$f(T)=A T\equiv \ln (\alpha)T,$$ where $\alpha$ can (and does) depend on $N$. So  $$\mu (N,V,T)=kT\ln \left(\frac{\alpha(N)}{VT^{\frac{c_V}{k}}}\right).$$
Now, since $\mu$ is the ratio of two extensive quantities, it may depend only on the density $v=\frac{V}{N}$. Therefore: $$\mu (N,V,T)=kT\ln \left(\frac{K}{ vT^{\frac{c_V}{k}}}\right).$$ It is now impossible to further specify $\mu$ without completely specifying the entropy, which is determined in pure thermodynamics up to an extensive constant. The quantum statistical treatment yields, for a monoatomic gas, $c_V=\frac{3}{2}k$ and: $$\mu =kT\ln \left(\frac{\lambda ^3}{v}\right)$$ where $\lambda = \frac{h}{\sqrt {2mkT}}.$ In agreement with our previous expression.
At the end of the day, this long calculation shows that (1) the fact that $\frac{ \delta Q}{T} $ is a differential is not inconsistent with the form of the first law for an open ideal gas and (2) this very fact allows an almost complete determination of the chemical potential $\mu$. 
A: First, $dS= \frac{\delta Q_{rev}}{T}$ if only heat transfer is involved. This would not hold if mass exchange were also present. Mass brings with it entropy, internal energy etc., but not heat. Heat is only that transfer of energy which is caused by temperature differences. So $\frac{\delta Q}{\partial N}$ makes no physical sense.
You must begin with the fundamental equation characterizing a system: 
\begin{align}
S=f(U,V,N)
\end{align}
and then differentiate it
\begin{align}
dS & =\frac{\partial f}{\partial U}dU+\frac{\partial f}{\partial V}dV+\frac{\partial f}{\partial N}dN \\
& = \frac{1}{T}dU+\frac{p}{T}dV-\frac{\mu}{T}dN
\end{align}
and proceed to derive whatever result you want.
Reference: Thermodynamics by H. Callen.
