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

/* Variables and functions */
scalar_t__ bake_mode(char *, int /*<<< orphan*/ *);
scalar_t__ chmod(char *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc < 3) {
    fprintf(stderr, "chmod: not enough arguments\n");
    return 1;
  }
  int i, err = 0;
  for (i = 2; i < argc; i++) {
    char *modestr = argv[1];
    char *path = argv[i];
    mode_t mode = 0;
    struct stat st;
    if (stat(path, &st) < 0) {
      perror("chmod");
      err = 1;
      continue;
    }
    mode = st.st_mode;
    if (bake_mode(modestr, &mode) < 0) {
      fprintf(stderr, "chmod: invalid mode '%s'\n", modestr);
      return 1;
    }
    if (chmod(path, mode) < 0) {
      perror("chmod");
      err = 1;
      continue;
    }
  }
  return err;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1193 <main+0x43>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    128a <main+0x13a>
movl   $0x0,-0x18(%rbp)
movl   $0x2,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1284 <main+0x134>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    11ff <main+0xaf>
lea    0xe2f(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x18(%rbp)
jmp    1276 <main+0x126>
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
call   1070 <bake_mode@plt>
cmp    $0x0,%rax
jge    1243 <main+0xf3>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdf3(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    128a <main+0x13a>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1050 <chmod@plt>
cmp    $0x0,%rax
jge    1271 <main+0x121>
lea    0xdbd(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x18(%rbp)
jmp    1276 <main+0x126>
jmp    1276 <main+0x126>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11a1 <main+0x51>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x3,%edi
jge    117f <main+0x2f>
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
lea    0xe8d(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    1220 <main+0xd0>
mov    %rsi,%rbx
mov    %edi,%r14d
movl   $0x0,0x8(%rsp)
mov    $0x2,%r12d
lea    0x10(%rsp),%r13
jmp    11b9 <main+0x69>
lea    0xe7c(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,0x8(%rsp)
xchg   %ax,%ax
add    $0x1,%r12
cmp    %r12,%r14
je     1228 <main+0xd8>
mov    0x8(%rbx),%rbp
mov    (%rbx,%r12,8),%r15
movl   $0x0,0xc(%rsp)
mov    %r15,%rdi
mov    %r13,%rsi
call   1040 <stat@plt>
test   %rax,%rax
js     119a <main+0x4a>
mov    0x10(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    %rbp,%rdi
lea    0xc(%rsp),%rsi
call   1070 <bake_mode@plt>
test   %rax,%rax
js     1206 <main+0xb6>
mov    0xc(%rsp),%esi
mov    %r15,%rdi
call   1050 <chmod@plt>
test   %rax,%rax
jns    11b0 <main+0x60>
jmp    119a <main+0x4a>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
movl   $0x1,0x8(%rsp)
mov    0x8(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [29 x i8] c"chmod: not enough arguments\0A\00"
@.str.1 = [6 x i8] c"chmod\00"
@.str.2 = [26 x i8] c"chmod: invalid mode '%s'\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %59
17: ; preds = %2
store i32 0, i32* %7
store i32 2, i32* %6
br label %18
18: ; preds = %54, %17
%19 = load i32, i32* %6
%20 = load i32, i32* %4
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %57
22: ; preds = %18
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
store i8* %25, i8** %8
%26 = load i8**, i8*** %5
%27 = load i32, i32* %6
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8*, i8** %26, i64 %28
%30 = load i8*, i8** %29
store i8* %30, i8** %9
store i32 0, i32* %10
%31 = load i8*, i8** %9
%32 = call i64 @stat(i8* %31, %struct.stat* %11)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %22
%35 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %7
br label %54
36: ; preds = %22
%37 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%38 = load i32, i32* %37
store i32 %38, i32* %10
%39 = load i8*, i8** %8
%40 = call i64 @bake_mode(i8* %39, i32* %10)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %36
%43 = load i32, i32* @stderr
%44 = load i8*, i8** %8
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %44)
store i32 1, i32* %3
br label %59
46: ; preds = %36
%47 = load i8*, i8** %9
%48 = load i32, i32* %10
%49 = call i64 @chmod(i8* %47, i32 %48)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %46
%52 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %7
br label %54
53: ; preds = %46
br label %54
54: ; preds = %53, %51, %34
%55 = load i32, i32* %6
%56 = add nsw i32 %55, 1
store i32 %56, i32* %6
br label %18
57: ; preds = %18
%58 = load i32, i32* %7
store i32 %58, i32* %3
br label %59
59: ; preds = %57, %42, %14
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [29 x i8] c"chmod: not enough arguments\0A\00"
@.str.1 = [6 x i8] c"chmod\00"
@.str.2 = [26 x i8] c"chmod: invalid mode '%s'\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.stat
%5 = icmp slt i32 %0, 3
br i1 %5, label %12, label %6
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = bitcast i32* %3 to i8*
%9 = bitcast %struct.stat* %4 to i8*
%10 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%11 = zext i32 %0 to i64
br label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %40
15: ; preds = %6, %36
%16 = phi i64 [ 2, %6 ], [ %38, %36 ]
%17 = phi i32 [ 0, %6 ], [ %37, %36 ]
%18 = load i8*, i8** %7
%19 = getelementptr inbounds i8*, i8** %1, i64 %16
%20 = load i8*, i8** %19
store i32 0, i32* %3
%21 = call i64 @stat(i8* %20, %struct.stat* %4)
%22 = icmp slt i64 %21, 0
br i1 %22, label %34, label %23
23: ; preds = %15
%24 = load i32, i32* %10
store i32 %24, i32* %3
%25 = call i64 @bake_mode(i8* %18, i32* %3)
%26 = icmp slt i64 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %23
%28 = load i32, i32* %3
%29 = call i64 @chmod(i8* %20, i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %34, label %36
31: ; preds = %23
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %18)
br label %40
34: ; preds = %27, %15
%35 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %27
%37 = phi i32 [ %17, %27 ], [ 1, %34 ]
%38 = add nuw nsw i64 %16, 1
%39 = icmp eq i64 %38, %11
br i1 %39, label %40, label %15
40: ; preds = %36, %31, %12
%41 = phi i32 [ 1, %12 ], [ 1, %31 ], [ %37, %36 ]
ret i32 %41
}