// File: /workspace/github_c_makefile_star2_O0/Matthias-Wandel[P]imgcomp/src/util.c
// Function: CopyFile
static int CopyFile(char * src, char * dest)
{
    int inputFd, outputFd, openFlags;
    int filePerms;
    struct stat statbuf;
    size_t numRead;
    #define BUF_SIZE 8192
    char buf[BUF_SIZE];

    // Get file modification time from old file.
    stat(src, &statbuf);

    // Open input and output files  
    inputFd = open(src, O_RDONLY, 0);
    if (inputFd == -1){
        fprintf(Log,"CopyFile could not open src %s\n",src);
        exit(-1);
    }
 
    openFlags = O_CREAT | O_WRONLY | O_TRUNC;

    filePerms = 0x1ff;

    outputFd = open(dest, openFlags, filePerms);
    if (outputFd == -1){
        fprintf(Log,"CopyFile could not open dest %s\n",dest);
        exit(-1);
    }

    // Transfer data until we encounter end of input or an error
 
    for(;;){
        numRead = read(inputFd, buf, BUF_SIZE);
        if (numRead <= 0) break;
        if (write(outputFd, buf, numRead) != numRead){
            fprintf(Log,"write error to %s",dest);
            exit(-1);
        }
    }

    if (numRead == -1){
        fprintf(Log,"CopyFile read error from %s\n",src);
        exit(-1);
    }

    close(inputFd);
    close(outputFd);

    {
        struct utimbuf mtime;
        mtime.actime = statbuf.st_ctime;
        mtime.modtime = statbuf.st_mtime;
        utime(dest, &mtime);
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20d0, %rsp           # imm = 0x20D0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x32b0
movq -0x8(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
movb $0x0, %al
callq 0x3410
movl %eax, -0x14(%rbp)
cmpl $-0x1, -0x14(%rbp)
jne 0xc5a1
leaq 0x8a96(%rip), %rax      # 0x15018
movq (%rax), %rdi
movq -0x8(%rbp), %rdx
leaq 0x4e7c(%rip), %rsi      # 0x1140c
movb $0x0, %al
callq 0x3290
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3480
movl $0x241, -0x1c(%rbp)     # imm = 0x241
movl $0x1ff, -0x20(%rbp)     # imm = 0x1FF
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %esi
movl -0x20(%rbp), %edx
movb $0x0, %al
callq 0x3410
movl %eax, -0x18(%rbp)
cmpl $-0x1, -0x18(%rbp)
jne 0xc5ef
leaq 0x8a48(%rip), %rax      # 0x15018
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x4e4e(%rip), %rsi      # 0x1142c
movb $0x0, %al
callq 0x3290
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3480
jmp 0xc5f1
movl -0x14(%rbp), %edi
leaq -0x20c0(%rbp), %rsi
movl $0x2000, %edx           # imm = 0x2000
callq 0x3200
movq %rax, -0xb8(%rbp)
cmpq $0x0, -0xb8(%rbp)
ja 0xc618
jmp 0xc65f
movl -0x18(%rbp), %edi
leaq -0x20c0(%rbp), %rsi
movq -0xb8(%rbp), %rdx
callq 0x30f0
cmpq -0xb8(%rbp), %rax
je 0xc65d
leaq 0x89da(%rip), %rax      # 0x15018
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x4e01(%rip), %rsi      # 0x1144d
movb $0x0, %al
callq 0x3290
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3480
jmp 0xc5f1
cmpq $-0x1, -0xb8(%rbp)
jne 0xc68f
leaq 0x89a8(%rip), %rax      # 0x15018
movq (%rax), %rdi
movq -0x8(%rbp), %rdx
leaq 0x4de1(%rip), %rsi      # 0x1145f
movb $0x0, %al
callq 0x3290
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3480
movl -0x14(%rbp), %edi
callq 0x31e0
movl -0x18(%rbp), %edi
callq 0x31e0
movq -0x48(%rbp), %rax
movq %rax, -0x20d0(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x20c8(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x20d0(%rbp), %rsi
callq 0x3030
xorl %eax, %eax
addq $0x20d0, %rsp           # imm = 0x20D0
popq %rbp
retq