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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xece(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    117f <main+0x3f>
lea    0xe90(%rip),%rdi
call   1030 <printf@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    0xe84(%rip),%rdi
call   1050 <chmod@plt>
cmp    $0x0,%rax
jge    11c5 <main+0x85>
lea    0xe5e(%rip),%rdi
call   1030 <printf@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    0xe48(%rip),%rdi
call   1050 <chmod@plt>
cmp    $0x0,%rax
jge    1215 <main+0xd5>
lea    0xe22(%rip),%rdi
call   1030 <printf@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    0xeb8(%rip),%rdi
mov    %rsp,%rsi
call   1050 <stat@plt>
test   %rax,%rax
js     11c6 <main+0x76>
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
not    %esi
and    (%rsp),%esi
mov    0x2e56(%rip),%rax
or     (%rax),%esi
lea    0xe8d(%rip),%rdi
call   1060 <chmod@plt>
test   %rax,%rax
js     11dc <main+0x8c>
mov    0x2e24(%rip),%rax
mov    0x2e4d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e22(%rip),%rax
or     (%rax),%esi
mov    0x2e11(%rip),%rax
or     (%rax),%esi
lea    0xe6c(%rip),%rdi
call   1060 <chmod@plt>
test   %rax,%rax
js     11e5 <main+0x95>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe5b(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
lea    0xe1d(%rip),%rdi
jmp    11ec <main+0x9c>
lea    0xe28(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [4 x i8] c"foo\00"
@.str.1 = [20 x i8] c"stat error for foo\0A\00"
@S_IXGRP = global i32 0
@S_ISGID = global i32 0
@.str.2 = [20 x i8] c"chmod error for foo\00"
@.str.3 = [4 x i8] c"bar\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.4 = [20 x i8] c"chmod error for bar\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.stat
store i32 0, i32* %1
%3 = call i64 @stat(i8* getelementptr inbounds ([4 x i8], [4 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 @printf(i8* getelementptr inbounds ([20 x i8], [20 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 ([4 x i8], [4 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 @printf(i8* getelementptr inbounds ([20 x i8], [20 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 ([4 x i8], [4 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 @printf(i8* getelementptr inbounds ([20 x i8], [20 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 = [4 x i8] c"foo\00"
@S_IXGRP = global i32 0
@S_ISGID = global i32 0
@.str.2 = [20 x i8] c"chmod error for foo\00"
@.str.3 = [4 x i8] c"bar\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.4 = [20 x i8] c"chmod error for bar\00"
@str = [19 x i8] c"stat error for foo\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = call i64 @stat(i8* getelementptr inbounds ([4 x i8], [4 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 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, 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 ([4 x i8], [4 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 @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 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 ([4 x i8], [4 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 @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %21
ret i32 0
}