#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 ALLPERMS;
int DEFFILEMODE;
int O_CREAT;
int O_RDWR;
int S_IRUSR;
int S_IRWXO;
int S_IRWXU;
int S_ISUID;
int S_IWOTH;
int S_IWUSR;
int S_IXGRP;
int /*<<< orphan*/ assert(int);
scalar_t__ chmod(char *, int);
scalar_t__ lchmod(char *, int);
scalar_t__ lstat(char *, struct stat *);
int open(char *, int, int /*<<< orphan*/);
scalar_t__ stat(char *, struct stat *);
scalar_t__ symlink(char *, char *);
int /*<<< orphan*/ unlink(char *);
int test_vfs_chmod(void) {
  struct stat sb;
  assert(open("/tmp"
              "/file",
              O_RDWR | O_CREAT, 0) == 3);
  assert(stat("/tmp"
              "/file",
              &sb) == 0);
  assert((sb.st_mode & ALLPERMS) == 0);
  assert(chmod("/tmp"
               "/file",
               DEFFILEMODE) == 0);
  assert(stat("/tmp"
              "/file",
              &sb) == 0);
  assert((sb.st_mode & ALLPERMS) == DEFFILEMODE);
  mode_t mode = S_IXGRP | S_IWOTH | S_IRUSR | S_ISUID;
  assert(chmod("/tmp"
               "/file",
               mode) == 0);
  assert(stat("/tmp"
              "/file",
              &sb) == 0);
  assert((sb.st_mode & ALLPERMS) == mode);
  mode_t lmode = S_IWUSR | S_IRWXU | S_IRWXO;
  assert(symlink("/tmp"
                 "/file",
                 "/tmp"
                 "/link") == 0);
  assert(lchmod("/tmp"
                "/link",
                lmode) == 0);
  assert(stat("/tmp"
              "/link",
              &sb) == 0);
  assert((sb.st_mode & ALLPERMS) == mode);
  assert(lstat("/tmp"
               "/link",
               &sb) == 0);
  assert((sb.st_mode & ALLPERMS) == lmode);
  unlink("/tmp"
         "/file");
  unlink("/tmp"
         "/link");
  return 0;
}


