#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 S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_ISGID;
int S_IWUSR;
int S_IXGRP;
scalar_t__ chmod(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char *, struct stat *);
int main(void) {
  struct stat statbuf;
  if (stat("before.txt", &statbuf) < 0) {
    perror("stat error for before.txt");
    exit(-1);
  }
  if (chmod("before.txt", (statbuf.st_mode & ~S_IXGRP) | S_ISGID) < 0) {
    perror("chmod error for before.txt");
    exit(-1);
  }
  if (chmod("after.txt", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
    perror("chmod error for after.txt");
    exit(-1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xed4(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1030 <stat@plt>
cmp    $0x0,%rax
jge    117f <main+0x3f>
lea    0xe90(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%esi
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%esi
mov    0x2e39(%rip),%rax
or     (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1040 <chmod@plt>
cmp    $0x0,%rax
jge    11c5 <main+0x85>
lea    0xe64(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
mov    0x2e13(%rip),%rax
or     (%rax),%esi
mov    0x2dea(%rip),%rax
or     (%rax),%esi
mov    0x2dd9(%rip),%rax
or     (%rax),%esi
lea    0xe55(%rip),%rdi
call   1040 <chmod@plt>
cmp    $0x0,%rax
jge    1215 <main+0xd5>
lea    0xe2f(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xee2(%rip),%rdi
mov    %rsp,%rsi
call   1030 <stat@plt>
test   %rax,%rax
js     11b6 <main+0x76>
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
not    %esi
and    (%rsp),%esi
mov    0x2e66(%rip),%rax
or     (%rax),%esi
lea    0xeb7(%rip),%rdi
call   1040 <chmod@plt>
test   %rax,%rax
js     11bf <main+0x7f>
mov    0x2e34(%rip),%rax
mov    0x2e5d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e32(%rip),%rax
or     (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
lea    0xe9d(%rip),%rdi
call   1040 <chmod@plt>
test   %rax,%rax
js     11c8 <main+0x88>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe43(%rip),%rdi
jmp    11cf <main+0x8f>
lea    0xe54(%rip),%rdi
jmp    11cf <main+0x8f>
lea    0xe66(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [11 x i8] c"before.txt\00"
@.str.1 = [26 x i8] c"stat error for before.txt\00"
@S_IXGRP = global i32 0
@S_ISGID = global i32 0
@.str.2 = [27 x i8] c"chmod error for before.txt\00"
@.str.3 = [10 x i8] c"after.txt\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.4 = [26 x i8] c"chmod error for after.txt\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.stat
store i32 0, i32* %1
%3 = call i64 @stat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%7 = call i32 @exit(i32 -1)
unreachable
8: ; preds = %0
%9 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%10 = load i32, i32* %9
%11 = load i32, i32* @S_IXGRP
%12 = xor i32 %11, -1
%13 = and i32 %10, %12
%14 = load i32, i32* @S_ISGID
%15 = or i32 %13, %14
%16 = call i64 @chmod(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %8
%19 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %8
%22 = load i32, i32* @S_IRUSR
%23 = load i32, i32* @S_IWUSR
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IRGRP
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IROTH
%28 = or i32 %26, %27
%29 = call i64 @chmod(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %21
%32 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %21
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [11 x i8] c"before.txt\00"
@.str.1 = [26 x i8] c"stat error for before.txt\00"
@S_IXGRP = global i32 0
@S_ISGID = global i32 0
@.str.2 = [27 x i8] c"chmod error for before.txt\00"
@.str.3 = [10 x i8] c"after.txt\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.4 = [26 x i8] c"chmod error for after.txt\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = call i64 @stat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%7 = call i32 @exit(i32 -1)
unreachable
8: ; preds = %0
%9 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%10 = load i32, i32* %9
%11 = load i32, i32* @S_IXGRP
%12 = xor i32 %11, -1
%13 = and i32 %10, %12
%14 = load i32, i32* @S_ISGID
%15 = or i32 %13, %14
%16 = call i64 @chmod(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %8
%19 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %8
%22 = load i32, i32* @S_IRUSR
%23 = load i32, i32* @S_IWUSR
%24 = or i32 %23, %22
%25 = load i32, i32* @S_IRGRP
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IROTH
%28 = or i32 %26, %27
%29 = call i64 @chmod(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %21
%32 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %21
ret i32 0
}