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

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_ISGID;
int S_IXGRP;
int S_IXOTH;
int S_IXUSR;
scalar_t__ chmod(char *, int);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fchmod(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
scalar_t__ stat(char *, struct stat *);
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 1> <filename 2>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  if (stat(argv[1], &buf) < 0) {
    fprintf(stderr, "Err: (%d) - %s\n", errno, strerror(errno));
    exit(errno);
  }
  if ((fd = open(argv[2], O_RDONLY)) < 0) {
    fprintf(stderr, "Err: (%d) %s - %s\n", errno, argv[2], strerror(errno));
    exit(errno);
  }
  if (chmod(argv[1], (buf.st_mode & ~S_IXGRP) | S_ISGID) < 0) {
    fprintf(stderr, "Err: (%d) - %s\n", errno, strerror(errno));
    exit(errno);
  }
  if (fchmod(fd, (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) <
      0) {
    fprintf(stderr, "Err: (%d) - %s\n", errno, strerror(errno));
    exit(errno);
  }
  close(fd);
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11cc <main+0x4c>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jge    1232 <main+0xb2>
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%edx
mov    %rax,%rcx
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    12af <main+0x12f>
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    -0x34(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    %rax,%r8
lea    0xd99(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%esi
mov    0x2d0f(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%esi
mov    0x2ce9(%rip),%rax
or     (%rax),%esi
call   1070 <chmod@plt>
cmp    $0x0,%rax
jge    132b <main+0x1ab>
mov    0x2cd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2cbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%edx
mov    %rax,%rcx
lea    0xd0d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2c8c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    0x2c5b(%rip),%rax
mov    (%rax),%esi
mov    0x2ca2(%rip),%rax
or     (%rax),%esi
mov    0x2c61(%rip),%rax
or     (%rax),%esi
mov    0x2c80(%rip),%rax
or     (%rax),%esi
mov    0x2c47(%rip),%rax
or     (%rax),%esi
mov    0x2c86(%rip),%rax
or     (%rax),%esi
call   1060 <fchmod@plt>
cmp    $0x0,%rax
jge    13be <main+0x23e>
mov    0x2c46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2c2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2c1e(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    -0x44(%rbp),%edx
mov    %rax,%rcx
lea    0xc7a(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2bf9(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    0x2bbb(%rip),%rax
mov    (%rax),%eax
add    $0x50,%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    1278 <main+0xf8>
mov    0x8(%rbx),%rdi
mov    %rsp,%rsi
call   1050 <stat@plt>
test   %rax,%rax
js     1241 <main+0xc1>
mov    0x10(%rbx),%rdi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     12a0 <main+0x120>
mov    %eax,%ebp
mov    0x2e05(%rip),%r14
mov    (%r14),%esi
not    %esi
and    (%rsp),%esi
mov    0x8(%rbx),%rdi
mov    0x2dda(%rip),%rax
or     (%rax),%esi
call   1070 <chmod@plt>
test   %rax,%rax
js     1241 <main+0xc1>
mov    0x2d9f(%rip),%rax
mov    0x2de8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2da5(%rip),%rax
or     (%rax),%esi
or     (%r14),%esi
mov    0x2d91(%rip),%rax
or     (%rax),%esi
mov    0x2dd0(%rip),%rax
or     (%rax),%esi
mov    %ebp,%edi
call   1060 <fchmod@plt>
test   %rax,%rax
js     1241 <main+0xc1>
mov    %ebp,%edi
call   1030 <close@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d78(%rip),%rax
mov    (%rax),%ebx
mov    0x2d5f(%rip),%r14
mov    (%r14),%ebp
mov    %ebp,%edi
call   10a0 <strerror@plt>
lea    0xdc1(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%r14),%edi
call   1090 <exit@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd75(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d19(%rip),%rax
mov    (%rax),%r14d
mov    0x2cff(%rip),%r15
mov    (%r15),%ebp
mov    0x10(%rbx),%rbx
mov    %ebp,%edi
call   10a0 <strerror@plt>
lea    0xd6d(%rip),%rsi
mov    %r14d,%edi
mov    %ebp,%edx
mov    %rbx,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%r15),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [35 x i8] c"Uso: %s <filename 1> <filename 2>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"Err: (%d) - %s\0A\00"
@errno = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [19 x i8] c"Err: (%d) %s - %s\0A\00"
@S_IXGRP = global i32 0
@S_ISGID = global i32 0
@S_IRUSR = global i32 0
@S_IXUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@S_IXOTH = global i32 0
@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 ([35 x i8], [35 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 @stat(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 ([16 x i8], [16 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 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = load i32, i32* @O_RDONLY
%37 = call i32 @open(i8* %35, i32 %36)
store i32 %37, i32* %7
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %50
39: ; preds = %32
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 2
%44 = load i8*, i8** %43
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %41, i8* %44, i8* %46)
%48 = load i32, i32* @errno
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %32
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 1
%53 = load i8*, i8** %52
%54 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%55 = load i32, i32* %54
%56 = load i32, i32* @S_IXGRP
%57 = xor i32 %56, -1
%58 = and i32 %55, %57
%59 = load i32, i32* @S_ISGID
%60 = or i32 %58, %59
%61 = call i64 @chmod(i8* %53, i32 %60)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %71
63: ; preds = %50
%64 = load i32, i32* @stderr
%65 = load i32, i32* @errno
%66 = load i32, i32* @errno
%67 = call i8* @strerror(i32 %66)
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %64, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %65, i8* %67)
%69 = load i32, i32* @errno
%70 = call i32 @exit(i32 %69)
unreachable
71: ; preds = %50
%72 = load i32, i32* %7
%73 = load i32, i32* @S_IRUSR
%74 = load i32, i32* @S_IXUSR
%75 = or i32 %73, %74
%76 = load i32, i32* @S_IRGRP
%77 = or i32 %75, %76
%78 = load i32, i32* @S_IXGRP
%79 = or i32 %77, %78
%80 = load i32, i32* @S_IROTH
%81 = or i32 %79, %80
%82 = load i32, i32* @S_IXOTH
%83 = or i32 %81, %82
%84 = call i64 @fchmod(i32 %72, i32 %83)
%85 = icmp slt i64 %84, 0
br i1 %85, label %86, label %94
86: ; preds = %71
%87 = load i32, i32* @stderr
%88 = load i32, i32* @errno
%89 = load i32, i32* @errno
%90 = call i8* @strerror(i32 %89)
%91 = call i32 (i32, i8*, ...) @fprintf(i32 %87, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %88, i8* %90)
%92 = load i32, i32* @errno
%93 = call i32 @exit(i32 %92)
unreachable
94: ; preds = %71
%95 = load i32, i32* %7
%96 = call i32 @close(i32 %95)
%97 = load i32, i32* @EXIT_SUCCESS
ret i32 %97
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [35 x i8] c"Uso: %s <filename 1> <filename 2>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"Err: (%d) - %s\0A\00"
@errno = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [19 x i8] c"Err: (%d) %s - %s\0A\00"
@S_IXGRP = global i32 0
@S_ISGID = global i32 0
@S_IRUSR = global i32 0
@S_IXUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@S_IXOTH = global i32 0
@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 ([35 x i8], [35 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 @stat(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 = call i8* @strerror(i32 %19)
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %19, i8* %20)
%22 = load i32, i32* @errno
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %12
%25 = getelementptr inbounds i8*, i8** %1, i64 2
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_RDONLY
%28 = call i32 @open(i8* %26, i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %38
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = load i32, i32* @errno
%33 = load i8*, i8** %25
%34 = call i8* @strerror(i32 %32)
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %32, i8* %33, i8* %34)
%36 = load i32, i32* @errno
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %24
%39 = load i8*, i8** %13
%40 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%41 = load i32, i32* %40
%42 = load i32, i32* @S_IXGRP
%43 = xor i32 %42, -1
%44 = and i32 %41, %43
%45 = load i32, i32* @S_ISGID
%46 = or i32 %44, %45
%47 = call i64 @chmod(i8* %39, i32 %46)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %56
49: ; preds = %38
%50 = load i32, i32* @stderr
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %51, i8* %52)
%54 = load i32, i32* @errno
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %38
%57 = load i32, i32* @S_IRUSR
%58 = load i32, i32* @S_IXUSR
%59 = or i32 %58, %57
%60 = load i32, i32* @S_IRGRP
%61 = or i32 %59, %60
%62 = load i32, i32* @S_IXGRP
%63 = or i32 %61, %62
%64 = load i32, i32* @S_IROTH
%65 = or i32 %63, %64
%66 = load i32, i32* @S_IXOTH
%67 = or i32 %65, %66
%68 = call i64 @fchmod(i32 %28, i32 %67)
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %77
70: ; preds = %56
%71 = load i32, i32* @stderr
%72 = load i32, i32* @errno
%73 = call i8* @strerror(i32 %72)
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %72, i8* %73)
%75 = load i32, i32* @errno
%76 = call i32 @exit(i32 %75)
unreachable
77: ; preds = %56
%78 = call i32 @close(i32 %28)
%79 = load i32, i32* @EXIT_SUCCESS
ret i32 %79
}