#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ usage();
int /*<<< orphan*/ write(unsigned char *, size_t, char *);
int main(int argc, char *argv[]) {
  unsigned char *src = NULL;
  size_t src_size;
  printf("MicroCabin (MSX) data cut tool ("
         "Mar 15 2022"
         ") (c)RuRuRu\n");
  if (argc < 3) {
    usage();
    return -1;
  }
  {
    FILE *fh = fopen(argv[1], "rb");
    if (fh == NULL) {
      printf("error: can't open %s\n", argv[1]);
      return -2;
    }
    fseek(fh, 0, SEEK_END);
    src_size = ftell(fh);
    fseek(fh, 0, SEEK_SET);
    src = (unsigned char *)malloc(src_size);
    fread(src, 1, src_size, fh);
    fclose(fh);
  }
  write(src, src_size, argv[2]);
  free(src);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
cmpl   $0x3,-0x8(%rbp)
jge    11e9 <main+0x49>
mov    $0x0,%al
call   10b0 <usage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <main+0x10d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe40(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122e <main+0x8e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12ad <main+0x10d>
mov    -0x28(%rbp),%rdi
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
call   1050 <write@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebx
lea    0xe55(%rip),%rdi
call   1040 <puts@plt>
cmp    $0x2,%ebx
jg     11e0 <main+0x30>
xor    %eax,%eax
call   10c0 <usage@plt>
mov    $0xffffffff,%r12d
jmp    1283 <main+0xd3>
mov    0x8(%r14),%rdi
lea    0xe15(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     126b <main+0xbb>
mov    %rax,%rbx
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
xor    %r12d,%r12d
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %rbx,%rdi
call   1090 <ftell@plt>
mov    %rax,%r15
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %r15,%rdi
call   10a0 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    %rbx,%rdi
call   1070 <fclose@plt>
mov    0x10(%r14),%rdx
mov    %rbp,%rdi
mov    %r15,%rsi
call   1060 <write@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    1283 <main+0xd3>
mov    0x8(%r14),%rsi
lea    0xd8d(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0xfffffffe,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [56 x i8] c"MicroCabin (MSX) data cut tool (Mar 15 2022) (c)RuRuRu\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [22 x i8] c"error: can't open %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (...) @usage()
store i32 -1, i32* %3
br label %52
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %8
%19 = load i32*, i32** %8
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %26
21: ; preds = %14
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %24)
store i32 -2, i32* %3
br label %52
26: ; preds = %14
%27 = load i32*, i32** %8
%28 = load i32, i32* @SEEK_END
%29 = call i32 @fseek(i32* %27, i32 0, i32 %28)
%30 = load i32*, i32** %8
%31 = call i64 @ftell(i32* %30)
store i64 %31, i64* %7
%32 = load i32*, i32** %8
%33 = load i32, i32* @SEEK_SET
%34 = call i32 @fseek(i32* %32, i32 0, i32 %33)
%35 = load i64, i64* %7
%36 = call i64 @malloc(i64 %35)
%37 = inttoptr i64 %36 to i8*
store i8* %37, i8** %6
%38 = load i8*, i8** %6
%39 = load i64, i64* %7
%40 = load i32*, i32** %8
%41 = call i32 @fread(i8* %38, i32 1, i64 %39, i32* %40)
%42 = load i32*, i32** %8
%43 = call i32 @fclose(i32* %42)
%44 = load i8*, i8** %6
%45 = load i64, i64* %7
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 2
%48 = load i8*, i8** %47
%49 = call i32 @write(i8* %44, i64 %45, i8* %48)
%50 = load i8*, i8** %6
%51 = call i32 @free(i8* %50)
br label %52
52: ; preds = %26, %21, %12
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [22 x i8] c"error: can't open %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@str = [55 x i8] c"MicroCabin (MSX) data cut tool (Mar 15 2022) (c)RuRuRu\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
%4 = icmp slt i32 %0, 3
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (...) @usage()
br label %29
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i8*, i8** %8
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %13)
br label %29
15: ; preds = %7
%16 = load i32, i32* @SEEK_END
%17 = tail call i32 @fseek(i32* %10, i32 0, i32 %16)
%18 = tail call i64 @ftell(i32* %10)
%19 = load i32, i32* @SEEK_SET
%20 = tail call i32 @fseek(i32* %10, i32 0, i32 %19)
%21 = tail call i64 @malloc(i64 %18)
%22 = inttoptr i64 %21 to i8*
%23 = tail call i32 @fread(i8* %22, i32 1, i64 %18, i32* %10)
%24 = tail call i32 @fclose(i32* %10)
%25 = getelementptr inbounds i8*, i8** %1, i64 2
%26 = load i8*, i8** %25
%27 = tail call i32 @write(i8* %22, i64 %18, i8* %26)
%28 = tail call i32 @free(i8* %22)
br label %29
29: ; preds = %12, %15, %5
%30 = phi i32 [ -1, %5 ], [ 0, %15 ], [ -2, %12 ]
ret i32 %30
}