#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 */
struct stat {
  scalar_t__ st_size;
  scalar_t__ st_mtime;
};

/* Variables and functions */
int /*<<< orphan*/ cp(char const *, char const *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, char const *);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ unlink(char const *);
void CopyIfNeeded(const char *to, const char *from) {
  struct stat toStat, fromStat;
  if (stat(to, &toStat) < 0)
    goto copyNeeded;
  if (stat(from, &fromStat) < 0) {
    printf("Can't open source file %s\n", from);
    exit(-1);
  }
  if (fromStat.st_size != toStat.st_size)
    goto copyNeeded;
  if (fromStat.st_mtime >= toStat.st_mtime)
    goto copyNeeded;
  return;
copyNeeded:
  unlink(to);
  cp(to, from);
}


x86 assembly (clang, -O0):
<CopyIfNeeded>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    117c <CopyIfNeeded+0x2c>
jmp    11d8 <CopyIfNeeded+0x88>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    11ad <CopyIfNeeded+0x5d>
mov    -0x10(%rbp),%rsi
lea    0xe62(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     11c0 <CopyIfNeeded+0x70>
jmp    11d8 <CopyIfNeeded+0x88>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jl     11d3 <CopyIfNeeded+0x83>
jmp    11d8 <CopyIfNeeded+0x88>
jmp    11ee <CopyIfNeeded+0x9e>
mov    -0x8(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <cp@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CopyIfNeeded>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x18(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
js     1196 <CopyIfNeeded+0x46>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1060 <stat@plt>
test   %rax,%rax
js     11b1 <CopyIfNeeded+0x61>
mov    0x8(%rsp),%rax
cmp    0x18(%rsp),%rax
jne    1196 <CopyIfNeeded+0x46>
mov    0x10(%rsp),%rax
cmp    0x20(%rsp),%rax
jl     11a9 <CopyIfNeeded+0x59>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <cp@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe48(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64, i64 }
@.str = [27 x i8] c"Can't open source file %s\0A\00"
define void @CopyIfNeeded(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca %struct.stat
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
%8 = call i64 @stat(i8* %7, %struct.stat* %5)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
br label %34
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = call i64 @stat(i8* %12, %struct.stat* %6)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = load i8*, i8** %4
%17 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %11
%20 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%21 = load i64, i64* %20
%22 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%23 = load i64, i64* %22
%24 = icmp ne i64 %21, %23
br i1 %24, label %25, label %26
25: ; preds = %19
br label %34
26: ; preds = %19
%27 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%28 = load i64, i64* %27
%29 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 1
%30 = load i64, i64* %29
%31 = icmp sge i64 %28, %30
br i1 %31, label %32, label %33
32: ; preds = %26
br label %34
33: ; preds = %26
br label %40
34: ; preds = %32, %25, %10
%35 = load i8*, i8** %3
%36 = call i32 @unlink(i8* %35)
%37 = load i8*, i8** %3
%38 = load i8*, i8** %4
%39 = call i32 @cp(i8* %37, i8* %38)
br label %40
40: ; preds = %34, %33
ret void
}

LLVM IR (-O2):
%struct.stat = type { i64, i64 }
@.str = [27 x i8] c"Can't open source file %s\0A\00"
define void @CopyIfNeeded(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %3 to i8*
%6 = bitcast %struct.stat* %4 to i8*
%7 = call i64 @stat(i8* %0, %struct.stat* %3)
%8 = icmp slt i64 %7, 0
br i1 %8, label %27, label %9
9: ; preds = %2
%10 = call i64 @stat(i8* %1, %struct.stat* %4)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %1)
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %9
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%17 = load i64, i64* %16
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%19 = load i64, i64* %18
%20 = icmp eq i64 %17, %19
br i1 %20, label %21, label %27
21: ; preds = %15
%22 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%23 = load i64, i64* %22
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%25 = load i64, i64* %24
%26 = icmp slt i64 %23, %25
br i1 %26, label %30, label %27
27: ; preds = %21, %15, %2
%28 = call i32 @unlink(i8* %0)
%29 = call i32 @cp(i8* %0, i8* %1)
br label %30
30: ; preds = %21, %27
ret void
}