Just to put the focus on determining cover for a ranged attack (from the Dungeon Master Guide):
Attacker’s Burden: For ranged attacks, the attacker has to prove that he has a clear shot. That proof consists of one corner in his space that has clear lines to every corner of the target’s space.
Choose a Corner: The attacker chooses one corner of a square he occupies, and draws imaginary lines from that corner to every corner of any one square the defender occupies. If none of those lines are blocked by a solid object or an enemy creature, the attacker has a clear shot. The defender doesn’t have cover. (A line that runs parallel right along a wall isn’t blocked.)
Cover: If you can’t find a clear shot, the target has cover. No matter which corner in your space you choose, one or two lines from that corner to every corner in the defender’s space are blocked.
Also, from Player's Handbook 1:
Determining Cover: To determine if a target has cover, choose a corner of a square you occupy (or a corner of your attack’s origin square) and trace imaginary lines from that corner to every corner of any one square the target occupies. If one or two of those lines are blocked by an obstacle or an enemy, the target has cover. (A line isn’t blocked if it runs along the edge of an obstacle’s or an enemy’s square.) If three or four of those lines are blocked but you have line of effect, the target has superior cover.
If a creature is adjacent to a narrow opening on a wall that lays on a square's edge (like the X in your example), I would rule that the creature can choose any corner of its space in order to determine a clear shot (even those that are "in" the wall like the corners 1 and 3). So O have no cover against X's ranged attacks.
Instead, a creature not adjacent to the narrow opening (O) have no clear line to any of X's corners (but has line of effect), so X has superior cover against O's ranged attacks. In this case I don't consider 1 and 3 viable corners for determining a clear line.
. . . . . . . . . . .
|
. . . . . . . 1 2 . .
O ¦ X
. . . . . . . 3 4 . .
|
. . . . . . . . . . .
This is only a speculation on my side, but it could help if you are searching for a deterministic method for determining cover. However, remember that DM's practical sense is the best tool here.