#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 timespec {
  scalar_t__ tv_nsec;
  int /*<<< orphan*/ tv_sec;
};
struct stat {
  int /*<<< orphan*/ st_gid;
  int /*<<< orphan*/ st_uid;
  int /*<<< orphan*/ st_mode;
  int /*<<< orphan*/ st_mtime;
  int /*<<< orphan*/ st_atime;
};

/* Variables and functions */
int /*<<< orphan*/ AT_FDCWD;
int /*<<< orphan*/ AT_SYMLINK_NOFOLLOW;
int chmod(char const *, int /*<<< orphan*/);
int chown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
int stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
int utimensat(int /*<<< orphan*/, char const *, struct timespec *,
              int /*<<< orphan*/);
int copy_attributes(const char *old, const char *new, int preserve) {
  struct stat oldstat;
  struct timespec timespecs[2];
  if (stat(old, &oldstat) == -1) {
    fprintf(stderr, "Cannot call stat on %s: ", old);
    perror(NULL);
    return -1;
  }
  timespecs[0].tv_sec = oldstat.st_atime;
  timespecs[0].tv_nsec = 0;
  timespecs[1].tv_sec = oldstat.st_mtime;
  timespecs[1].tv_nsec = 0;
  if (utimensat(AT_FDCWD, new, timespecs, AT_SYMLINK_NOFOLLOW) == -1) {
    fprintf(stderr,
            "Cannot set modification and access times of file %s.\n"
            "Times on file %s will be clobbered: ",
            new, old);
    perror(NULL);
    if (preserve & 1)
      return -1;
  }
  if (preserve & 2)
    return 0;
  if (chmod(new, oldstat.st_mode) == -1) {
    fprintf(stderr,
            "Cannot set permission of file %s.\n"
            "Permission of file %s will be clobbered: ",
            new, old);
    perror(NULL);
    if (preserve & 1)
      return -1;
  }
  if (chown(new, oldstat.st_uid, oldstat.st_gid) == -1) {
    fprintf(stderr,
            "Cannot set ownership of file %s.\n"
            "Ownership of file %s will be clobbered: ",
            new, old);
    perror(NULL);
    if (preserve & 1)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<copy_attributes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
jne    11b9 <copy_attributes+0x59>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe63(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1324 <copy_attributes+0x1c4>
mov    -0x20(%rbp),%eax
mov    %eax,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
lea    -0x50(%rbp),%rdx
mov    0x2deb(%rip),%rax
mov    (%rax),%ecx
call   1030 <utimensat@plt>
cmp    $0xffffffff,%eax
jne    1245 <copy_attributes+0xe5>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe04(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <perror@plt>
mov    -0x1c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1240 <copy_attributes+0xe0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1324 <copy_attributes+0x1c4>
jmp    1245 <copy_attributes+0xe5>
mov    -0x1c(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     1260 <copy_attributes+0x100>
movl   $0x0,-0x4(%rbp)
jmp    1324 <copy_attributes+0x1c4>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1070 <chmod@plt>
cmp    $0xffffffff,%eax
jne    12bd <copy_attributes+0x15d>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xde6(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <perror@plt>
mov    -0x1c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     12b8 <copy_attributes+0x158>
movl   $0xffffffff,-0x4(%rbp)
jmp    1324 <copy_attributes+0x1c4>
jmp    12bd <copy_attributes+0x15d>
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x30(%rbp),%edx
call   1060 <chown@plt>
cmp    $0xffffffff,%eax
jne    131d <copy_attributes+0x1bd>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xdd2(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <perror@plt>
mov    -0x1c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1318 <copy_attributes+0x1b8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1324 <copy_attributes+0x1c4>
jmp    131d <copy_attributes+0x1bd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_attributes>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
je     120b <copy_attributes+0xab>
mov    0x14(%rsp),%eax
mov    0x18(%rsp),%ecx
mov    %ecx,0x28(%rsp)
movq   $0x0,0x20(%rsp)
mov    %eax,0x38(%rsp)
movq   $0x0,0x30(%rsp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%ecx
lea    0x20(%rsp),%rdx
mov    %rbp,%rsi
call   1030 <utimensat@plt>
cmp    $0xffffffff,%eax
je     1233 <copy_attributes+0xd3>
xor    %eax,%eax
test   $0x2,%r14b
jne    1202 <copy_attributes+0xa2>
mov    0x10(%rsp),%esi
mov    %rbp,%rdi
call   1070 <chmod@plt>
cmp    $0xffffffff,%eax
je     1268 <copy_attributes+0x108>
mov    0x8(%rsp),%edx
mov    0xc(%rsp),%esi
mov    %rbp,%rdi
call   1060 <chown@plt>
cmp    $0xffffffff,%eax
je     12a0 <copy_attributes+0x140>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %edi,%edi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    1202 <copy_attributes+0xa2>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
mov    %rbp,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %edi,%edi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
test   $0x1,%r14b
je     11ca <copy_attributes+0x6a>
jmp    1202 <copy_attributes+0xa2>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
mov    %rbp,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %edi,%edi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
test   $0x1,%r14b
je     11e7 <copy_attributes+0x87>
jmp    1202 <copy_attributes+0xa2>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
mov    %rbp,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %edi,%edi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
test   $0x1,%r14b
je     1200 <copy_attributes+0xa0>
jmp    1202 <copy_attributes+0xa2>

LLVM IR (-O0):
%struct.stat = type { i32, i32, i32, i32, i32 }
%struct.timespec = type { i64, i32 }
@stderr = global i32 0
@.str = [25 x i8] c"Cannot call stat on %s: \00"
@AT_FDCWD = global i32 0
@AT_SYMLINK_NOFOLLOW = global i32 0
@.str.1 = [90 x i8] c"Cannot set modification and access times of file %s.\0ATimes on file %s will be clobbered: \00"
@.str.2 = [76 x i8] c"Cannot set permission of file %s.\0APermission of file %s will be clobbered: \00"
@.str.3 = [74 x i8] c"Cannot set ownership of file %s.\0AOwnership of file %s will be clobbered: \00"
define i32 @copy_attributes(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca [2 x %struct.timespec]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = call i32 @stat(i8* %10, %struct.stat* %8)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %5
%16 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @perror(i32* null)
store i32 -1, i32* %4
br label %90
18: ; preds = %3
%19 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 4
%20 = load i32, i32* %19
%21 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %9, i64 0, i64 0
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %21, i32 0, i32 1
store i32 %20, i32* %22
%23 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %9, i64 0, i64 0
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %23, i32 0, i32 0
store i64 0, i64* %24
%25 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 3
%26 = load i32, i32* %25
%27 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %9, i64 0, i64 1
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %27, i32 0, i32 1
store i32 %26, i32* %28
%29 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %9, i64 0, i64 1
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %29, i32 0, i32 0
store i64 0, i64* %30
%31 = load i32, i32* @AT_FDCWD
%32 = load i8*, i8** %6
%33 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %9, i64 0, i64 0
%34 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%35 = call i32 @utimensat(i32 %31, i8* %32, %struct.timespec* %33, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %48
37: ; preds = %18
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %6
%40 = load i8*, i8** %5
%41 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.1, i64 0, i64 0), i8* %39, i8* %40)
%42 = call i32 @perror(i32* null)
%43 = load i32, i32* %7
%44 = and i32 %43, 1
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %37
store i32 -1, i32* %4
br label %90
47: ; preds = %37
br label %48
48: ; preds = %47, %18
%49 = load i32, i32* %7
%50 = and i32 %49, 2
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %48
store i32 0, i32* %4
br label %90
53: ; preds = %48
%54 = load i8*, i8** %6
%55 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 2
%56 = load i32, i32* %55
%57 = call i32 @chmod(i8* %54, i32 %56)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %70
59: ; preds = %53
%60 = load i32, i32* @stderr
%61 = load i8*, i8** %6
%62 = load i8*, i8** %5
%63 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.2, i64 0, i64 0), i8* %61, i8* %62)
%64 = call i32 @perror(i32* null)
%65 = load i32, i32* %7
%66 = and i32 %65, 1
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %59
store i32 -1, i32* %4
br label %90
69: ; preds = %59
br label %70
70: ; preds = %69, %53
%71 = load i8*, i8** %6
%72 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%73 = load i32, i32* %72
%74 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%75 = load i32, i32* %74
%76 = call i32 @chown(i8* %71, i32 %73, i32 %75)
%77 = icmp eq i32 %76, -1
br i1 %77, label %78, label %89
78: ; preds = %70
%79 = load i32, i32* @stderr
%80 = load i8*, i8** %6
%81 = load i8*, i8** %5
%82 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %79, i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str.3, i64 0, i64 0), i8* %80, i8* %81)
%83 = call i32 @perror(i32* null)
%84 = load i32, i32* %7
%85 = and i32 %84, 1
%86 = icmp ne i32 %85, 0
br i1 %86, label %87, label %88
87: ; preds = %78
store i32 -1, i32* %4
br label %90
88: ; preds = %78
br label %89
89: ; preds = %88, %70
store i32 0, i32* %4
br label %90
90: ; preds = %89, %87, %68, %52, %46, %13
%91 = load i32, i32* %4
ret i32 %91
}

LLVM IR (-O2):
%struct.stat = type { i32, i32, i32, i32, i32 }
%struct.timespec = type { i64, i32 }
@stderr = global i32 0
@.str = [25 x i8] c"Cannot call stat on %s: \00"
@AT_FDCWD = global i32 0
@AT_SYMLINK_NOFOLLOW = global i32 0
@.str.1 = [90 x i8] c"Cannot set modification and access times of file %s.\0ATimes on file %s will be clobbered: \00"
@.str.2 = [76 x i8] c"Cannot set permission of file %s.\0APermission of file %s will be clobbered: \00"
@.str.3 = [74 x i8] c"Cannot set ownership of file %s.\0AOwnership of file %s will be clobbered: \00"
define i32 @copy_attributes(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.stat
%5 = alloca [2 x %struct.timespec]
%6 = bitcast %struct.stat* %4 to i8*
%7 = bitcast [2 x %struct.timespec]* %5 to i8*
%8 = call i32 @stat(i8* %0, %struct.stat* %4)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0)
%13 = tail call i32 @perror(i32* null)
br label %62
14: ; preds = %3
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 4
%16 = load i32, i32* %15
%17 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %5, i64 0, i64 0
%18 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %5, i64 0, i64 0, i32 1
store i32 %16, i32* %18
%19 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %5, i64 0, i64 0, i32 0
store i64 0, i64* %19
%20 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 3
%21 = load i32, i32* %20
%22 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %5, i64 0, i64 1, i32 1
store i32 %21, i32* %22
%23 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %5, i64 0, i64 1, i32 0
store i64 0, i64* %23
%24 = load i32, i32* @AT_FDCWD
%25 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%26 = call i32 @utimensat(i32 %24, i8* %1, %struct.timespec* %17, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %34
28: ; preds = %14
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %0)
%31 = call i32 @perror(i32* null)
%32 = and i32 %2, 1
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %62
34: ; preds = %28, %14
%35 = and i32 %2, 2
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %62
37: ; preds = %34
%38 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 2
%39 = load i32, i32* %38
%40 = call i32 @chmod(i8* %1, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %48
42: ; preds = %37
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %0)
%45 = call i32 @perror(i32* null)
%46 = and i32 %2, 1
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %62
48: ; preds = %42, %37
%49 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%50 = load i32, i32* %49
%51 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 @chown(i8* %1, i32 %50, i32 %52)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %61
55: ; preds = %48
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str.3, i64 0, i64 0), i8* %1, i8* %0)
%58 = call i32 @perror(i32* null)
%59 = and i32 %2, 1
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %55, %48
br label %62
62: ; preds = %55, %42, %34, %28, %61, %10
%63 = phi i32 [ -1, %10 ], [ 0, %61 ], [ -1, %28 ], [ 0, %34 ], [ -1, %42 ], [ -1, %55 ]
ret i32 %63
}