/* compile with
   
gcc  -D__KERNEL__ -D__SMP__ -Wall -Wstrict-prototypes -O2 \
     -fomit-frame-pointer -fno-strict-aliasing -pipe -mno-fp-regs -ffixed-8 \
     -mcpu=ev6 -Wa,-mev6 -I/usr/src/linux-2.4.3-12/include -o gccbug.S -S gccbug.c

*/

#include <linux/mm.h>
#include <linux/smp_lock.h>
#include <asm/atomic.h>

struct foo;
struct bar {
        atomic_t atom;
        struct inode *ip;
};



void external_function(struct vm_area_struct *vma, struct inode *vp, struct bar *bar, struct foo *foo);
struct inode *second_external_function(unsigned long var_a, unsigned long var_b, int c);

void function_with_wrong_args(unsigned long var_a, unsigned long var_b,
                              int wrong_flag, int var_d);
static inline void bogus_inline(unsigned long var_a, unsigned long var_b,
                                  int flag, int var_d)
{
        /* the bug occurs only with the pairs 11:3 and 8:3. These
           combination can be calculated with a s4addq or s8addq.
           Changing e.g. the 11:3 to 10:3 avoids the bug.
        */
        function_with_wrong_args(var_a, var_b, flag ? 11: 3, var_d);
}


void bugy_function(struct foo *foo, unsigned long var_a, unsigned long var_b, int var_c)
{
	struct address_space *mapping = NULL;
	struct bar *bar= NULL;
        struct inode *vp = NULL;

	vp = second_external_function(var_b, var_a, 0);

        mapping = vp->i_mapping;
        bar = vp->u.generic_ip;

        if(S_ISDIR(vp->i_mode)) goto almost_end;
retry:
	if (atomic_read(&bar->atom)) {
		struct vm_area_struct *vma;

		spin_lock(&mapping->i_shared_lock);

		vma = mapping->i_mmap_shared;
		while (vma) {
                        if (var_c)
				external_function(vma, vp, bar, foo);
			vma = vma->vm_next_share;
		}
		vma = mapping->i_mmap;
		while (vma) {
			external_function(vma, vp, bar, foo);
			vma = vma->vm_next_share;
		}
		spin_unlock(&mapping->i_shared_lock);

	}

        /* this call ends up buggy. The var_c gets computed in
           the inline function as
           cmpult $31, $13, $1
           and stored on the stack with
           stl $1, 80($30)
           and the arguments gets then reloaded with
           ldl $18, 80($30)
        */
        
        bogus_inline(var_a, var_b, var_c, 0);

	if (var_c)
                goto retry;

	goto end;
        
almost_end:
        /* here we got the right computation of var_c
           in the inline function:
           cmpult $31,$13,$18
           s8addq $18,3,$18
        */

	bogus_inline(var_a, var_b, var_c, 0);
  end:
        
}

