#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_mode;
};

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
scalar_t__ S_ISLNK(int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fchown(int, int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ lchown(char *, int, int);
scalar_t__ lstat(char *, struct stat *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int);
int main(int argc, char *argv[]) {
  struct stat buf;
  int fd;
  if (argc < 3) {
    fprintf(stderr,
            "Uso: %s <filename> <filename>\n"
            "Il primo argomento dev'essere un link simbolico\n",
            argv[0]);
    exit(EXIT_FAILURE);
  }
  if (lstat(argv[1], &buf) < 0) {
    fprintf(stderr, "Err stat: (%d) - %s\n", errno, strerror(errno));
    exit(errno);
  }
  if (S_ISLNK(buf.st_mode)) {
    if (lchown(argv[1], 1001, 1001) < 0) {
      fprintf(stderr, "Err. cannot set User-ID Group-ID on '%s'\n", argv[1]);
      exit(EXIT_FAILURE);
    }
  } else {
    fprintf(stderr, "Err. '%s' is not a symbolic link\n", argv[1]);
    exit(EXIT_FAILURE);
  }
  if ((fd = open(argv[2], O_RDONLY)) < 0) {
    fprintf(stderr, "Err: (%d) - %s: '%s'\n", errno, strerror(errno), argv[2]);
    exit(errno);
  }
  if (fchown(fd, -1, 1001) < 0) {
    fprintf(stderr, "Err. cannot set User-ID Group-ID on '%s'\n", argv[2]);
    exit(EXIT_FAILURE);
  }
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11cc <main+0x4c>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <lstat@plt>
cmp    $0x0,%rax
jge    1232 <main+0xb2>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%edx
mov    %rax,%rcx
lea    0xe32(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%rax
je     1294 <main+0x114>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x3e9,%edx
mov    %edx,%esi
call   1060 <lchown@plt>
cmp    $0x0,%rax
jge    128f <main+0x10f>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    12c1 <main+0x141>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xde2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1336 <main+0x1b6>
mov    0x2cee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2cda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%edx
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%r8
lea    0xd8f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2ca1(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    $0xffffffff,%esi
mov    $0x3e9,%edx
call   1080 <fchown@plt>
cmp    $0x0,%rax
jge    137f <main+0x1ff>
mov    0x2c7f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xcfa(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2c50(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2c3a(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jle    121d <main+0x9d>
mov    0x8(%rbx),%rdi
mov    %rsp,%rsi
call   1040 <lstat@plt>
test   %rax,%rax
js     1235 <main+0xb5>
mov    (%rsp),%edi
call   1030 <S_ISLNK@plt>
test   %rax,%rax
je     126c <main+0xec>
mov    0x8(%rbx),%rdi
mov    $0x3e9,%esi
mov    $0x3e9,%edx
call   1060 <lchown@plt>
test   %rax,%rax
js     1282 <main+0x102>
mov    0x10(%rbx),%rdi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     1291 <main+0x111>
mov    %eax,%edi
mov    $0xffffffff,%esi
mov    $0x3e9,%edx
call   1080 <fchown@plt>
test   %rax,%rax
js     12ce <main+0x14e>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdd0(%rip),%rsi
jmp    12e2 <main+0x162>
mov    0x2d9c(%rip),%rax
mov    (%rax),%ebx
mov    0x2d8b(%rip),%r14
mov    (%r14),%ebp
mov    %ebp,%edi
call   10a0 <strerror@plt>
lea    0xdf9(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r14),%edi
call   1090 <exit@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xe0e(%rip),%rsi
jmp    12e2 <main+0x162>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
jmp    12db <main+0x15b>
mov    0x2d40(%rip),%rax
mov    (%rax),%r14d
mov    0x2d2e(%rip),%r15
mov    (%r15),%ebp
mov    %ebp,%edi
call   10a0 <strerror@plt>
mov    0x10(%rbx),%r8
lea    0xdf9(%rip),%rsi
mov    %r14d,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r15),%edi
call   1090 <exit@plt>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rbx),%rdx
lea    0xd82(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [79 x i8] c"Uso: %s <filename> <filename>\0AIl primo argomento dev'essere un link simbolico\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [21 x i8] c"Err stat: (%d) - %s\0A\00"
@errno = global i32 0
@.str.2 = [42 x i8] c"Err. cannot set User-ID Group-ID on '%s'\0A\00"
@.str.3 = [34 x i8] c"Err. '%s' is not a symbolic link\0A\00"
@O_RDONLY = global i32 0
@.str.4 = [22 x i8] c"Err: (%d) - %s: '%s'\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 3
br i1 %9, label %10, label %18
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i64 @lstat(i8* %21, %struct.stat* %6)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %26, i8* %28)
%30 = load i32, i32* @errno
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %18
%33 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i64 @S_ISLNK(i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %52
37: ; preds = %32
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = call i64 @lchown(i8* %40, i32 1001, i32 1001)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %51
43: ; preds = %37
%44 = load i32, i32* @stderr
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 1
%47 = load i8*, i8** %46
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %47)
%49 = load i32, i32* @EXIT_FAILURE
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %37
br label %60
52: ; preds = %32
%53 = load i32, i32* @stderr
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 1
%56 = load i8*, i8** %55
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %56)
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 @exit(i32 %58)
unreachable
60: ; preds = %51
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 2
%63 = load i8*, i8** %62
%64 = load i32, i32* @O_RDONLY
%65 = call i32 @open(i8* %63, i32 %64)
store i32 %65, i32* %7
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %78
67: ; preds = %60
%68 = load i32, i32* @stderr
%69 = load i32, i32* @errno
%70 = load i32, i32* @errno
%71 = call i8* @strerror(i32 %70)
%72 = load i8**, i8*** %5
%73 = getelementptr inbounds i8*, i8** %72, i64 2
%74 = load i8*, i8** %73
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %69, i8* %71, i8* %74)
%76 = load i32, i32* @errno
%77 = call i32 @exit(i32 %76)
unreachable
78: ; preds = %60
%79 = load i32, i32* %7
%80 = call i64 @fchown(i32 %79, i32 -1, i32 1001)
%81 = icmp slt i64 %80, 0
br i1 %81, label %82, label %90
82: ; preds = %78
%83 = load i32, i32* @stderr
%84 = load i8**, i8*** %5
%85 = getelementptr inbounds i8*, i8** %84, i64 2
%86 = load i8*, i8** %85
%87 = call i32 (i32, i8*, ...) @fprintf(i32 %83, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %86)
%88 = load i32, i32* @EXIT_FAILURE
%89 = call i32 @exit(i32 %88)
unreachable
90: ; preds = %78
%91 = load i32, i32* @EXIT_SUCCESS
ret i32 %91
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [79 x i8] c"Uso: %s <filename> <filename>\0AIl primo argomento dev'essere un link simbolico\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [21 x i8] c"Err stat: (%d) - %s\0A\00"
@errno = global i32 0
@.str.2 = [42 x i8] c"Err. cannot set User-ID Group-ID on '%s'\0A\00"
@.str.3 = [34 x i8] c"Err. '%s' is not a symbolic link\0A\00"
@O_RDONLY = global i32 0
@.str.4 = [22 x i8] c"Err: (%d) - %s: '%s'\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %12
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = call i64 @lstat(i8* %14, %struct.stat* %3)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %19, i8* %20)
%22 = load i32, i32* @errno
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %12
%25 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%26 = load i32, i32* %25
%27 = tail call i64 @S_ISLNK(i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %39, label %29
29: ; preds = %24
%30 = load i8*, i8** %13
%31 = tail call i64 @lchown(i8* %30, i32 1001, i32 1001)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %45
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %13
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = load i32, i32* @EXIT_FAILURE
%38 = tail call i32 @exit(i32 %37)
unreachable
39: ; preds = %24
%40 = load i32, i32* @stderr
%41 = load i8*, i8** %13
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %41)
%43 = load i32, i32* @EXIT_FAILURE
%44 = tail call i32 @exit(i32 %43)
unreachable
45: ; preds = %29
%46 = getelementptr inbounds i8*, i8** %1, i64 2
%47 = load i8*, i8** %46
%48 = load i32, i32* @O_RDONLY
%49 = tail call i32 @open(i8* %47, i32 %48)
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %59
51: ; preds = %45
%52 = load i32, i32* @stderr
%53 = load i32, i32* @errno
%54 = tail call i8* @strerror(i32 %53)
%55 = load i8*, i8** %46
%56 = tail call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %53, i8* %54, i8* %55)
%57 = load i32, i32* @errno
%58 = tail call i32 @exit(i32 %57)
unreachable
59: ; preds = %45
%60 = tail call i64 @fchown(i32 %49, i32 -1, i32 1001)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %68
62: ; preds = %59
%63 = load i32, i32* @stderr
%64 = load i8*, i8** %46
%65 = tail call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %64)
%66 = load i32, i32* @EXIT_FAILURE
%67 = tail call i32 @exit(i32 %66)
unreachable
68: ; preds = %59
%69 = load i32, i32* @EXIT_SUCCESS
ret i32 %69
}