#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*/ err_sys(char *);
scalar_t__ stat(char *, struct stat *);
int main(int argc, char **argv) {
  struct stat buf;
  if (stat("foo", &buf) < 0) {
    err_sys("stat error on 'foo'");
  }
  if (chmod("foo", (buf.st_mode & ~S_IXGRP) | S_ISGID) < 0) {
    err_sys("chmod error on 'foo'");
  }
  if (chmod("bar", (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
    err_sys("chmod error on 'bar'");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xeb3(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    116c <main+0x3c>
lea    0xe9d(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x18(%rbp),%esi
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%esi
mov    0x2e4c(%rip),%rax
or     (%rax),%esi
lea    0xe73(%rip),%rdi
call   1050 <chmod@plt>
cmp    $0x0,%rax
jge    11a8 <main+0x78>
lea    0xe75(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2e30(%rip),%rax
or     (%rax),%esi
mov    0x2e07(%rip),%rax
or     (%rax),%esi
mov    0x2df6(%rip),%rax
or     (%rax),%esi
lea    0xe5a(%rip),%rdi
call   1050 <chmod@plt>
cmp    $0x0,%rax
jge    11ee <main+0xbe>
lea    0xe48(%rip),%rdi
call   1030 <err_sys@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xec8(%rip),%rdi
mov    %rsp,%rsi
call   1040 <stat@plt>
test   %rax,%rax
jns    1151 <main+0x21>
lea    0xeb8(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
not    %esi
and    (%rsp),%esi
mov    0x2e6a(%rip),%rax
or     (%rax),%esi
lea    0xe91(%rip),%rdi
call   1050 <chmod@plt>
test   %rax,%rax
jns    1185 <main+0x55>
lea    0xe98(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x2e2c(%rip),%rax
mov    0x2e55(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e2a(%rip),%rax
or     (%rax),%esi
mov    0x2e19(%rip),%rax
or     (%rax),%esi
lea    0xe7d(%rip),%rdi
call   1050 <chmod@plt>
test   %rax,%rax
jns    11c6 <main+0x96>
lea    0xe70(%rip),%rdi
call   1030 <err_sys@plt>
xor    %eax,%eax
pop    %rcx
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [4 x i8] c"foo\00"
@.str.1 = [20 x i8] c"stat error on 'foo'\00"
@S_IXGRP = global i32 0
@S_ISGID = global i32 0
@.str.2 = [21 x i8] c"chmod error on '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 = [21 x i8] c"chmod error on 'bar'\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), %struct.stat* %3)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = call i32 @err_sys(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%11 = load i32, i32* %10
%12 = load i32, i32* @S_IXGRP
%13 = xor i32 %12, -1
%14 = and i32 %11, %13
%15 = load i32, i32* @S_ISGID
%16 = or i32 %14, %15
%17 = call i64 @chmod(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %9
%20 = call i32 @err_sys(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %9
%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 %33
31: ; preds = %21
%32 = call i32 @err_sys(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %33
33: ; preds = %31, %21
ret i32 0
}