// File: /workspace/github_c_makefile_star2_O1/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 0x4a7b
leaq 0x1b39(%rip), %rcx      # 0x65b2
jmp 0x4aeb
leal (%r15,%r15), %eax
movl %eax, %ecx
imull %r13d, %ecx
cmpl %ebx, %ecx
jle 0x4a92
leaq 0x1b25(%rip), %rcx      # 0x65b5
jmp 0x4aeb
movq -0x30(%rbp), %rdx
leal (%rax,%rdx), %ecx
imull %r13d, %ecx
cmpl %ebx, %ecx
jle 0x4aaa
leaq 0x1b10(%rip), %rcx      # 0x65b8
jmp 0x4aeb
leal (%rax,%rdx,2), %eax
imull %r13d, %eax
cmpl %ebx, %eax
jle 0x4abe
leaq 0x1aff(%rip), %rcx      # 0x65bb
jmp 0x4aeb
movl %r15d, %ecx
imull %edx, %ecx
addl %eax, %ecx
cmpl %ebx, %ecx
jle 0x4ad3
leaq 0x1aed(%rip), %rcx      # 0x65be
jmp 0x4aeb
leal (%rdx,%rdx), %ecx
imull %r15d, %ecx
addl %ecx, %eax
cmpl %ebx, %eax
jle 0x4ca8
leaq 0x1ad6(%rip), %rcx      # 0x65c1
leaq -0x50(%rbp), %rdi
movl $0x2, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x2210
leaq 0x1aad(%rip), %rsi      # 0x65b2
leaq -0x50(%rbp), %rdi
callq 0x2050
testl %eax, %eax
je 0x4bc3
leaq 0x1a98(%rip), %rsi      # 0x65b5
leaq -0x50(%rbp), %rdi
callq 0x2050
testl %eax, %eax
je 0x4bc9
leaq 0x1a83(%rip), %rsi      # 0x65b8
leaq -0x50(%rbp), %rdi
callq 0x2050
testl %eax, %eax
je 0x4bd9
leaq 0x1a6e(%rip), %rsi      # 0x65bb
leaq -0x50(%rbp), %rdi
callq 0x2050
testl %eax, %eax
je 0x4bf6
leaq 0x1a59(%rip), %rsi      # 0x65be
leaq -0x50(%rbp), %rdi
callq 0x2050
testl %eax, %eax
movq -0x30(%rbp), %r14
je 0x4c19
leaq 0x1a40(%rip), %rsi      # 0x65c1
leaq -0x50(%rbp), %rdi
callq 0x2050
testl %eax, %eax
jne 0x4c5d
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 0x4c33
imull -0x30(%rbp), %ebx
jmp 0x4c33
movq -0x30(%rbp), %rax
imull %eax, %r14d
leal (%rax,%r14), %ebx
decl %ebx
jmp 0x4c33
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 0x4c33
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 0x4c31
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 0x4c4c
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 0x196f(%rip), %rsi      # 0x65e0
leaq -0x70(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x55a5
movb $0x1, %r15b
leaq -0x70(%rbp), %rsi
movq %rbx, %rdi
callq 0x21d0
xorl %r15d, %r15d
movq 0x4341(%rip), %rsi      # 0x8fd8
movq 0x431a(%rip), %rdx      # 0x8fb8
movq %rbx, %rdi
callq 0x2290
jmp 0x4cda
movl $0x10, %edi
callq 0x20b0
movq %rax, %rbx
leaq 0x1908(%rip), %rsi      # 0x65c4
movq %rax, %rdi
callq 0x22d0
movq 0x4315(%rip), %rsi      # 0x8fe0
movq 0x42f6(%rip), %rdx      # 0x8fc8
movq %rbx, %rdi
callq 0x2290
movq %rax, %r14
jmp 0x4d08
movq %rax, %r14
leaq -0x60(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4d03
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2160
jmp 0x4d03
movq %rax, %r14
movb $0x1, %r15b
testb %r15b, %r15b
je 0x4d15
movq %rbx, %rdi
callq 0x20f0
jmp 0x4d15
movq %rax, %r14
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x4d2e
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2160
movq %r14, %rdi
callq 0x22b0