x86 assembly (clang, -O0):
<test_vfs_chmod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    0x2e60(%rip),%rax
or     (%rax),%esi
lea    0xe5f(%rip),%rdi
xor    %edx,%edx
call   10a0 <open@plt>
cmp    $0x3,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    0xe41(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%eax
mov    0x2dc5(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
lea    0xdfb(%rip),%rdi
call   1090 <chmod@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    0xdde(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%eax
mov    0x2d62(%rip),%rcx
and    (%rcx),%eax
mov    0x2d39(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%eax
mov    0x2d22(%rip),%rcx
or     (%rcx),%eax
mov    0x2d29(%rip),%rcx
or     (%rcx),%eax
mov    0x2d48(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xd71(%rip),%rdi
call   1090 <chmod@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    0xd54(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%eax
mov    0x2cd8(%rip),%rcx
and    (%rcx),%eax
cmp    -0xc(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    0x2cd7(%rip),%rax
mov    (%rax),%eax
mov    0x2ce6(%rip),%rcx
or     (%rcx),%eax
mov    0x2ced(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x10(%rbp)
lea    0xcf9(%rip),%rdi
lea    0xcfc(%rip),%rsi
call   1050 <symlink@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%esi
lea    0xcdc(%rip),%rdi
call   1040 <lchmod@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    0xcbf(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%eax
mov    0x2c39(%rip),%rcx
and    (%rcx),%eax
cmp    -0xc(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    0xc82(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%eax
mov    0x2bfc(%rip),%rcx
and    (%rcx),%eax
cmp    -0x10(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    0xc3b(%rip),%rdi
call   1030 <unlink@plt>
lea    0xc39(%rip),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_vfs_chmod>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e3e(%rip),%rax
mov    0x2e5f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe5c(%rip),%r15
mov    %r15,%rdi
xor    %edx,%edx
call   10a0 <open@plt>
xor    %edi,%edi
cmp    $0x3,%eax
sete   %dil
call   1070 <assert@plt>
mov    %rsp,%r14
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <stat@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    0x2dc9(%rip),%r13
mov    0x0(%r13),%eax
xor    %edi,%edi
and    (%rsp),%eax
sete   %dil
call   1070 <assert@plt>
mov    0x2d90(%rip),%rbx
mov    (%rbx),%esi
mov    %r15,%rdi
call   1090 <chmod@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <stat@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    0x0(%r13),%eax
and    (%rsp),%eax
xor    %edi,%edi
cmp    (%rbx),%eax
sete   %dil
call   1070 <assert@plt>
mov    0x2d74(%rip),%rax
mov    0x2d45(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
mov    0x2d4a(%rip),%rax
or     (%rax),%ebp
mov    0x2d69(%rip),%rax
or     (%rax),%ebp
mov    %r15,%rdi
mov    %ebp,%esi
call   1090 <chmod@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <stat@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    0x0(%r13),%eax
and    (%rsp),%eax
xor    %edi,%edi
cmp    %ebp,%eax
sete   %dil
call   1070 <assert@plt>
mov    0x2d13(%rip),%rax
mov    0x2d24(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x2d29(%rip),%rax
or     (%rax),%ebx
lea    0xd42(%rip),%r12
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <symlink@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    %r12,%rdi
mov    %ebx,%esi
call   1040 <lchmod@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   1080 <stat@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    0x0(%r13),%eax
and    (%rsp),%eax
xor    %edi,%edi
cmp    %ebp,%eax
sete   %dil
call   1070 <assert@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   1060 <lstat@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    0x0(%r13),%eax
and    (%rsp),%eax
xor    %edi,%edi
cmp    %ebx,%eax
sete   %dil
call   1070 <assert@plt>
mov    %r15,%rdi
call   1030 <unlink@plt>
mov    %r12,%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [10 x i8] c"/tmp/file\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@ALLPERMS = global i32 0
@DEFFILEMODE = global i32 0
@S_IXGRP = global i32 0
@S_IWOTH = global i32 0
@S_IRUSR = global i32 0
@S_ISUID = global i32 0
@S_IWUSR = global i32 0
@S_IRWXU = global i32 0
@S_IRWXO = global i32 0
@.str.1 = [10 x i8] c"/tmp/link\00"
define i32 @test_vfs_chmod() {
%1 = alloca %struct.stat
%2 = alloca i32
%3 = alloca i32
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %4, %5
%7 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6, i32 0)
%8 = icmp eq i32 %7, 3
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = call i64 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%12 = icmp eq i64 %11, 0
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = getelementptr inbounds %struct.stat, %struct.stat* %1, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load i32, i32* @ALLPERMS
%18 = and i32 %16, %17
%19 = icmp eq i32 %18, 0
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i32, i32* @DEFFILEMODE
%23 = call i64 @chmod(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = icmp eq i64 %23, 0
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = call i64 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%28 = icmp eq i64 %27, 0
%29 = zext i1 %28 to i32
%30 = call i32 @assert(i32 %29)
%31 = getelementptr inbounds %struct.stat, %struct.stat* %1, i32 0, i32 0
%32 = load i32, i32* %31
%33 = load i32, i32* @ALLPERMS
%34 = and i32 %32, %33
%35 = load i32, i32* @DEFFILEMODE
%36 = icmp eq i32 %34, %35
%37 = zext i1 %36 to i32
%38 = call i32 @assert(i32 %37)
%39 = load i32, i32* @S_IXGRP
%40 = load i32, i32* @S_IWOTH
%41 = or i32 %39, %40
%42 = load i32, i32* @S_IRUSR
%43 = or i32 %41, %42
%44 = load i32, i32* @S_ISUID
%45 = or i32 %43, %44
store i32 %45, i32* %2
%46 = load i32, i32* %2
%47 = call i64 @chmod(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %46)
%48 = icmp eq i64 %47, 0
%49 = zext i1 %48 to i32
%50 = call i32 @assert(i32 %49)
%51 = call i64 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%52 = icmp eq i64 %51, 0
%53 = zext i1 %52 to i32
%54 = call i32 @assert(i32 %53)
%55 = getelementptr inbounds %struct.stat, %struct.stat* %1, i32 0, i32 0
%56 = load i32, i32* %55
%57 = load i32, i32* @ALLPERMS
%58 = and i32 %56, %57
%59 = load i32, i32* %2
%60 = icmp eq i32 %58, %59
%61 = zext i1 %60 to i32
%62 = call i32 @assert(i32 %61)
%63 = load i32, i32* @S_IWUSR
%64 = load i32, i32* @S_IRWXU
%65 = or i32 %63, %64
%66 = load i32, i32* @S_IRWXO
%67 = or i32 %65, %66
store i32 %67, i32* %3
%68 = call i64 @symlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%69 = icmp eq i64 %68, 0
%70 = zext i1 %69 to i32
%71 = call i32 @assert(i32 %70)
%72 = load i32, i32* %3
%73 = call i64 @lchmod(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %72)
%74 = icmp eq i64 %73, 0
%75 = zext i1 %74 to i32
%76 = call i32 @assert(i32 %75)
%77 = call i64 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %1)
%78 = icmp eq i64 %77, 0
%79 = zext i1 %78 to i32
%80 = call i32 @assert(i32 %79)
%81 = getelementptr inbounds %struct.stat, %struct.stat* %1, i32 0, i32 0
%82 = load i32, i32* %81
%83 = load i32, i32* @ALLPERMS
%84 = and i32 %82, %83
%85 = load i32, i32* %2
%86 = icmp eq i32 %84, %85
%87 = zext i1 %86 to i32
%88 = call i32 @assert(i32 %87)
%89 = call i64 @lstat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %1)
%90 = icmp eq i64 %89, 0
%91 = zext i1 %90 to i32
%92 = call i32 @assert(i32 %91)
%93 = getelementptr inbounds %struct.stat, %struct.stat* %1, i32 0, i32 0
%94 = load i32, i32* %93
%95 = load i32, i32* @ALLPERMS
%96 = and i32 %94, %95
%97 = load i32, i32* %3
%98 = icmp eq i32 %96, %97
%99 = zext i1 %98 to i32
%100 = call i32 @assert(i32 %99)
%101 = call i32 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%102 = call i32 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [10 x i8] c"/tmp/file\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@ALLPERMS = global i32 0
@DEFFILEMODE = global i32 0
@S_IXGRP = global i32 0
@S_IWOTH = global i32 0
@S_IRUSR = global i32 0
@S_ISUID = global i32 0
@S_IWUSR = global i32 0
@S_IRWXU = global i32 0
@S_IRWXO = global i32 0
@.str.1 = [10 x i8] c"/tmp/link\00"
define i32 @test_vfs_chmod() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5, i32 0)
%7 = icmp eq i32 %6, 3
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = call i64 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%11 = icmp eq i64 %10, 0
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @ALLPERMS
%17 = and i32 %16, %15
%18 = icmp eq i32 %17, 0
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load i32, i32* @DEFFILEMODE
%22 = call i64 @chmod(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = icmp eq i64 %22, 0
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = call i64 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%27 = icmp eq i64 %26, 0
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
%30 = load i32, i32* %14
%31 = load i32, i32* @ALLPERMS
%32 = and i32 %31, %30
%33 = load i32, i32* @DEFFILEMODE
%34 = icmp eq i32 %32, %33
%35 = zext i1 %34 to i32
%36 = call i32 @assert(i32 %35)
%37 = load i32, i32* @S_IXGRP
%38 = load i32, i32* @S_IWOTH
%39 = or i32 %38, %37
%40 = load i32, i32* @S_IRUSR
%41 = or i32 %39, %40
%42 = load i32, i32* @S_ISUID
%43 = or i32 %41, %42
%44 = call i64 @chmod(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %43)
%45 = icmp eq i64 %44, 0
%46 = zext i1 %45 to i32
%47 = call i32 @assert(i32 %46)
%48 = call i64 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%49 = icmp eq i64 %48, 0
%50 = zext i1 %49 to i32
%51 = call i32 @assert(i32 %50)
%52 = load i32, i32* %14
%53 = load i32, i32* @ALLPERMS
%54 = and i32 %53, %52
%55 = icmp eq i32 %54, %43
%56 = zext i1 %55 to i32
%57 = call i32 @assert(i32 %56)
%58 = load i32, i32* @S_IWUSR
%59 = load i32, i32* @S_IRWXU
%60 = or i32 %59, %58
%61 = load i32, i32* @S_IRWXO
%62 = or i32 %60, %61
%63 = call i64 @symlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%64 = icmp eq i64 %63, 0
%65 = zext i1 %64 to i32
%66 = call i32 @assert(i32 %65)
%67 = call i64 @lchmod(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %62)
%68 = icmp eq i64 %67, 0
%69 = zext i1 %68 to i32
%70 = call i32 @assert(i32 %69)
%71 = call i64 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %1)
%72 = icmp eq i64 %71, 0
%73 = zext i1 %72 to i32
%74 = call i32 @assert(i32 %73)
%75 = load i32, i32* %14
%76 = load i32, i32* @ALLPERMS
%77 = and i32 %76, %75
%78 = icmp eq i32 %77, %43
%79 = zext i1 %78 to i32
%80 = call i32 @assert(i32 %79)
%81 = call i64 @lstat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %1)
%82 = icmp eq i64 %81, 0
%83 = zext i1 %82 to i32
%84 = call i32 @assert(i32 %83)
%85 = load i32, i32* %14
%86 = load i32, i32* @ALLPERMS
%87 = and i32 %86, %85
%88 = icmp eq i32 %87, %62
%89 = zext i1 %88 to i32
%90 = call i32 @assert(i32 %89)
%91 = call i32 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%92 = call i32 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
ret i32 0
}