#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*/ perror(char *);
int stat(char *, struct stat *);
int main(int argc, char **argv) {
  struct stat sbuf;
  if (stat("foo", &sbuf) == -1) {
    perror("can't stat foo");
    exit(EXIT_FAILURE);
  }
  if (chmod("foo", (sbuf.st_mode & ~S_IXGRP) | S_ISUID) == -1) {
    perror("can't chmod foo");
    exit(EXIT_FAILURE);
  }
  if (chmod("foo1", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) {
    perror("can't chmod foo1");
    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    0xebe(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <stat@plt>
cmp    $0xffffffff,%eax
jne    1189 <main+0x49>
lea    0xe8a(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%esi
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%esi
mov    0x2e47(%rip),%rax
or     (%rax),%esi
lea    0xe71(%rip),%rdi
call   1040 <chmod@plt>
cmp    $0xffffffff,%eax
jne    11d2 <main+0x92>
lea    0xe50(%rip),%rdi
call   1050 <perror@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    0x2dfe(%rip),%rax
or     (%rax),%esi
mov    0x2ddd(%rip),%rax
or     (%rax),%esi
mov    0x2dcc(%rip),%rax
or     (%rax),%esi
lea    0xe2e(%rip),%rdi
call   1040 <chmod@plt>
cmp    $0xffffffff,%eax
jne    1225 <main+0xe5>
lea    0xe0d(%rip),%rdi
call   1050 <perror@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xed3(%rip),%rdi
mov    %rsp,%rsi
call   1030 <stat@plt>
cmp    $0xffffffff,%eax
jne    115e <main+0x1e>
lea    0xea4(%rip),%rdi
jmp    11cb <main+0x8b>
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
not    %esi
and    (%rsp),%esi
mov    0x2e75(%rip),%rax
or     (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1040 <chmod@plt>
cmp    $0xffffffff,%eax
jne    118f <main+0x4f>
lea    0xe82(%rip),%rdi
jmp    11cb <main+0x8b>
mov    0x2e1a(%rip),%rax
mov    0x2e43(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e20(%rip),%rax
or     (%rax),%esi
mov    0x2e0f(%rip),%rax
or     (%rax),%esi
lea    0xe71(%rip),%rdi
call   1040 <chmod@plt>
cmp    $0xffffffff,%eax
jne    11de <main+0x9e>
lea    0xe54(%rip),%rdi
call   1050 <perror@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [4 x i8] c"foo\00"
@.str.1 = [15 x i8] c"can't stat foo\00"
@EXIT_FAILURE = global i32 0
@S_IXGRP = global i32 0
@S_ISUID = global i32 0
@.str.2 = [16 x i8] c"can't chmod foo\00"
@.str.3 = [5 x i8] c"foo1\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 foo1\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 i32 @stat(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), %struct.stat* %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
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_ISUID
%20 = or i32 %18, %19
%21 = call i32 @chmod(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %13
%24 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
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 i32 @chmod(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %41
37: ; preds = %27
%38 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %27
%42 = load i32, i32* @EXIT_SUCCESS
%43 = call i32 @exit(i32 %42)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [4 x i8] c"foo\00"
@.str.1 = [15 x i8] c"can't stat foo\00"
@EXIT_FAILURE = global i32 0
@S_IXGRP = global i32 0
@S_ISUID = global i32 0
@.str.2 = [16 x i8] c"can't chmod foo\00"
@.str.3 = [5 x i8] c"foo1\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 foo1\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 i32 @stat(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), %struct.stat* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
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_ISUID
%18 = or i32 %16, %17
%19 = tail call i32 @chmod(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %11
%22 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @exit(i32 %23)
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 = tail call i32 @chmod(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %39
35: ; preds = %25
%36 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = tail call i32 @exit(i32 %37)
unreachable
39: ; preds = %25
%40 = load i32, i32* @EXIT_SUCCESS
%41 = tail call i32 @exit(i32 %40)
unreachable
}