// File: /workspace/github_c_makefile_star2_O3/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, %r13d
movl %edx, %r15d
movq %rsi, -0x30(%rbp)
movl %edi, %ebx
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl %ecx, %r12d
imull %edx, %r12d
movl %edi, %r14d
subl %r12d, %r14d
jge 0x4afb
leaq 0x1ab9(%rip), %rcx      # 0x65b2
jmp 0x4b6b
leal (%r15,%r15), %eax
movl %eax, %ecx
imull %r13d, %ecx
cmpl %ebx, %ecx
jle 0x4b12
leaq 0x1aa5(%rip), %rcx      # 0x65b5
jmp 0x4b6b
movq -0x30(%rbp), %rdx
leal (%rax,%rdx), %ecx
imull %r13d, %ecx
cmpl %ebx, %ecx
jle 0x4b2a
leaq 0x1a90(%rip), %rcx      # 0x65b8
jmp 0x4b6b
leal (%rax,%rdx,2), %eax
imull %r13d, %eax
cmpl %ebx, %eax
jle 0x4b3e
leaq 0x1a7f(%rip), %rcx      # 0x65bb
jmp 0x4b6b
movl %r15d, %ecx
imull %edx, %ecx
addl %eax, %ecx
cmpl %ebx, %ecx
jle 0x4b53
leaq 0x1a6d(%rip), %rcx      # 0x65be
jmp 0x4b6b
leal (%rdx,%rdx), %ecx
imull %r15d, %ecx
addl %ecx, %eax
cmpl %ebx, %eax
jle 0x4d28
leaq 0x1a56(%rip), %rcx      # 0x65c1
leaq -0x50(%rbp), %rdi
movl $0x2, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x2210
leaq 0x1a2d(%rip), %rsi      # 0x65b2
leaq -0x50(%rbp), %rdi
callq 0x2050
testl %eax, %eax
je 0x4c43
leaq 0x1a18(%rip), %rsi      # 0x65b5
leaq -0x50(%rbp), %rdi
callq 0x2050
testl %eax, %eax
je 0x4c49
leaq 0x1a03(%rip), %rsi      # 0x65b8
leaq -0x50(%rbp), %rdi
callq 0x2050
testl %eax, %eax
je 0x4c59
leaq 0x19ee(%rip), %rsi      # 0x65bb
leaq -0x50(%rbp), %rdi
callq 0x2050
testl %eax, %eax
je 0x4c76
leaq 0x19d9(%rip), %rsi      # 0x65be
leaq -0x50(%rbp), %rdi
callq 0x2050
testl %eax, %eax
movq -0x30(%rbp), %r14
je 0x4c99
leaq 0x19c0(%rip), %rsi      # 0x65c1
leaq -0x50(%rbp), %rdi
callq 0x2050
testl %eax, %eax
jne 0x4cdd
leal (%r15,%r14), %eax
movl %r15d, %ecx
imull %r14d, %ecx
imull %r13d, %eax
addl %eax, %eax
subl %ecx, %ebx
subl %eax, %ebx
movl %ebx, %eax
cltd
idivl %r15d
imull %r14d, %edx
decl %r13d
imull %ecx, %r13d
addl %edx, %r13d
addl %eax, %r13d
movl %r13d, %ebx
jmp 0x4cb3
imull -0x30(%rbp), %ebx
jmp 0x4cb3
movq -0x30(%rbp), %rax
imull %eax, %r14d
leal (%rax,%r14), %ebx
decl %ebx
jmp 0x4cb3
addl %r12d, %r12d
subl %r12d, %ebx
movl %ebx, %eax
cltd
idivl %r13d
imull -0x30(%rbp), %r15d
imull %edx, %r15d
addl %eax, %r15d
movl %r15d, %ebx
jmp 0x4cb3
movq -0x30(%rbp), %rcx
leal (%rcx,%r15,2), %eax
imull %r13d, %eax
subl %eax, %ebx
movl %ebx, %eax
cltd
idivl %r13d
imull %r15d, %edx
leal (%r15,%rdx), %ebx
decl %ebx
imull %ecx, %ebx
jmp 0x4cb1
leal (%r15,%r14), %eax
imull %r13d, %eax
addl %eax, %eax
subl %eax, %ebx
movl %ebx, %eax
cltd
idivl %r15d
movl %edx, %ebx
imull %r14d, %ebx
addl %eax, %ebx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x4ccc
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2160
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x20b0
movq %rax, %rbx
leaq 0x18ef(%rip), %rsi      # 0x65e0
leaq -0x70(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x5625
movb $0x1, %r15b
leaq -0x70(%rbp), %rsi
movq %rbx, %rdi
callq 0x21d0
xorl %r15d, %r15d
movq 0x42c1(%rip), %rsi      # 0x8fd8
movq 0x429a(%rip), %rdx      # 0x8fb8
movq %rbx, %rdi
callq 0x2290
jmp 0x4d5a
movl $0x10, %edi
callq 0x20b0
movq %rax, %rbx
leaq 0x1888(%rip), %rsi      # 0x65c4
movq %rax, %rdi
callq 0x22d0
movq 0x4295(%rip), %rsi      # 0x8fe0
movq 0x4276(%rip), %rdx      # 0x8fc8
movq %rbx, %rdi
callq 0x2290
jmp 0x4d7f
movq %rax, %r14
leaq -0x60(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4d78
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2160
testb %r15b, %r15b
jne 0x4d82
jmp 0x4d8f
movq %rax, %r14
movq %rbx, %rdi
callq 0x20f0
jmp 0x4d8f
movq %rax, %r14
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x4da8
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2160
movq %r14, %rdi
callq 0x22b0