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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int S_IXGRP;
int S_IXOTH;
int S_IXUSR;
int chmod(char const *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usage();
void do_chmod(const char *filename, mode_t mode) {
  int ret;
  if (mode > 0777) {
    fprintf(stderr, "chmod: invalid mode %o\n", mode);
    usage();
  }
  mode = ((mode & 0001) ? S_IXOTH : 0) | ((mode & 0002) ? S_IWOTH : 0) |
         ((mode & 0004) ? S_IROTH : 0) | ((mode & 0010) ? S_IXGRP : 0) |
         ((mode & 0020) ? S_IWGRP : 0) | ((mode & 0040) ? S_IRGRP : 0) |
         ((mode & 0100) ? S_IXUSR : 0) | ((mode & 0200) ? S_IWUSR : 0) |
         ((mode & 0400) ? S_IRUSR : 0);
  ret = chmod(filename, mode);
  if (ret == -1) {
    fprintf(stderr, "chmod: cannot chmod\n");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<do_chmod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x1ff,-0xc(%rbp)
jle    117d <do_chmod+0x3d>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe91(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x0,%al
call   1040 <usage@plt>
mov    -0xc(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     119d <do_chmod+0x5d>
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    11a7 <do_chmod+0x67>
xor    %eax,%eax
mov    %eax,-0x14(%rbp)
jmp    11a7 <do_chmod+0x67>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     11cd <do_chmod+0x8d>
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    11d7 <do_chmod+0x97>
xor    %eax,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d7 <do_chmod+0x97>
mov    -0x18(%rbp),%eax
mov    -0x1c(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     1202 <do_chmod+0xc2>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    120c <do_chmod+0xcc>
xor    %eax,%eax
mov    %eax,-0x24(%rbp)
jmp    120c <do_chmod+0xcc>
mov    -0x20(%rbp),%eax
mov    -0x24(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
je     1237 <do_chmod+0xf7>
mov    0x2d9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1241 <do_chmod+0x101>
xor    %eax,%eax
mov    %eax,-0x2c(%rbp)
jmp    1241 <do_chmod+0x101>
mov    -0x28(%rbp),%eax
mov    -0x2c(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
and    $0x10,%eax
cmp    $0x0,%eax
je     126c <do_chmod+0x12c>
mov    0x2d76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    1276 <do_chmod+0x136>
xor    %eax,%eax
mov    %eax,-0x34(%rbp)
jmp    1276 <do_chmod+0x136>
mov    -0x30(%rbp),%eax
mov    -0x34(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x38(%rbp)
mov    -0xc(%rbp),%eax
and    $0x20,%eax
cmp    $0x0,%eax
je     12a1 <do_chmod+0x161>
mov    0x2d19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
jmp    12ab <do_chmod+0x16b>
xor    %eax,%eax
mov    %eax,-0x3c(%rbp)
jmp    12ab <do_chmod+0x16b>
mov    -0x38(%rbp),%eax
mov    -0x3c(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x40(%rbp)
mov    -0xc(%rbp),%eax
and    $0x40,%eax
cmp    $0x0,%eax
je     12d6 <do_chmod+0x196>
mov    0x2d14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
jmp    12e0 <do_chmod+0x1a0>
xor    %eax,%eax
mov    %eax,-0x44(%rbp)
jmp    12e0 <do_chmod+0x1a0>
mov    -0x40(%rbp),%eax
mov    -0x44(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x48(%rbp)
mov    -0xc(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
je     130d <do_chmod+0x1cd>
mov    0x2ccd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
jmp    1317 <do_chmod+0x1d7>
xor    %eax,%eax
mov    %eax,-0x4c(%rbp)
jmp    1317 <do_chmod+0x1d7>
mov    -0x48(%rbp),%eax
mov    -0x4c(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x50(%rbp)
mov    -0xc(%rbp),%eax
and    $0x100,%eax
cmp    $0x0,%eax
je     1344 <do_chmod+0x204>
mov    0x2c5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
jmp    134e <do_chmod+0x20e>
xor    %eax,%eax
mov    %eax,-0x54(%rbp)
jmp    134e <do_chmod+0x20e>
mov    -0x50(%rbp),%eax
mov    -0x54(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <chmod@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1397 <do_chmod+0x257>
mov    0x2c3f(%rip),%rax
mov    (%rax),%edi
lea    0xc96(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2c10(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_chmod>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
cmp    $0x200,%esi
jl     1171 <do_chmod+0x31>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
lea    0xebf(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
xor    %eax,%eax
call   1040 <usage@plt>
movd   %ebp,%xmm0
pshufd $0x0,%xmm0,%xmm0
movdqa 0xe7e(%rip),%xmm1
pand   %xmm0,%xmm1
pand   0xe82(%rip),%xmm0
pxor   %xmm2,%xmm2
pcmpeqd %xmm2,%xmm0
pcmpeqd %xmm2,%xmm1
packssdw %xmm0,%xmm1
packsswb %xmm1,%xmm1
pmovmskb %xmm1,%ecx
xor    %r8d,%r8d
mov    $0x0,%esi
test   $0x80,%cl
jne    11bc <do_chmod+0x7c>
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%edx
test   $0x40,%cl
jne    11cf <do_chmod+0x8f>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
and    $0x1,%ebp
mov    $0x0,%ebx
test   $0x20,%cl
jne    11e5 <do_chmod+0xa5>
mov    0x2de5(%rip),%rax
mov    (%rax),%ebx
neg    %ebp
mov    0x2dfa(%rip),%rax
mov    $0x0,%edi
test   $0x10,%cl
jne    1201 <do_chmod+0xc1>
mov    0x2dd9(%rip),%rdi
mov    (%rdi),%edi
and    (%rax),%ebp
mov    $0x0,%eax
test   $0x8,%cl
jne    1216 <do_chmod+0xd6>
mov    0x2d9c(%rip),%rax
mov    (%rax),%eax
or     %eax,%edi
or     %ebp,%esi
or     %ebx,%edx
mov    $0x0,%eax
test   $0x4,%cl
jne    122f <do_chmod+0xef>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
or     %eax,%edi
or     %edx,%esi
mov    $0x0,%edx
test   $0x2,%cl
jne    1246 <do_chmod+0x106>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edx
or     %edi,%esi
test   $0x1,%cl
jne    1257 <do_chmod+0x117>
mov    0x2d44(%rip),%rax
mov    (%rax),%r8d
or     %r8d,%edx
or     %edx,%esi
mov    %r14,%rdi
call   1050 <chmod@plt>
cmp    $0xffffffff,%eax
je     126e <do_chmod+0x12e>
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"chmod: invalid mode %o\0A\00"
@S_IXOTH = global i32 0
@S_IWOTH = global i32 0
@S_IROTH = global i32 0
@S_IXGRP = global i32 0
@S_IWGRP = global i32 0
@S_IRGRP = global i32 0
@S_IXUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@.str.1 = [21 x i8] c"chmod: cannot chmod\0A\00"
@EXIT_FAILURE = global i32 0
define void @do_chmod(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i32, i32* %4
%7 = icmp sgt i32 %6, 511
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i32, i32* %4
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = call i32 (...) @usage()
br label %13
13: ; preds = %8, %2
%14 = load i32, i32* %4
%15 = and i32 %14, 1
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = load i32, i32* @S_IXOTH
br label %20
19: ; preds = %13
br label %20
20: ; preds = %19, %17
%21 = phi i32 [ %18, %17 ], [ 0, %19 ]
%22 = load i32, i32* %4
%23 = and i32 %22, 2
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = load i32, i32* @S_IWOTH
br label %28
27: ; preds = %20
br label %28
28: ; preds = %27, %25
%29 = phi i32 [ %26, %25 ], [ 0, %27 ]
%30 = or i32 %21, %29
%31 = load i32, i32* %4
%32 = and i32 %31, 4
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = load i32, i32* @S_IROTH
br label %37
36: ; preds = %28
br label %37
37: ; preds = %36, %34
%38 = phi i32 [ %35, %34 ], [ 0, %36 ]
%39 = or i32 %30, %38
%40 = load i32, i32* %4
%41 = and i32 %40, 8
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = load i32, i32* @S_IXGRP
br label %46
45: ; preds = %37
br label %46
46: ; preds = %45, %43
%47 = phi i32 [ %44, %43 ], [ 0, %45 ]
%48 = or i32 %39, %47
%49 = load i32, i32* %4
%50 = and i32 %49, 16
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %46
%53 = load i32, i32* @S_IWGRP
br label %55
54: ; preds = %46
br label %55
55: ; preds = %54, %52
%56 = phi i32 [ %53, %52 ], [ 0, %54 ]
%57 = or i32 %48, %56
%58 = load i32, i32* %4
%59 = and i32 %58, 32
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %55
%62 = load i32, i32* @S_IRGRP
br label %64
63: ; preds = %55
br label %64
64: ; preds = %63, %61
%65 = phi i32 [ %62, %61 ], [ 0, %63 ]
%66 = or i32 %57, %65
%67 = load i32, i32* %4
%68 = and i32 %67, 64
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %64
%71 = load i32, i32* @S_IXUSR
br label %73
72: ; preds = %64
br label %73
73: ; preds = %72, %70
%74 = phi i32 [ %71, %70 ], [ 0, %72 ]
%75 = or i32 %66, %74
%76 = load i32, i32* %4
%77 = and i32 %76, 128
%78 = icmp ne i32 %77, 0
br i1 %78, label %79, label %81
79: ; preds = %73
%80 = load i32, i32* @S_IWUSR
br label %82
81: ; preds = %73
br label %82
82: ; preds = %81, %79
%83 = phi i32 [ %80, %79 ], [ 0, %81 ]
%84 = or i32 %75, %83
%85 = load i32, i32* %4
%86 = and i32 %85, 256
%87 = icmp ne i32 %86, 0
br i1 %87, label %88, label %90
88: ; preds = %82
%89 = load i32, i32* @S_IRUSR
br label %91
90: ; preds = %82
br label %91
91: ; preds = %90, %88
%92 = phi i32 [ %89, %88 ], [ 0, %90 ]
%93 = or i32 %84, %92
store i32 %93, i32* %4
%94 = load i8*, i8** %3
%95 = load i32, i32* %4
%96 = call i32 @chmod(i8* %94, i32 %95)
store i32 %96, i32* %5
%97 = load i32, i32* %5
%98 = icmp eq i32 %97, -1
br i1 %98, label %99, label %104
99: ; preds = %91
%100 = load i32, i32* @stderr
%101 = call i32 (i32, i8*, ...) @fprintf(i32 %100, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%102 = load i32, i32* @EXIT_FAILURE
%103 = call i32 @exit(i32 %102)
unreachable
104: ; preds = %91
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"chmod: invalid mode %o\0A\00"
@S_IXOTH = global i32 0
@S_IWOTH = global i32 0
@S_IROTH = global i32 0
@S_IXGRP = global i32 0
@S_IWGRP = global i32 0
@S_IRGRP = global i32 0
@S_IXUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@.str.1 = [21 x i8] c"chmod: cannot chmod\0A\00"
@EXIT_FAILURE = global i32 0
define void @do_chmod(i8* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 511
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %1)
%7 = tail call i32 (...) @usage()
br label %8
8: ; preds = %4, %2
%9 = and i32 %1, 1
%10 = icmp eq i32 %9, 0
%11 = load i32, i32* @S_IXOTH
%12 = select i1 %10, i32 0, i32 %11
%13 = load i32, i32* @S_IWOTH
%14 = insertelement <8 x i32> poison, i32 %1, i64 0
%15 = shufflevector <8 x i32> %14, <8 x i32> poison, <8 x i32> zeroinitializer
%16 = and <8 x i32> %15, <i32 256, i32 128, i32 64, i32 32, i32 16, i32 8, i32 4, i32 2>
%17 = icmp eq <8 x i32> %16, zeroinitializer
%18 = extractelement <8 x i1> %17, i64 7
%19 = select i1 %18, i32 0, i32 %13
%20 = or i32 %19, %12
%21 = load i32, i32* @S_IROTH
%22 = extractelement <8 x i1> %17, i64 6
%23 = select i1 %22, i32 0, i32 %21
%24 = or i32 %20, %23
%25 = load i32, i32* @S_IXGRP
%26 = extractelement <8 x i1> %17, i64 5
%27 = select i1 %26, i32 0, i32 %25
%28 = or i32 %24, %27
%29 = load i32, i32* @S_IWGRP
%30 = extractelement <8 x i1> %17, i64 4
%31 = select i1 %30, i32 0, i32 %29
%32 = or i32 %28, %31
%33 = load i32, i32* @S_IRGRP
%34 = extractelement <8 x i1> %17, i64 3
%35 = select i1 %34, i32 0, i32 %33
%36 = or i32 %32, %35
%37 = load i32, i32* @S_IXUSR
%38 = extractelement <8 x i1> %17, i64 2
%39 = select i1 %38, i32 0, i32 %37
%40 = or i32 %36, %39
%41 = load i32, i32* @S_IWUSR
%42 = extractelement <8 x i1> %17, i64 1
%43 = select i1 %42, i32 0, i32 %41
%44 = or i32 %40, %43
%45 = load i32, i32* @S_IRUSR
%46 = extractelement <8 x i1> %17, i64 0
%47 = select i1 %46, i32 0, i32 %45
%48 = or i32 %44, %47
%49 = tail call i32 @chmod(i8* %0, i32 %48)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %56
51: ; preds = %8
%52 = load i32, i32* @stderr
%53 = tail call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
%55 = tail call i32 @exit(i32 %54)
unreachable
56: ; preds = %8
ret void
}