#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 ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ cbuf_copy_in(char *);
int cbuf_copy_out(char *);
int /*<<< orphan*/ cbuf_init();
int /*<<< orphan*/ cbuf_terminate();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ usage();
int main(int argc, char *argv[]) {
  FILE *in, *out;
  char *src, *dst;
  char *line;
  size_t buf_len;
  ssize_t readn;
  char buffer[1024];
  int cbuf_n;
  if (argc < 3)
    usage();
  src = argv[1];
  dst = argv[2];
  if ((in = fopen(src, "r")) == NULL)
    do {
      perror("fopen source");
      exit(EXIT_FAILURE);
    } while (0);
  if ((out = fopen(dst, "w")) == NULL)
    do {
      perror("fopen destination");
      exit(EXIT_FAILURE);
    } while (0);
  line = NULL;
  cbuf_init();
  while ((readn = getline(&line, &buf_len, in)) != -1) {
    cbuf_copy_in(line);
    cbuf_n = cbuf_copy_out(buffer);
    fwrite(buffer, cbuf_n - 1, 1, out);
  }
  cbuf_terminate();
  free(line);
  fclose(in);
  fclose(out);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11ea <main+0x2a>
mov    $0x0,%al
call   1090 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xdf3(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    123f <main+0x7f>
jmp    1225 <main+0x65>
lea    0xdd6(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x30(%rbp),%rdi
lea    0xdc5(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    127c <main+0xbc>
jmp    1262 <main+0xa2>
lea    0xda8(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
movq   $0x0,-0x38(%rbp)
mov    $0x0,%al
call   1070 <cbuf_init@plt>
mov    -0x18(%rbp),%rdx
lea    -0x38(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   10c0 <getline@plt>
mov    %eax,-0x44(%rbp)
cmp    $0xffffffff,%eax
je     12e6 <main+0x126>
mov    -0x38(%rbp),%rdi
call   1050 <cbuf_copy_in@plt>
lea    -0x450(%rbp),%rdi
call   1060 <cbuf_copy_out@plt>
mov    %eax,-0x454(%rbp)
lea    -0x450(%rbp),%rdi
mov    -0x454(%rbp),%esi
sub    $0x1,%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   10e0 <fwrite@plt>
jmp    128b <main+0xcb>
mov    $0x0,%al
call   1080 <cbuf_terminate@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jg     11df <main+0x1f>
xor    %eax,%eax
call   1090 <usage@plt>
mov    0x8(%rbx),%rdi
mov    0x10(%rbx),%rbx
lea    0xe12(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12ba <main+0xfa>
mov    %rax,%r14
lea    0xe09(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12c3 <main+0x103>
mov    %rax,%r15
movq   $0x0,(%rsp)
xor    %eax,%eax
call   1070 <cbuf_init@plt>
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %r14,%rdx
call   10c0 <getline@plt>
cmp    $0xffffffff,%eax
je     1287 <main+0xc7>
lea    0x10(%rsp),%rbx
mov    %rsp,%r12
lea    0x8(%rsp),%r13
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%rdi
call   1050 <cbuf_copy_in@plt>
mov    %rbx,%rdi
call   1060 <cbuf_copy_out@plt>
lea    -0x1(%rax),%esi
mov    %rbx,%rdi
mov    $0x1,%edx
mov    %r15,%rcx
call   10e0 <fwrite@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
call   10c0 <getline@plt>
cmp    $0xffffffff,%eax
jne    1250 <main+0x90>
xor    %eax,%eax
call   1080 <cbuf_terminate@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xd41(%rip),%rdi
jmp    12ca <main+0x10a>
lea    0xd47(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [13 x i8] c"fopen source\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [18 x i8] c"fopen destination\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca [1024 x i8]
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 3
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 (...) @usage()
br label %19
19: ; preds = %17, %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
store i8* %22, i8** %8
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
store i8* %25, i8** %9
%26 = load i8*, i8** %8
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %27, i32** %6
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %35
29: ; preds = %19
br label %30
30: ; preds = %29
%31 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; No predecessors!
br label %35
35: ; preds = %34, %19
%36 = load i8*, i8** %9
%37 = call i32* @fopen(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %37, i32** %7
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %45
39: ; preds = %35
br label %40
40: ; preds = %39
%41 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; No predecessors!
br label %45
45: ; preds = %44, %35
store i8* null, i8** %10
%46 = call i32 (...) @cbuf_init()
br label %47
47: ; preds = %51, %45
%48 = load i32*, i32** %6
%49 = call i32 @getline(i8** %10, i64* %11, i32* %48)
store i32 %49, i32* %12
%50 = icmp ne i32 %49, -1
br i1 %50, label %51, label %61
51: ; preds = %47
%52 = load i8*, i8** %10
%53 = call i32 @cbuf_copy_in(i8* %52)
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%55 = call i32 @cbuf_copy_out(i8* %54)
store i32 %55, i32* %14
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%57 = load i32, i32* %14
%58 = sub nsw i32 %57, 1
%59 = load i32*, i32** %7
%60 = call i32 @fwrite(i8* %56, i32 %58, i32 1, i32* %59)
br label %47
61: ; preds = %47
%62 = call i32 (...) @cbuf_terminate()
%63 = load i8*, i8** %10
%64 = call i32 @free(i8* %63)
%65 = load i32*, i32** %6
%66 = call i32 @fclose(i32* %65)
%67 = load i32*, i32** %7
%68 = call i32 @fclose(i32* %67)
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [13 x i8] c"fopen source\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [18 x i8] c"fopen destination\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca [1024 x i8]
%6 = bitcast i8** %3 to i8*
%7 = bitcast i64* %4 to i8*
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%9 = icmp slt i32 %0, 3
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 (...) @usage()
br label %12
12: ; preds = %10, %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = tail call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %23
19: ; preds = %12
%20 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %12
%24 = tail call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %23
store i8* null, i8** %3
%31 = tail call i32 (...) @cbuf_init()
%32 = call i32 @getline(i8** %3, i64* %4, i32* %17)
%33 = icmp eq i32 %32, -1
br i1 %33, label %42, label %34
34: ; preds = %30, %34
%35 = load i8*, i8** %3
%36 = call i32 @cbuf_copy_in(i8* %35)
%37 = call i32 @cbuf_copy_out(i8* %8)
%38 = add nsw i32 %37, -1
%39 = call i32 @fwrite(i8* %8, i32 %38, i32 1, i32* %24)
%40 = call i32 @getline(i8** %3, i64* %4, i32* %17)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %34
42: ; preds = %34, %30
%43 = call i32 (...) @cbuf_terminate()
%44 = load i8*, i8** %3
%45 = call i32 @free(i8* %44)
%46 = call i32 @fclose(i32* %17)
%47 = call i32 @fclose(i32* %24)
ret i32 0
}