#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;
};
typedef int mode_t;

/* Variables and functions */
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IRWXG;
int S_IRWXO;
int S_IRWXU;
int chmod(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int stat(char *, struct stat *);
int main(void) {
  int fd;
  struct stat statbuf;
  mode_t mode;
  if (chmod("test.txt", S_IRUSR | S_IRGRP | S_IROTH) == -1) {
    perror("fail to change model");
    exit(1);
  }
  if (stat("test2.txt", &statbuf) == -1) {
    perror("fail to stat");
    exit(1);
  }
  mode = statbuf.st_mode;
  mode &= (~S_IRWXU & ~S_IRWXG & ~S_IRWXO);
  mode |= (S_IRUSR | S_IRGRP | S_IROTH);
  if (chmod("test2.txt", mode) == -1) {
    perror("fail to change model");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
mov    0x2e69(%rip),%rax
or     (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1040 <chmod@plt>
cmp    $0xffffffff,%eax
jne    1195 <main+0x55>
lea    0xe83(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    0xe82(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1030 <stat@plt>
cmp    $0xffffffff,%eax
jne    11c4 <main+0x84>
lea    0xe73(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
mov    0x2dfb(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    0x2e05(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
and    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    0x2dc0(%rip),%rcx
or     (%rcx),%eax
mov    0x2daf(%rip),%rcx
or     (%rcx),%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xdfb(%rip),%rdi
call   1040 <chmod@plt>
cmp    $0xffffffff,%eax
jne    1247 <main+0x107>
lea    0xdd1(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e68(%rip),%r14
mov    0x2e71(%rip),%rbx
mov    (%rbx),%esi
or     (%r14),%esi
mov    0x2e5d(%rip),%r15
or     (%r15),%esi
lea    0xe93(%rip),%rdi
call   1040 <chmod@plt>
cmp    $0xffffffff,%eax
je     11d5 <main+0x95>
lea    0xea0(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1030 <stat@plt>
cmp    $0xffffffff,%eax
je     11de <main+0x9e>
mov    0x2e4c(%rip),%rax
mov    0x2e3d(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e4a(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    0x8(%rsp),%ecx
mov    (%rbx),%esi
or     (%r14),%esi
or     %ecx,%esi
or     (%r15),%esi
lea    0xe5f(%rip),%rdi
call   1040 <chmod@plt>
cmp    $0xffffffff,%eax
je     11d5 <main+0x95>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe2d(%rip),%rdi
jmp    11e5 <main+0xa5>
lea    0xe43(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [9 x i8] c"test.txt\00"
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [21 x i8] c"fail to change model\00"
@.str.2 = [10 x i8] c"test2.txt\00"
@.str.3 = [13 x i8] c"fail to stat\00"
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.stat
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @S_IRUSR
%6 = load i32, i32* @S_IRGRP
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IROTH
%9 = or i32 %7, %8
%10 = call i32 @chmod(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = call i32 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), %struct.stat* %3)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %15
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%23 = load i32, i32* %22
store i32 %23, i32* %4
%24 = load i32, i32* @S_IRWXU
%25 = xor i32 %24, -1
%26 = load i32, i32* @S_IRWXG
%27 = xor i32 %26, -1
%28 = and i32 %25, %27
%29 = load i32, i32* @S_IRWXO
%30 = xor i32 %29, -1
%31 = and i32 %28, %30
%32 = load i32, i32* %4
%33 = and i32 %32, %31
store i32 %33, i32* %4
%34 = load i32, i32* @S_IRUSR
%35 = load i32, i32* @S_IRGRP
%36 = or i32 %34, %35
%37 = load i32, i32* @S_IROTH
%38 = or i32 %36, %37
%39 = load i32, i32* %4
%40 = or i32 %39, %38
store i32 %40, i32* %4
%41 = load i32, i32* %4
%42 = call i32 @chmod(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %21
%45 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %21
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"test.txt\00"
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [21 x i8] c"fail to change model\00"
@.str.2 = [10 x i8] c"test2.txt\00"
@.str.3 = [13 x i8] c"fail to stat\00"
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
define i32 @main() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = load i32, i32* @S_IRUSR
%4 = load i32, i32* @S_IRGRP
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IROTH
%7 = or i32 %5, %6
%8 = tail call i32 @chmod(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = call i32 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), %struct.stat* %1)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%21 = load i32, i32* %20
%22 = load i32, i32* @S_IRWXU
%23 = load i32, i32* @S_IRWXG
%24 = or i32 %23, %22
%25 = load i32, i32* @S_IRWXO
%26 = or i32 %24, %25
%27 = xor i32 %26, -1
%28 = and i32 %21, %27
%29 = load i32, i32* @S_IRUSR
%30 = load i32, i32* @S_IRGRP
%31 = or i32 %30, %29
%32 = load i32, i32* @S_IROTH
%33 = or i32 %31, %28
%34 = or i32 %33, %32
%35 = tail call i32 @chmod(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %19
%38 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%39 = tail call i32 @exit(i32 1)
unreachable
40: ; preds = %19
ret i32 0
}