#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_ISUID;
int S_IWUSR;
int S_IXGRP;
int chmod(char *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  struct stat statbuf;
  if (stat("foo", &statbuf) < 0) {
    fprintf(stderr, "can't stat foo\n");
    exit(EXIT_FAILURE);
  }
  if (chmod("foo", (statbuf.st_mode & ~S_IXGRP) | S_ISUID) == -1) {
    fprintf(stderr, "can't chmod foo\n");
    exit(EXIT_FAILURE);
  }
  if (chmod("bar", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) {
    fprintf(stderr, "can't chmod bar\n");
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}


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    1193 <main+0x53>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe84(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%esi
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%esi
mov    0x2e3d(%rip),%rax
or     (%rax),%esi
lea    0xe4c(%rip),%rdi
call   1050 <chmod@plt>
cmp    $0xffffffff,%eax
jne    11e5 <main+0xa5>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    0x2deb(%rip),%rax
or     (%rax),%esi
mov    0x2dc2(%rip),%rax
or     (%rax),%esi
mov    0x2db1(%rip),%rax
or     (%rax),%esi
lea    0xe15(%rip),%rdi
call   1050 <chmod@plt>
cmp    $0xffffffff,%eax
jne    1241 <main+0x101>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xeb8(%rip),%rdi
mov    %rsp,%rsi
call   1040 <stat@plt>
test   %rax,%rax
jns    1167 <main+0x27>
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
lea    0xe9f(%rip),%rsi
jmp    11e6 <main+0xa6>
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
not    %esi
and    (%rsp),%esi
mov    0x2e6c(%rip),%rax
or     (%rax),%esi
lea    0xe7b(%rip),%rdi
call   1050 <chmod@plt>
cmp    $0xffffffff,%eax
jne    11a1 <main+0x61>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe75(%rip),%rsi
jmp    11e6 <main+0xa6>
mov    0x2e00(%rip),%rax
mov    0x2e31(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e06(%rip),%rax
or     (%rax),%esi
mov    0x2df5(%rip),%rax
or     (%rax),%esi
lea    0xe59(%rip),%rdi
call   1050 <chmod@plt>
cmp    $0xffffffff,%eax
jne    11f9 <main+0xb9>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%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 = [16 x i8] c"can't stat foo\0A\00"
@EXIT_FAILURE = global i32 0
@S_IXGRP = global i32 0
@S_ISUID = global i32 0
@.str.2 = [17 x i8] c"can't chmod 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 = [17 x i8] c"can't chmod bar\0A\00"
@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
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 %14
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load i32, i32* @S_IXGRP
%18 = xor i32 %17, -1
%19 = and i32 %16, %18
%20 = load i32, i32* @S_ISUID
%21 = or i32 %19, %20
%22 = call i32 @chmod(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %14
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %14
%30 = load i32, i32* @S_IRUSR
%31 = load i32, i32* @S_IWUSR
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IRGRP
%34 = or i32 %32, %33
%35 = load i32, i32* @S_IROTH
%36 = or i32 %34, %35
%37 = call i32 @chmod(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %44
39: ; preds = %29
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %29
%45 = load i32, i32* @EXIT_SUCCESS
%46 = call i32 @exit(i32 %45)
unreachable
}

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