#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 {
  scalar_t__ st_mode;
};
typedef scalar_t__ int32_t;

/* Variables and functions */
int /*<<< orphan*/ chmod(char *, scalar_t__);
int /*<<< orphan*/ errno;
scalar_t__ parse_mode(char *, scalar_t__);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ stat(char *, struct stat *);
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int32_t new_mode = 0, result, old_mode;
  struct stat stat_buf;
  if (argc < 3) {
    printf("\nUsage: chmod mode filename\n\n");
    return -1;
  }
  result = stat(argv[2], &stat_buf);
  if (result < 0) {
    printf("Error stating %s: %s\n", argv[2], strerror(errno));
  }
  old_mode = stat_buf.st_mode;
  new_mode = parse_mode(argv[1], old_mode);
  chmod(argv[2], new_mode);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1192 <main+0x42>
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <main+0xc6>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <stat@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11e1 <main+0x91>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <parse_mode@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <chmod@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jg     117c <main+0x1c>
lea    0xea6(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    11d1 <main+0x71>
mov    %rsi,%rbx
mov    0x10(%rsi),%rdi
mov    %rsp,%rsi
call   1050 <stat@plt>
test   %rax,%rax
jns    11b6 <main+0x56>
mov    0x10(%rbx),%r14
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe57(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rsp),%rsi
mov    0x8(%rbx),%rdi
call   1070 <parse_mode@plt>
mov    0x10(%rbx),%rdi
mov    %rax,%rsi
call   1060 <chmod@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [30 x i8] c"\0AUsage: chmod mode filename\0A\0A\00"
@.str.1 = [22 x i8] c"Error stating %s: %s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %6
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %41
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 2
%17 = load i8*, i8** %16
%18 = call i64 @stat(i8* %17, %struct.stat* %9)
store i64 %18, i64* %7
%19 = load i64, i64* %7
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %14
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8* %26)
br label %28
28: ; preds = %21, %14
%29 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%30 = load i64, i64* %29
store i64 %30, i64* %8
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = load i64, i64* %8
%35 = call i64 @parse_mode(i8* %33, i64 %34)
store i64 %35, i64* %6
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 2
%38 = load i8*, i8** %37
%39 = load i64, i64* %6
%40 = call i32 @chmod(i8* %38, i64 %39)
store i32 0, i32* %3
br label %41
41: ; preds = %28, %12
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str.1 = [22 x i8] c"Error stating %s: %s\0A\00"
@errno = global i32 0
@str = [29 x i8] c"\0AUsage: chmod mode filename\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %26
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 2
%10 = load i8*, i8** %9
%11 = call i64 @stat(i8* %10, %struct.stat* %3)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %8
%14 = load i8*, i8** %9
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %16)
br label %18
18: ; preds = %13, %8
%19 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%20 = load i64, i64* %19
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = call i64 @parse_mode(i8* %22, i64 %20)
%24 = load i8*, i8** %9
%25 = call i32 @chmod(i8* %24, i64 %23)
br label %26
26: ; preds = %18, %6
%27 = phi i32 [ -1, %6 ], [ 0, %18 ]
ret i32 %27
}