#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 {
  unsigned int st_mode;
};

/* Variables and functions */
unsigned int S_IFMT;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int stat(char *, struct stat *);
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  if (argc < 2) {
    printf("ac_getmode 1.0\n");
    printf("by Simon Strandgaard <simon@opcoders.com>\n\n");
    printf("  usage:\n  ac_getmode <filename>\n\n\n");
    fflush(stdout);
    return -1;
  }
  struct stat st;
  int rc;
  rc = stat(argv[1], &st);
  if (rc == -1) {
    perror("stat");
    return 1;
  }
  unsigned int mode = st.st_mode & S_IFMT;
  printf("%07o\n", mode);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11a4 <main+0x64>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe9b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeb9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1201 <main+0xc1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11da <main+0x9a>
lea    0xe97(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1201 <main+0xc1>
mov    -0x18(%rbp),%eax
mov    0x2dfc(%rip),%rcx
and    (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jg     1193 <main+0x43>
lea    0xeaa(%rip),%rdi
call   1030 <puts@plt>
lea    0xead(%rip),%rdi
call   1030 <puts@plt>
lea    0xecc(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    $0xffffffff,%ebx
jmp    11c3 <main+0x73>
mov    0x8(%rsi),%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
je     11cb <main+0x7b>
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
and    0x8(%rsp),%esi
lea    0xe4b(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe2e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%ebx
jmp    11c3 <main+0x73>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [16 x i8] c"ac_getmode 1.0\0A\00"
@.str.1 = [44 x i8] c"by Simon Strandgaard <simon@opcoders.com>\0A\0A\00"
@.str.2 = [36 x i8] c" usage:\0A ac_getmode <filename>\0A\0A\0A\00"
@stdout = global i32 0
@.str.3 = [5 x i8] c"stat\00"
@S_IFMT = global i32 0
@.str.4 = [6 x i8] c"%07o\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%15 = load i32, i32* @stdout
%16 = call i32 @fflush(i32 %15)
store i32 -1, i32* %3
br label %33
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @stat(i8* %20, %struct.stat* %6)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %33
26: ; preds = %17
%27 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%28 = load i32, i32* %27
%29 = load i32, i32* @S_IFMT
%30 = and i32 %28, %29
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %31)
store i32 0, i32* %3
br label %33
33: ; preds = %26, %24, %11
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stdout = global i32 0
@.str.3 = [5 x i8] c"stat\00"
@S_IFMT = global i32 0
@.str.4 = [6 x i8] c"%07o\0A\00"
@str = [15 x i8] c"ac_getmode 1.0\00"
@str.5 = [43 x i8] c"by Simon Strandgaard <simon@opcoders.com>\0A\00"
@str.6 = [35 x i8] c" usage:\0A ac_getmode <filename>\0A\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %11
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.5, i64 0, i64 0))
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.6, i64 0, i64 0))
%9 = load i32, i32* @stdout
%10 = tail call i32 @fflush(i32 %9)
br label %27
11: ; preds = %2
%12 = bitcast %struct.stat* %3 to i8*
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @stat(i8* %14, %struct.stat* %3)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %25
19: ; preds = %11
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%21 = load i32, i32* %20
%22 = load i32, i32* @S_IFMT
%23 = and i32 %22, %21
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %19, %17
%26 = phi i32 [ 1, %17 ], [ 0, %19 ]
br label %27
27: ; preds = %25, %5
%28 = phi i32 [ -1, %5 ], [ %26, %25 ]
ret i32 %28
}