#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 */

/* Variables and functions */
scalar_t__ chmod(char *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int chmod_main(int argc, char **argv) {
  int i;
  if (argc < 3) {
    fprintf(stderr, "Usage: chmod <MODE> <FILE>\n");
    return 10;
  }
  int mode = 0;
  const char *s = argv[1];
  while (*s) {
    if (*s >= '0' && *s <= '7') {
      mode = (mode << 3) | (*s - '0');
    } else {
      fprintf(stderr, "Bad mode\n");
      return 10;
    }
    s++;
  }
  for (i = 2; i < argc; i++) {
    if (chmod(argv[i], mode) < 0) {
      fprintf(stderr, "Unable to chmod %s: %s\n", argv[i], strerror(errno));
      return 10;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<chmod_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    116c <chmod_main+0x3c>
mov    0x2e90(%rip),%rax
mov    (%rax),%edi
lea    0xea7(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xa,-0x4(%rbp)
jmp    1293 <chmod_main+0x163>
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     11fa <chmod_main+0xca>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jl     11c6 <chmod_main+0x96>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x37,%eax
jg     11c6 <chmod_main+0x96>
mov    -0x18(%rbp),%eax
shl    $0x3,%eax
mov    -0x20(%rbp),%rcx
movsbl (%rcx),%ecx
sub    $0x30,%ecx
or     %ecx,%eax
mov    %eax,-0x18(%rbp)
jmp    11e9 <chmod_main+0xb9>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xa,-0x4(%rbp)
jmp    1293 <chmod_main+0x163>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    117f <chmod_main+0x4f>
movl   $0x2,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    128c <chmod_main+0x15c>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x18(%rbp),%esi
call   1040 <chmod@plt>
cmp    $0x0,%rax
jge    1279 <chmod_main+0x149>
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x28(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xdc0(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xa,-0x4(%rbp)
jmp    1293 <chmod_main+0x163>
jmp    127e <chmod_main+0x14e>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1201 <chmod_main+0xd1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chmod_main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jg     1151 <chmod_main+0x21>
mov    0x2e9d(%rip),%rax
mov    (%rax),%edi
lea    0xeb4(%rip),%rsi
jmp    11dd <chmod_main+0xad>
mov    %rsi,%r14
mov    0x8(%rsi),%rax
mov    (%rax),%cl
test   %cl,%cl
je     119b <chmod_main+0x6b>
add    $0x1,%rax
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ecx,%edx
and    $0xf8,%dl
cmp    $0x30,%dl
jne    11cd <chmod_main+0x9d>
mov    %ebp,%edx
movzbl %cl,%ebp
shl    $0x3,%edx
add    $0xffffffd0,%ebp
or     %edx,%ebp
movzbl (%rax),%ecx
add    $0x1,%rax
test   %cl,%cl
jne    1170 <chmod_main+0x40>
xor    %eax,%eax
cmp    $0x3,%edi
jge    119d <chmod_main+0x6d>
jmp    11e9 <chmod_main+0xb9>
xor    %ebp,%ebp
mov    %edi,%r15d
mov    $0x2,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r14,%rbx,8),%rdi
mov    %ebp,%esi
call   1040 <chmod@plt>
test   %rax,%rax
js     11f4 <chmod_main+0xc4>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    11b0 <chmod_main+0x80>
xor    %eax,%eax
jmp    11e9 <chmod_main+0xb9>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0xa,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%eax
mov    0x2de3(%rip),%rcx
mov    (%rcx),%ebp
mov    (%r14,%rax,8),%rbx
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe0e(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    11e4 <chmod_main+0xb4>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"Usage: chmod <MODE> <FILE>\0A\00"
@.str.1 = [10 x i8] c"Bad mode\0A\00"
@.str.2 = [24 x i8] c"Unable to chmod %s: %s\0A\00"
@errno = global i32 0
define i32 @chmod_main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 3
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 10, i32* %3
br label %75
14: ; preds = %2
store i32 0, i32* %7
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %8
br label %18
18: ; preds = %43, %14
%19 = load i8*, i8** %8
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 0
br i1 %21, label %22, label %46
22: ; preds = %18
%23 = load i8*, i8** %8
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp sge i32 %25, 48
br i1 %26, label %27, label %40
27: ; preds = %22
%28 = load i8*, i8** %8
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp sle i32 %30, 55
br i1 %31, label %32, label %40
32: ; preds = %27
%33 = load i32, i32* %7
%34 = shl i32 %33, 3
%35 = load i8*, i8** %8
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = sub nsw i32 %37, 48
%39 = or i32 %34, %38
store i32 %39, i32* %7
br label %43
40: ; preds = %27, %22
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 10, i32* %3
br label %75
43: ; preds = %32
%44 = load i8*, i8** %8
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %8
br label %18
46: ; preds = %18
store i32 2, i32* %6
br label %47
47: ; preds = %71, %46
%48 = load i32, i32* %6
%49 = load i32, i32* %4
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %74
51: ; preds = %47
%52 = load i8**, i8*** %5
%53 = load i32, i32* %6
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8*, i8** %52, i64 %54
%56 = load i8*, i8** %55
%57 = load i32, i32* %7
%58 = call i64 @chmod(i8* %56, i32 %57)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %70
60: ; preds = %51
%61 = load i32, i32* @stderr
%62 = load i8**, i8*** %5
%63 = load i32, i32* %6
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8*, i8** %62, i64 %64
%66 = load i8*, i8** %65
%67 = load i32, i32* @errno
%68 = call i8* @strerror(i32 %67)
%69 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %66, i8* %68)
store i32 10, i32* %3
br label %75
70: ; preds = %51
br label %71
71: ; preds = %70
%72 = load i32, i32* %6
%73 = add nsw i32 %72, 1
store i32 %73, i32* %6
br label %47
74: ; preds = %47
store i32 0, i32* %3
br label %75
75: ; preds = %74, %60, %40, %11
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"Usage: chmod <MODE> <FILE>\0A\00"
@.str.1 = [10 x i8] c"Bad mode\0A\00"
@.str.2 = [24 x i8] c"Unable to chmod %s: %s\0A\00"
@errno = global i32 0
define i32 @chmod_main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %51
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 0
br i1 %11, label %14, label %17
12: ; preds = %23
%13 = icmp sgt i32 %0, 2
br i1 %13, label %14, label %51
14: ; preds = %7, %12
%15 = phi i32 [ %27, %12 ], [ 0, %7 ]
%16 = zext i32 %0 to i64
br label %34
17: ; preds = %7, %23
%18 = phi i8 [ %29, %23 ], [ %10, %7 ]
%19 = phi i8* [ %28, %23 ], [ %9, %7 ]
%20 = phi i32 [ %27, %23 ], [ 0, %7 ]
%21 = and i8 %18, -8
%22 = icmp eq i8 %21, 48
br i1 %22, label %23, label %31
23: ; preds = %17
%24 = zext i8 %18 to i32
%25 = shl i32 %20, 3
%26 = add nsw i32 %24, -48
%27 = or i32 %26, %25
%28 = getelementptr inbounds i8, i8* %19, i64 1
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 0
br i1 %30, label %12, label %17
31: ; preds = %17
%32 = load i32, i32* @stderr
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %51
34: ; preds = %14, %48
%35 = phi i64 [ 2, %14 ], [ %49, %48 ]
%36 = getelementptr inbounds i8*, i8** %1, i64 %35
%37 = load i8*, i8** %36
%38 = tail call i64 @chmod(i8* %37, i32 %15)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %48
40: ; preds = %34
%41 = and i64 %35, 4294967295
%42 = getelementptr inbounds i8*, i8** %1, i64 %41
%43 = load i32, i32* @stderr
%44 = load i8*, i8** %42
%45 = load i32, i32* @errno
%46 = tail call i8* @strerror(i32 %45)
%47 = tail call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %44, i8* %46)
br label %51
48: ; preds = %34
%49 = add nuw nsw i64 %35, 1
%50 = icmp eq i64 %49, %16
br i1 %50, label %51, label %34
51: ; preds = %48, %12, %31, %40, %4
%52 = phi i32 [ 10, %4 ], [ 10, %31 ], [ 10, %40 ], [ 0, %12 ], [ 0, %48 ]
ret i32 %52
}