// File: /workspace/github_c_makefile_star2_O2/ZmengXu[P]boxMesh/boxMesh.C
// Function: ownerOfExternalFace(int, int, int, int)
int ownerOfExternalFace(int externalFaceId, int Nx, int Ny, int Nz)
{
    /* Returns owner of face nInternalFaces+externalFaceId (which is an external face) */

    /* The way to calculate the owner depends on which face of the domain it belongs to:
     * The first Ny*Nz faces have outward normal (-1, 0, 0) (referred to as x-)
     * The next  Ny*Nz faces have outward normal ( 1, 0, 0) (referred to as x+)
     * The next  Nx*Nz faces have outward normal ( 0,-1, 0) (referred to as y-)
     * The next  Nx*Nz faces have outward normal ( 0, 1, 0) (referred to as y+)
     * The next  Nx*Ny faces have outward normal ( 0, 0,-1) (referred to as z-)
     * The next  Nx*Ny faces have outward normal ( 0, 0, 1) (referred to as z+)
     * The following tests check in which face of the domain the face is. */
    string normal;
    if   (externalFaceId < Ny*Nz)
        normal = "x-";
    else if (externalFaceId < 2*Ny*Nz)
        normal = "x+";
    else if (externalFaceId < 2*Ny*Nz + Nx*Nz)
        normal = "y-";
    else if (externalFaceId < 2*Ny*Nz + 2*Nx*Nz)
        normal = "y+";
    else if (externalFaceId < 2*Ny*Nz + 2*Nx*Nz + Nx*Ny)
        normal = "z-";
    else if (externalFaceId < 2*Ny*Nz + 2*Nx*Nz + 2*Nx*Ny)
        normal = "z+";
    else
        throw invalid_argument("externalFaceId is too large");

    /* Now that the normal to the face is known, calculate the owner in the
     * respective way. */
    int temp;
    int owner;
    if (normal == "x-")
    {
        temp  = externalFaceId;     // index on "x-" face
        int zCell = int(temp/Ny);   // z-grid-coordinate of face
        int yCell = temp%Ny;        // y-grid-coordinate of face
        owner = yCell*Nx + zCell*(Nx*Ny);
    }
    else if (normal == "x+")
    {
        temp  = externalFaceId - (Ny*Nz);
        int zCell = int(temp/Ny);
        int yCell = temp%Ny;
        owner = yCell*Nx + zCell*(Nx*Ny) + (Nx-1);
    }
    else if (normal == "y-")
    {
        temp  = externalFaceId - (2*Ny*Nz);     // index on "y-" face
        int xCell = int(temp/Nz);               // x-grid-coordinate of face
        int zCell = temp%Nz;                    // z-grid-coordinate of face
        owner = xCell + zCell*(Nx*Ny);
    }
    else if (normal == "y+")
    {
        temp  = externalFaceId - (2*Ny*Nz + Nx*Nz);
        int xCell = int(temp/Nz);
        int zCell = temp%Nz;
        owner = xCell + zCell*(Nx*Ny) + Nx*(Ny-1);
    }
    else if (normal == "z-")
    {
        temp  = externalFaceId - (2*Ny*Nz + 2*Nx*Nz);   // index on "z-" face
        int xCell = int(temp/Ny);                       // x-grid-coordinate of face
        int yCell = temp%Ny;                            // y-grid-coordinate of face
        owner = xCell + yCell*Nx;
    }
    else if (normal == "z+")
    {
        temp  = externalFaceId - (2*Ny*Nz + 2*Nx*Nz + Nx*Ny);
        int xCell = int(temp/Ny);
        int yCell = temp%Ny;
        owner = xCell + yCell*Nx + Nx*Ny*(Nz-1);
    }
    else
        throw logic_error("Unexpected value for normal: " + normal);

    return owner;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %ecx, %r12d
movl %edx, %r15d
movl %edi, %r14d
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl %ecx, %r13d
imull %edx, %r13d
movl %edi, %ebx
subl %r13d, %ebx
movq %rsi, -0x30(%rbp)
jge 0x3ffd
leaq 0x259b(%rip), %rsi      # 0x6596
jmp 0x406e
leal (%r15,%r15), %eax
movl %eax, %ecx
imull %r12d, %ecx
cmpl %r14d, %ecx
jle 0x4015
leaq 0x2586(%rip), %rsi      # 0x6599
jmp 0x406e
leal (%rax,%rsi), %ecx
imull %r12d, %ecx
cmpl %r14d, %ecx
jle 0x402a
leaq 0x2574(%rip), %rsi      # 0x659c
jmp 0x406e
leal (%rax,%rsi,2), %eax
imull %r12d, %eax
cmpl %r14d, %eax
jle 0x403f
leaq 0x2562(%rip), %rsi      # 0x659f
jmp 0x406e
movl %r15d, %ecx
imull %esi, %ecx
addl %eax, %ecx
cmpl %r14d, %ecx
jle 0x4055
leaq 0x254f(%rip), %rsi      # 0x65a2
jmp 0x406e
leal (%rsi,%rsi), %ecx
imull %r15d, %ecx
addl %ecx, %eax
cmpl %r14d, %eax
jle 0x41c7
leaq 0x2537(%rip), %rsi      # 0x65a5
leaq -0x50(%rbp), %rdi
callq 0x22c0
leaq 0x2518(%rip), %rsi      # 0x6596
leaq -0x50(%rbp), %rdi
callq 0x497d
testb %al, %al
je 0x4095
imull -0x30(%rbp), %r14d
jmp 0x41ac
leaq 0x24fd(%rip), %rsi      # 0x6599
leaq -0x50(%rbp), %rdi
callq 0x497d
testb %al, %al
je 0x40bc
movq -0x30(%rbp), %rax
imull %eax, %ebx
leal (%rax,%rbx), %r14d
decl %r14d
jmp 0x41ac
leaq 0x24d9(%rip), %rsi      # 0x659c
leaq -0x50(%rbp), %rdi
callq 0x497d
testb %al, %al
je 0x40f1
addl %r13d, %r13d
subl %r13d, %r14d
movl %r14d, %eax
cltd
idivl %r12d
imull -0x30(%rbp), %r15d
imull %edx, %r15d
addl %eax, %r15d
movl %r15d, %r14d
jmp 0x41ac
leaq 0x24a7(%rip), %rsi      # 0x659f
leaq -0x50(%rbp), %rdi
callq 0x497d
testb %al, %al
je 0x4128
movq -0x30(%rbp), %rcx
leal (%rcx,%r15,2), %eax
imull %r12d, %eax
subl %eax, %r14d
movl %r14d, %eax
cltd
idivl %r12d
imull %r15d, %edx
leal (%r15,%rdx), %r14d
decl %r14d
jmp 0x4157
leaq 0x2473(%rip), %rsi      # 0x65a2
leaq -0x50(%rbp), %rdi
callq 0x497d
testb %al, %al
je 0x4160
movq -0x30(%rbp), %rcx
leal (%r15,%rcx), %eax
imull %r12d, %eax
addl %eax, %eax
subl %eax, %r14d
movl %r14d, %eax
cltd
idivl %r15d
movl %edx, %r14d
imull %ecx, %r14d
addl %eax, %r14d
jmp 0x41ac
leaq 0x243e(%rip), %rsi      # 0x65a5
leaq -0x50(%rbp), %rdi
callq 0x497d
testb %al, %al
je 0x41f9
movq -0x30(%rbp), %rsi
leal (%r15,%rsi), %eax
movl %r15d, %ecx
imull %esi, %ecx
imull %r12d, %eax
addl %eax, %eax
subl %ecx, %r14d
subl %eax, %r14d
movl %r14d, %eax
cltd
idivl %r15d
imull %esi, %edx
decl %r12d
imull %ecx, %r12d
addl %edx, %r12d
addl %eax, %r12d
movl %r12d, %r14d
leaq -0x50(%rbp), %rdi
callq 0x20a0
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x23cf(%rip), %rsi      # 0x65a8
movq %rax, %rdi
callq 0x22b0
movq 0x4df8(%rip), %rsi      # 0x8fe0
movq 0x4dd9(%rip), %rdx      # 0x8fc8
movq %rbx, %rdi
callq 0x2270
jmp 0x4240
pushq $0x10
popq %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x23b9(%rip), %rsi      # 0x65c4
leaq -0x70(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x498d
movb $0x1, %r15b
leaq -0x70(%rbp), %rsi
movq %rbx, %rdi
callq 0x21c0
xorl %r15d, %r15d
movq 0x4da7(%rip), %rsi      # 0x8fd8
movq 0x4d80(%rip), %rdx      # 0x8fb8
movq %rbx, %rdi
callq 0x2270
movq %rax, %r14
leaq -0x70(%rbp), %rdi
callq 0x20a0
testb %r15b, %r15b
jne 0x4258
jmp 0x4265
jmp 0x4255
movq %rax, %r14
movq %rbx, %rdi
callq 0x20e0
jmp 0x4265
movq %rax, %r14
leaq -0x50(%rbp), %rdi
callq 0x20a0
movq %r14, %rdi
callq 0x2290