#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*/ err(int, char *, char *);
scalar_t__ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getc(int /*<<< orphan*/ *);
int link(char *, char *);
int putc(int, int /*<<< orphan*/ *);
void savelog(char *from, char *to) {
  FILE *src, *dst;
  int c, res;
  res = link(from, to);
  if (res == 0)
    return;
  if ((src = fopen(from, "r")) == NULL)
    err(1, "can't fopen %s for reading", from);
  if ((dst = fopen(to, "w")) == NULL)
    err(1, "can't fopen %s for writing", to);
  while ((c = getc(src)) != EOF) {
    if ((putc(c, dst)) == EOF)
      err(1, "error writing to %s", to);
  }
  if (ferror(src))
    err(1, "error reading from %s", from);
  if ((fclose(src)) != 0)
    err(1, "can't fclose %s", to);
  if ((fclose(dst)) != 0)
    err(1, "can't fclose %s", from);
}


x86 assembly (clang, -O0):
<savelog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <link@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    119f <savelog+0x2f>
jmp    12d2 <savelog+0x162>
mov    -0x8(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11d2 <savelog+0x62>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe35(%rip),%rsi
call   1060 <err@plt>
mov    -0x10(%rbp),%rdi
lea    0xe40(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1205 <savelog+0x95>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xe1f(%rip),%rsi
call   1060 <err@plt>
jmp    120a <savelog+0x9a>
mov    -0x18(%rbp),%rdi
call   1090 <getc@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dcb(%rip),%rcx
cmp    (%rcx),%eax
je     125a <savelog+0xea>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1050 <putc@plt>
mov    0x2db0(%rip),%rcx
cmp    (%rcx),%eax
jne    1255 <savelog+0xe5>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xdea(%rip),%rsi
call   1060 <err@plt>
jmp    120a <savelog+0x9a>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1282 <savelog+0x112>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xdd1(%rip),%rsi
call   1060 <err@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     12aa <savelog+0x13a>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xdbf(%rip),%rsi
call   1060 <err@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     12d2 <savelog+0x162>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xd97(%rip),%rsi
call   1060 <err@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<savelog>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
call   1080 <link@plt>
test   %eax,%eax
je     126d <savelog+0xfd>
lea    0xe6b(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b9 <savelog+0x49>
lea    0xe56(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
call   1060 <err@plt>
lea    0xe5d(%rip),%rsi
mov    %r15,%rdi
call   1070 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11e4 <savelog+0x74>
lea    0xe48(%rip),%rsi
mov    $0x1,%edi
mov    %r15,%rdx
call   1060 <err@plt>
mov    %rbx,%rdi
call   1090 <getc@plt>
mov    0x2df5(%rip),%rbp
cmp    0x0(%rbp),%eax
jne    127c <savelog+0x10c>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     121d <savelog+0xad>
lea    0xe3e(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
call   1060 <err@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
je     123e <savelog+0xce>
lea    0xe33(%rip),%rsi
mov    $0x1,%edi
mov    %r15,%rdx
call   1060 <err@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
je     126d <savelog+0xfd>
lea    0xe12(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <err@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb7(%rip),%r13
jmp    12a1 <savelog+0x131>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1090 <getc@plt>
cmp    0x0(%rbp),%eax
je     11fc <savelog+0x8c>
mov    %eax,%edi
mov    %r12,%rsi
call   1050 <putc@plt>
cmp    0x0(%rbp),%eax
jne    1290 <savelog+0x120>
mov    $0x1,%edi
mov    %r13,%rsi
mov    %r15,%rdx
call   1060 <err@plt>
jmp    1290 <savelog+0x120>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [27 x i8] c"can't fopen %s for reading\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [27 x i8] c"can't fopen %s for writing\00"
@EOF = global i32 0
@.str.4 = [20 x i8] c"error writing to %s\00"
@.str.5 = [22 x i8] c"error reading from %s\00"
@.str.6 = [16 x i8] c"can't fclose %s\00"
define void @savelog(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = load i8*, i8** %4
%11 = call i32 @link(i8* %9, i8* %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
br label %66
15: ; preds = %2
%16 = load i8*, i8** %3
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %5
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i8*, i8** %3
%21 = call i32 @err(i32 1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %19, %15
%23 = load i8*, i8** %4
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %24, i32** %6
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i8*, i8** %4
%28 = call i32 @err(i32 1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %26, %22
br label %30
30: ; preds = %44, %29
%31 = load i32*, i32** %5
%32 = call i32 @getc(i32* %31)
store i32 %32, i32* %7
%33 = load i32, i32* @EOF
%34 = icmp ne i32 %32, %33
br i1 %34, label %35, label %45
35: ; preds = %30
%36 = load i32, i32* %7
%37 = load i32*, i32** %6
%38 = call i32 @putc(i32 %36, i32* %37)
%39 = load i32, i32* @EOF
%40 = icmp eq i32 %38, %39
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = load i8*, i8** %4
%43 = call i32 @err(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %42)
br label %44
44: ; preds = %41, %35
br label %30
45: ; preds = %30
%46 = load i32*, i32** %5
%47 = call i64 @ferror(i32* %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = load i8*, i8** %3
%51 = call i32 @err(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %49, %45
%53 = load i32*, i32** %5
%54 = call i64 @fclose(i32* %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %52
%57 = load i8*, i8** %4
%58 = call i32 @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* %57)
br label %59
59: ; preds = %56, %52
%60 = load i32*, i32** %6
%61 = call i64 @fclose(i32* %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %59
%64 = load i8*, i8** %3
%65 = call i32 @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* %64)
br label %66
66: ; preds = %14, %63, %59
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [27 x i8] c"can't fopen %s for reading\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [27 x i8] c"can't fopen %s for writing\00"
@EOF = global i32 0
@.str.4 = [20 x i8] c"error writing to %s\00"
@.str.5 = [22 x i8] c"error reading from %s\00"
@.str.6 = [16 x i8] c"can't fclose %s\00"
define void @savelog(i8* %0, i8* %1) {
%3 = tail call i32 @link(i8* %0, i8* %1)
%4 = icmp eq i32 %3, 0
br i1 %4, label %45, label %5
5: ; preds = %2
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %10
10: ; preds = %8, %5
%11 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %15
15: ; preds = %13, %10
%16 = tail call i32 @getc(i32* %6)
%17 = load i32, i32* @EOF
%18 = icmp eq i32 %16, %17
br i1 %18, label %30, label %19
19: ; preds = %15, %26
%20 = phi i32 [ %27, %26 ], [ %16, %15 ]
%21 = tail call i32 @putc(i32 %20, i32* %11)
%22 = load i32, i32* @EOF
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %1)
br label %26
26: ; preds = %24, %19
%27 = tail call i32 @getc(i32* %6)
%28 = load i32, i32* @EOF
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %19
30: ; preds = %26, %15
%31 = tail call i64 @ferror(i32* %6)
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %35
35: ; preds = %33, %30
%36 = tail call i64 @fclose(i32* %6)
%37 = icmp eq i64 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %35
%39 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* %1)
br label %40
40: ; preds = %38, %35
%41 = tail call i64 @fclose(i32* %11)
%42 = icmp eq i64 %41, 0
br i1 %42, label %45, label %43
43: ; preds = %40
%44 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* %0)
br label %45
45: ; preds = %40, %43, %2
ret void
}