#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 EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fputc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ system(char *);
int cp_by_fgetc_fputc(const char *source, const char *dest) {
  int ch;
  FILE *source_fp, *dest_fp;
  source_fp = fopen(source, "r");
  if (source_fp == NULL)
    return 1;
  dest_fp = fopen(dest, "w");
  if (dest_fp == NULL) {
    fclose(source_fp);
    return 2;
  }
  while ((ch = fgetc(source_fp)) != EOF) {
    if (fputc(ch, dest_fp) == EOF) {
      fclose(dest_fp);
      fclose(source_fp);
      char rm_file[20] = "rm ";
      strcat(rm_file, dest);
      system(rm_file);
      return 3;
    }
  }
  fclose(source_fp);
  fclose(dest_fp);
  return 0;
}


x86 assembly (clang, -O0):
<cp_by_fgetc_fputc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119b <cp_by_fgetc_fputc+0x3b>
movl   $0x1,-0x4(%rbp)
jmp    127b <cp_by_fgetc_fputc+0x11b>
mov    -0x18(%rbp),%rdi
lea    0xe5c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11cf <cp_by_fgetc_fputc+0x6f>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x2,-0x4(%rbp)
jmp    127b <cp_by_fgetc_fputc+0x11b>
jmp    11d4 <cp_by_fgetc_fputc+0x74>
mov    -0x28(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e01(%rip),%rcx
cmp    (%rcx),%eax
je     1262 <cp_by_fgetc_fputc+0x102>
mov    -0x1c(%rbp),%edi
mov    -0x30(%rbp),%rsi
call   1060 <fputc@plt>
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%eax
jne    125d <cp_by_fgetc_fputc+0xfd>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    0xded(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0xdea(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0xde8(%rip),%eax
mov    %eax,-0x40(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strcat@plt>
lea    -0x50(%rbp),%rdi
call   1040 <system@plt>
movl   $0x3,-0x4(%rbp)
jmp    127b <cp_by_fgetc_fputc+0x11b>
jmp    11d4 <cp_by_fgetc_fputc+0x74>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cp_by_fgetc_fputc>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
lea    0xe8b(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11fa <cp_by_fgetc_fputc+0x9a>
mov    %rax,%r15
lea    0xe79(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1201 <cp_by_fgetc_fputc+0xa1>
mov    %rax,%r12
mov    0x2e48(%rip),%rbx
mov    %r15,%rdi
call   1050 <fgetc@plt>
cmp    (%rbx),%eax
je     1210 <cp_by_fgetc_fputc+0xb0>
mov    %eax,%edi
mov    %r12,%rsi
call   1060 <fputc@plt>
cmp    (%rbx),%eax
jne    11a0 <cp_by_fgetc_fputc+0x40>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    %r15,%rdi
call   1030 <fclose@plt>
movaps 0xe3f(%rip),%xmm0
movaps %xmm0,(%rsp)
movl   $0x0,0x10(%rsp)
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rdi
call   1040 <system@plt>
mov    $0x3,%eax
jmp    1222 <cp_by_fgetc_fputc+0xc2>
mov    $0x1,%eax
jmp    1222 <cp_by_fgetc_fputc+0xc2>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    $0x2,%eax
jmp    1222 <cp_by_fgetc_fputc+0xc2>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %r12,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@EOF = global i32 0
@__const.cp_by_fgetc_fputc.rm_file = [20 x i8] c"rm \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
define i32 @cp_by_fgetc_fputc(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca [20 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 1, i32* %3
br label %52
15: ; preds = %2
%16 = load i8*, i8** %5
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %8
%18 = load i32*, i32** %8
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32*, i32** %7
%22 = call i32 @fclose(i32* %21)
store i32 2, i32* %3
br label %52
23: ; preds = %15
br label %24
24: ; preds = %46, %23
%25 = load i32*, i32** %7
%26 = call i32 @fgetc(i32* %25)
store i32 %26, i32* %6
%27 = load i32, i32* @EOF
%28 = icmp ne i32 %26, %27
br i1 %28, label %29, label %47
29: ; preds = %24
%30 = load i32, i32* %6
%31 = load i32*, i32** %8
%32 = call i32 @fputc(i32 %30, i32* %31)
%33 = load i32, i32* @EOF
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %46
35: ; preds = %29
%36 = load i32*, i32** %8
%37 = call i32 @fclose(i32* %36)
%38 = load i32*, i32** %7
%39 = call i32 @fclose(i32* %38)
%40 = bitcast [20 x i8]* %9 to i8*
%41 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%42 = load i8*, i8** %5
%43 = call i32 @strcat(i8* %41, i8* %42)
%44 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%45 = call i32 @system(i8* %44)
store i32 3, i32* %3
br label %52
46: ; preds = %29
br label %24
47: ; preds = %24
%48 = load i32*, i32** %7
%49 = call i32 @fclose(i32* %48)
%50 = load i32*, i32** %8
%51 = call i32 @fclose(i32* %50)
store i32 0, i32* %3
br label %52
52: ; preds = %47, %35, %20, %14
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@EOF = global i32 0
@__const.cp_by_fgetc_fputc.rm_file = [20 x i8] c"rm \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
define i32 @cp_by_fgetc_fputc(i8* nocapture %0, i8* %1) {
%3 = alloca [20 x i8]
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %28, label %6
6: ; preds = %2
%7 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @fclose(i32* %4)
br label %28
11: ; preds = %6, %15
%12 = tail call i32 @fgetc(i32* %4)
%13 = load i32, i32* @EOF
%14 = icmp eq i32 %12, %13
br i1 %14, label %25, label %15
15: ; preds = %11
%16 = tail call i32 @fputc(i32 %12, i32* %7)
%17 = load i32, i32* @EOF
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %11
19: ; preds = %15
%20 = tail call i32 @fclose(i32* %7)
%21 = tail call i32 @fclose(i32* %4)
%22 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%23 = call i32 @strcat(i8* %22, i8* %1)
%24 = call i32 @system(i8* %22)
br label %28
25: ; preds = %11
%26 = tail call i32 @fclose(i32* %4)
%27 = tail call i32 @fclose(i32* %7)
br label %28
28: ; preds = %2, %25, %19, %9
%29 = phi i32 [ 2, %9 ], [ 3, %19 ], [ 0, %25 ], [ 1, %2 ]
ret i32 %29
}