#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*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int main(int argc, char const *argv[]) {
  struct stat buf;
  if (stat("foo", &buf) < 0) {
    fprintf(stderr, "stat error for foo\n");
    exit(1);
  }
  if (chmod("foo", (buf.st_mode & ~S_IXGRP) | S_ISGID) < 0) {
    fprintf(stderr, "chmod error for foo\n");
    exit(1);
  }
  if (chmod("bar", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
    fprintf(stderr, "chmod error for bar\n");
    exit(1);
  }
  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    0xea3(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    118f <main+0x4f>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe84(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%esi
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
lea    0xe50(%rip),%rdi
call   1050 <chmod@plt>
cmp    $0x0,%rax
jge    11de <main+0x9e>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    0x2dfa(%rip),%rax
or     (%rax),%esi
mov    0x2dc9(%rip),%rax
or     (%rax),%esi
mov    0x2db8(%rip),%rax
or     (%rax),%esi
lea    0xe24(%rip),%rdi
call   1050 <chmod@plt>
cmp    $0x0,%rax
jge    1237 <main+0xf7>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xeb8(%rip),%rdi
mov    %rsp,%rsi
call   1040 <stat@plt>
test   %rax,%rax
js     11b6 <main+0x76>
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
not    %esi
and    (%rsp),%esi
mov    0x2e5e(%rip),%rax
or     (%rax),%esi
lea    0xe8d(%rip),%rdi
call   1050 <chmod@plt>
test   %rax,%rax
js     11c8 <main+0x88>
mov    0x2e2c(%rip),%rax
mov    0x2e5d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e2a(%rip),%rax
or     (%rax),%esi
mov    0x2e19(%rip),%rax
or     (%rax),%esi
lea    0xe85(%rip),%rdi
call   1050 <chmod@plt>
test   %rax,%rax
js     11da <main+0x9a>
xor    %eax,%eax
pop    %rcx
ret
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
jmp    11ea <main+0xaa>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
jmp    11ea <main+0xaa>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [4 x i8] c"foo\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"stat error for foo\0A\00"
@S_IXGRP = global i32 0
@S_ISGID = global i32 0
@.str.2 = [21 x i8] c"chmod error for foo\0A\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 for bar\0A\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 %11
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 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 %25
21: ; preds = %11
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %11
%26 = load i32, i32* @S_IRUSR
%27 = load i32, i32* @S_IWUSR
%28 = or i32 %27, %26
%29 = load i32, i32* @S_IRGRP
%30 = or i32 %28, %29
%31 = load i32, i32* @S_IROTH
%32 = or i32 %30, %31
%33 = call i64 @chmod(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %25
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %25
ret i32 0
}