#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 {
  int /*<<< orphan*/ st_gid;
  int /*<<< orphan*/ st_uid;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ ENOSYS;
int /*<<< orphan*/ S_ISLNK(int /*<<< orphan*/);
scalar_t__ chmod(char const *, int /*<<< orphan*/);
scalar_t__ chown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
scalar_t__ lchown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ lstat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int copy_filemodes(const char *source, const char *target) {
  struct stat st;
  if (lstat(source, &st) != 0) {
    fprintf(stderr, "Error lstating %s: %s\n", source, strerror(errno));
    return 0;
  }
  if (chmod(target, st.st_mode) != 0) {
    fprintf(stderr, "Error chmoding %s: %s\n", source, strerror(errno));
    return 0;
  }
  if (lchown(target, st.st_uid, st.st_gid) != 0) {
    if (errno == ENOSYS) {
      struct stat tst;
      if (lstat(target, &tst) != 0) {
        fprintf(stderr, "Error lstating %s: %s\n", target, strerror(errno));
        return 0;
      }
      if (!S_ISLNK(tst.st_mode) && chown(target, st.st_uid, st.st_gid) != 0) {
        fprintf(stderr, "Error lchowning %s: %s\n", source, strerror(errno));
        return 0;
      }
    } else {
      fprintf(stderr, "Error lchowning %s: %s\n", source, strerror(errno));
      return 0;
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<copy_filemodes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <lstat@plt>
cmp    $0x0,%rax
je     11df <copy_filemodes+0x6f>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xe32(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1394 <copy_filemodes+0x224>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1080 <chmod@plt>
cmp    $0x0,%rax
je     123a <copy_filemodes+0xca>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xdee(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1394 <copy_filemodes+0x224>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1060 <lchown@plt>
cmp    $0x0,%rax
je     138d <copy_filemodes+0x21d>
mov    0x2d76(%rip),%rax
mov    (%rax),%rax
mov    0x2d84(%rip),%rcx
cmp    (%rcx),%rax
jne    133d <copy_filemodes+0x1cd>
mov    -0x18(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1040 <lstat@plt>
cmp    $0x0,%rax
je     12c9 <copy_filemodes+0x159>
mov    0x2d4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2d31(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    %rax,%rcx
lea    0xd48(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1394 <copy_filemodes+0x224>
mov    -0x30(%rbp),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%eax
jne    1338 <copy_filemodes+0x1c8>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1070 <chown@plt>
cmp    $0x0,%rax
je     1338 <copy_filemodes+0x1c8>
mov    0x2cde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    0x2cc2(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x74(%rbp),%edi
mov    -0x70(%rbp),%rdx
mov    %rax,%rcx
lea    0xd07(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1394 <copy_filemodes+0x224>
jmp    1388 <copy_filemodes+0x218>
mov    0x2c94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x84(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    0x2c75(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x84(%rbp),%edi
mov    -0x80(%rbp),%rdx
mov    %rax,%rcx
lea    0xcb7(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1394 <copy_filemodes+0x224>
jmp    138d <copy_filemodes+0x21d>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_filemodes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1040 <lstat@plt>
test   %rax,%rax
je     11b4 <copy_filemodes+0x44>
mov    0x2e3e(%rip),%rax
mov    (%rax),%ebp
mov    0x2e2d(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
lea    0xe4e(%rip),%rsi
jmp    11e4 <copy_filemodes+0x74>
mov    0x10(%rsp),%esi
mov    %r14,%rdi
call   1080 <chmod@plt>
test   %rax,%rax
je     1204 <copy_filemodes+0x94>
mov    0x2e0c(%rip),%rax
mov    (%rax),%ebp
mov    0x2dfb(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
lea    0xe33(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%r15d
mov    0xc(%rsp),%r12d
mov    %r14,%rdi
mov    %r12d,%esi
mov    %r15d,%edx
call   1060 <lchown@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     11f3 <copy_filemodes+0x83>
mov    0x2da3(%rip),%r13
mov    0x0(%r13),%rdi
mov    0x2db0(%rip),%rax
cmp    (%rax),%rdi
jne    1272 <copy_filemodes+0x102>
lea    0x18(%rsp),%rsi
mov    %r14,%rdi
call   1040 <lstat@plt>
test   %rax,%rax
je     128c <copy_filemodes+0x11c>
mov    0x2d82(%rip),%rax
mov    (%rax),%ebx
mov    0x0(%r13),%rdi
call   1090 <strerror@plt>
lea    0xd98(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
jmp    11e9 <copy_filemodes+0x79>
mov    0x2d5f(%rip),%rax
mov    (%rax),%ebp
call   1090 <strerror@plt>
lea    0xda7(%rip),%rsi
jmp    11e4 <copy_filemodes+0x74>
mov    0x20(%rsp),%edi
call   1030 <S_ISLNK@plt>
test   %eax,%eax
jne    11f3 <copy_filemodes+0x83>
mov    %r14,%rdi
mov    %r12d,%esi
mov    %r15d,%edx
call   1070 <chown@plt>
test   %rax,%rax
je     11f3 <copy_filemodes+0x83>
mov    0x2d1d(%rip),%rax
mov    (%rax),%ebp
mov    0x0(%r13),%rdi
jmp    127b <copy_filemodes+0x10b>

LLVM IR (-O0):
%struct.stat = type { i32, i32, i32 }
@stderr = global i32 0
@.str = [23 x i8] c"Error lstating %s: %s\0A\00"
@errno = global i64 0
@.str.1 = [23 x i8] c"Error chmoding %s: %s\0A\00"
@ENOSYS = global i64 0
@.str.2 = [24 x i8] c"Error lchowning %s: %s\0A\00"
define i32 @copy_filemodes(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca %struct.stat
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = call i64 @lstat(i8* %8, %struct.stat* %6)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %4
%14 = load i64, i64* @errno
%15 = call i8* @strerror(i64 %14)
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %15)
store i32 0, i32* %3
br label %79
17: ; preds = %2
%18 = load i8*, i8** %5
%19 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 2
%20 = load i32, i32* %19
%21 = call i64 @chmod(i8* %18, i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %4
%26 = load i64, i64* @errno
%27 = call i8* @strerror(i64 %26)
%28 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %25, i8* %27)
store i32 0, i32* %3
br label %79
29: ; preds = %17
%30 = load i8*, i8** %5
%31 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i64 @lchown(i8* %30, i32 %32, i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %78
37: ; preds = %29
%38 = load i64, i64* @errno
%39 = load i64, i64* @ENOSYS
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %71
41: ; preds = %37
%42 = load i8*, i8** %5
%43 = call i64 @lstat(i8* %42, %struct.stat* %7)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %41
%46 = load i32, i32* @stderr
%47 = load i8*, i8** %5
%48 = load i64, i64* @errno
%49 = call i8* @strerror(i64 %48)
%50 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %47, i8* %49)
store i32 0, i32* %3
br label %79
51: ; preds = %41
%52 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 2
%53 = load i32, i32* %52
%54 = call i32 @S_ISLNK(i32 %53)
%55 = icmp ne i32 %54, 0
br i1 %55, label %70, label %56
56: ; preds = %51
%57 = load i8*, i8** %5
%58 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%59 = load i32, i32* %58
%60 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%61 = load i32, i32* %60
%62 = call i64 @chown(i8* %57, i32 %59, i32 %61)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %70
64: ; preds = %56
%65 = load i32, i32* @stderr
%66 = load i8*, i8** %4
%67 = load i64, i64* @errno
%68 = call i8* @strerror(i64 %67)
%69 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %66, i8* %68)
store i32 0, i32* %3
br label %79
70: ; preds = %56, %51
br label %77
71: ; preds = %37
%72 = load i32, i32* @stderr
%73 = load i8*, i8** %4
%74 = load i64, i64* @errno
%75 = call i8* @strerror(i64 %74)
%76 = call i32 @fprintf(i32 %72, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %73, i8* %75)
store i32 0, i32* %3
br label %79
77: ; preds = %70
br label %78
78: ; preds = %77, %29
store i32 1, i32* %3
br label %79
79: ; preds = %78, %71, %64, %45, %23, %11
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
%struct.stat = type { i32, i32, i32 }
@stderr = global i32 0
@.str = [23 x i8] c"Error lstating %s: %s\0A\00"
@errno = global i64 0
@.str.1 = [23 x i8] c"Error chmoding %s: %s\0A\00"
@ENOSYS = global i64 0
@.str.2 = [24 x i8] c"Error lchowning %s: %s\0A\00"
define i32 @copy_filemodes(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %3 to i8*
%6 = call i64 @lstat(i8* %0, %struct.stat* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i64, i64* @errno
%11 = tail call i8* @strerror(i64 %10)
%12 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %11)
br label %61
13: ; preds = %2
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 2
%15 = load i32, i32* %14
%16 = tail call i64 @chmod(i8* %1, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = load i64, i64* @errno
%21 = tail call i8* @strerror(i64 %20)
%22 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %21)
br label %61
23: ; preds = %13
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%27 = load i32, i32* %26
%28 = tail call i64 @lchown(i8* %1, i32 %25, i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %61, label %30
30: ; preds = %23
%31 = load i64, i64* @errno
%32 = load i64, i64* @ENOSYS
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %57
34: ; preds = %30
%35 = bitcast %struct.stat* %4 to i8*
%36 = call i64 @lstat(i8* %1, %struct.stat* %4)
%37 = icmp eq i64 %36, 0
br i1 %37, label %43, label %38
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = load i64, i64* @errno
%41 = tail call i8* @strerror(i64 %40)
%42 = tail call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %41)
br label %61
43: ; preds = %34
%44 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 2
%45 = load i32, i32* %44
%46 = tail call i32 @S_ISLNK(i32 %45)
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %56
48: ; preds = %43
%49 = tail call i64 @chown(i8* %1, i32 %25, i32 %27)
%50 = icmp eq i64 %49, 0
br i1 %50, label %56, label %51
51: ; preds = %48
%52 = load i32, i32* @stderr
%53 = load i64, i64* @errno
%54 = tail call i8* @strerror(i64 %53)
%55 = tail call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %54)
br label %61
56: ; preds = %43, %48
br label %61
57: ; preds = %30
%58 = load i32, i32* @stderr
%59 = tail call i8* @strerror(i64 %31)
%60 = tail call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %59)
br label %61
61: ; preds = %23, %56, %38, %51, %57, %18, %8
%62 = phi i32 [ 0, %8 ], [ 0, %18 ], [ 0, %57 ], [ 0, %51 ], [ 0, %38 ], [ 1, %56 ], [ 1, %23 ]
ret i32 %62
}