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

/* Variables and functions */
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
scalar_t__ S_ISDIR(int);
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int S_IXGRP;
int S_IXOTH;
int S_IXUSR;
scalar_t__ malloc(int);
char *get_file_permissions(struct stat buf) {
  char *permissions = (char *)malloc(11 * sizeof(char));
  permissions[0] = S_ISDIR(buf.st_mode) ? 'd' : '-';
  permissions[1] = buf.st_mode & S_IRUSR ? 'r' : '-';
  permissions[2] = buf.st_mode & S_IWUSR ? 'w' : '-';
  permissions[3] = buf.st_mode & S_IXUSR ? 'x' : '-';
  permissions[4] = buf.st_mode & S_IRGRP ? 'r' : '-';
  permissions[5] = buf.st_mode & S_IWGRP ? 'w' : '-';
  permissions[6] = buf.st_mode & S_IXGRP ? 'x' : '-';
  permissions[7] = buf.st_mode & S_IROTH ? 'r' : '-';
  permissions[8] = buf.st_mode & S_IWOTH ? 'w' : '-';
  permissions[9] = buf.st_mode & S_IXOTH ? 'x' : '-';
  permissions[10] = '\0';
  return permissions;
}


x86 assembly (clang, -O0):
<get_file_permissions>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    $0xb,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <S_ISDIR@plt>
mov    %rax,%rdx
mov    $0x2d,%eax
mov    $0x64,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%edx
mov    0x2e41(%rip),%rax
and    (%rax),%edx
mov    $0x2d,%eax
mov    $0x72,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x8(%rbp),%edx
mov    0x2e44(%rip),%rax
and    (%rax),%edx
mov    $0x2d,%eax
mov    $0x77,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x8(%rbp),%edx
mov    0x2e2f(%rip),%rax
and    (%rax),%edx
mov    $0x2d,%eax
mov    $0x78,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x3(%rax)
mov    -0x8(%rbp),%edx
mov    0x2de2(%rip),%rax
and    (%rax),%edx
mov    $0x2d,%eax
mov    $0x72,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x4(%rax)
mov    -0x8(%rbp),%edx
mov    0x2ddd(%rip),%rax
and    (%rax),%edx
mov    $0x2d,%eax
mov    $0x77,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x5(%rax)
mov    -0x8(%rbp),%edx
mov    0x2da8(%rip),%rax
and    (%rax),%edx
mov    $0x2d,%eax
mov    $0x78,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x6(%rax)
mov    -0x8(%rbp),%edx
mov    0x2d6b(%rip),%rax
and    (%rax),%edx
mov    $0x2d,%eax
mov    $0x72,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x7(%rax)
mov    -0x8(%rbp),%edx
mov    0x2d2e(%rip),%rax
and    (%rax),%edx
mov    $0x2d,%eax
mov    $0x77,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x8(%rax)
mov    -0x8(%rbp),%edx
mov    0x2d59(%rip),%rax
and    (%rax),%edx
mov    $0x2d,%eax
mov    $0x78,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x9(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0xa(%rax)
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_permissions>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    $0xb,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1030 <S_ISDIR@plt>
test   %rax,%rax
mov    $0x2d,%eax
mov    $0x64,%ecx
cmove  %eax,%ecx
mov    %cl,(%rbx)
mov    0x2e56(%rip),%rcx
mov    (%rcx),%ecx
and    %ebp,%ecx
mov    $0x72,%ecx
cmove  %eax,%ecx
mov    %cl,0x1(%rbx)
mov    0x2e68(%rip),%rcx
mov    (%rcx),%ecx
and    %ebp,%ecx
mov    $0x77,%ecx
cmove  %eax,%ecx
mov    %cl,0x2(%rbx)
mov    0x2e62(%rip),%rcx
mov    (%rcx),%ecx
and    %ebp,%ecx
mov    $0x78,%edx
cmove  %eax,%edx
mov    $0x72,%ecx
mov    %dl,0x3(%rbx)
mov    0x2e1f(%rip),%rdx
mov    (%rdx),%edx
and    %ebp,%edx
mov    $0x72,%esi
cmove  %eax,%esi
mov    $0x77,%edx
mov    %sil,0x4(%rbx)
mov    0x2e23(%rip),%rsi
mov    (%rsi),%esi
and    %ebp,%esi
mov    $0x77,%edi
cmove  %eax,%edi
mov    $0x78,%esi
mov    %dil,0x5(%rbx)
mov    0x2df7(%rip),%rdi
mov    (%rdi),%edi
and    %ebp,%edi
mov    $0x78,%edi
cmove  %eax,%edi
mov    %dil,0x6(%rbx)
mov    0x2dc8(%rip),%rdi
mov    (%rdi),%edi
and    %ebp,%edi
cmove  %eax,%ecx
mov    %cl,0x7(%rbx)
mov    0x2d9f(%rip),%rcx
mov    (%rcx),%ecx
and    %ebp,%ecx
cmove  %eax,%edx
mov    %dl,0x8(%rbx)
mov    0x2dde(%rip),%rcx
and    (%rcx),%ebp
cmove  %eax,%esi
mov    %sil,0x9(%rbx)
movb   $0x0,0xa(%rbx)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IXUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IXGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@S_IXOTH = global i32 0
define i8* @get_file_permissions(i32 %0) {
%2 = alloca %struct.stat
%3 = alloca i8*
%4 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
store i32 %0, i32* %4
%5 = call i64 @malloc(i32 11)
%6 = inttoptr i64 %5 to i8*
store i8* %6, i8** %3
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%8 = load i32, i32* %7
%9 = call i64 @S_ISDIR(i32 %8)
%10 = icmp ne i64 %9, 0
%11 = zext i1 %10 to i64
%12 = select i1 %10, i32 100, i32 45
%13 = trunc i32 %12 to i8
%14 = load i8*, i8** %3
%15 = getelementptr inbounds i8, i8* %14, i64 0
store i8 %13, i8* %15
%16 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* @S_IRUSR
%19 = and i32 %17, %18
%20 = icmp ne i32 %19, 0
%21 = zext i1 %20 to i64
%22 = select i1 %20, i32 114, i32 45
%23 = trunc i32 %22 to i8
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i64 1
store i8 %23, i8* %25
%26 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%27 = load i32, i32* %26
%28 = load i32, i32* @S_IWUSR
%29 = and i32 %27, %28
%30 = icmp ne i32 %29, 0
%31 = zext i1 %30 to i64
%32 = select i1 %30, i32 119, i32 45
%33 = trunc i32 %32 to i8
%34 = load i8*, i8** %3
%35 = getelementptr inbounds i8, i8* %34, i64 2
store i8 %33, i8* %35
%36 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load i32, i32* @S_IXUSR
%39 = and i32 %37, %38
%40 = icmp ne i32 %39, 0
%41 = zext i1 %40 to i64
%42 = select i1 %40, i32 120, i32 45
%43 = trunc i32 %42 to i8
%44 = load i8*, i8** %3
%45 = getelementptr inbounds i8, i8* %44, i64 3
store i8 %43, i8* %45
%46 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%47 = load i32, i32* %46
%48 = load i32, i32* @S_IRGRP
%49 = and i32 %47, %48
%50 = icmp ne i32 %49, 0
%51 = zext i1 %50 to i64
%52 = select i1 %50, i32 114, i32 45
%53 = trunc i32 %52 to i8
%54 = load i8*, i8** %3
%55 = getelementptr inbounds i8, i8* %54, i64 4
store i8 %53, i8* %55
%56 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%57 = load i32, i32* %56
%58 = load i32, i32* @S_IWGRP
%59 = and i32 %57, %58
%60 = icmp ne i32 %59, 0
%61 = zext i1 %60 to i64
%62 = select i1 %60, i32 119, i32 45
%63 = trunc i32 %62 to i8
%64 = load i8*, i8** %3
%65 = getelementptr inbounds i8, i8* %64, i64 5
store i8 %63, i8* %65
%66 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%67 = load i32, i32* %66
%68 = load i32, i32* @S_IXGRP
%69 = and i32 %67, %68
%70 = icmp ne i32 %69, 0
%71 = zext i1 %70 to i64
%72 = select i1 %70, i32 120, i32 45
%73 = trunc i32 %72 to i8
%74 = load i8*, i8** %3
%75 = getelementptr inbounds i8, i8* %74, i64 6
store i8 %73, i8* %75
%76 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%77 = load i32, i32* %76
%78 = load i32, i32* @S_IROTH
%79 = and i32 %77, %78
%80 = icmp ne i32 %79, 0
%81 = zext i1 %80 to i64
%82 = select i1 %80, i32 114, i32 45
%83 = trunc i32 %82 to i8
%84 = load i8*, i8** %3
%85 = getelementptr inbounds i8, i8* %84, i64 7
store i8 %83, i8* %85
%86 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%87 = load i32, i32* %86
%88 = load i32, i32* @S_IWOTH
%89 = and i32 %87, %88
%90 = icmp ne i32 %89, 0
%91 = zext i1 %90 to i64
%92 = select i1 %90, i32 119, i32 45
%93 = trunc i32 %92 to i8
%94 = load i8*, i8** %3
%95 = getelementptr inbounds i8, i8* %94, i64 8
store i8 %93, i8* %95
%96 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%97 = load i32, i32* %96
%98 = load i32, i32* @S_IXOTH
%99 = and i32 %97, %98
%100 = icmp ne i32 %99, 0
%101 = zext i1 %100 to i64
%102 = select i1 %100, i32 120, i32 45
%103 = trunc i32 %102 to i8
%104 = load i8*, i8** %3
%105 = getelementptr inbounds i8, i8* %104, i64 9
store i8 %103, i8* %105
%106 = load i8*, i8** %3
%107 = getelementptr inbounds i8, i8* %106, i64 10
store i8 0, i8* %107
%108 = load i8*, i8** %3
ret i8* %108
}

LLVM IR (-O2):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IXUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IXGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@S_IXOTH = global i32 0
define i8* @get_file_permissions(i32 %0) {
%2 = tail call i64 @malloc(i32 11)
%3 = inttoptr i64 %2 to i8*
%4 = tail call i64 @S_ISDIR(i32 %0)
%5 = icmp eq i64 %4, 0
%6 = select i1 %5, i8 45, i8 100
store i8 %6, i8* %3
%7 = load i32, i32* @S_IRUSR
%8 = and i32 %7, %0
%9 = icmp eq i32 %8, 0
%10 = select i1 %9, i8 45, i8 114
%11 = getelementptr inbounds i8, i8* %3, i64 1
store i8 %10, i8* %11
%12 = load i32, i32* @S_IWUSR
%13 = and i32 %12, %0
%14 = icmp eq i32 %13, 0
%15 = select i1 %14, i8 45, i8 119
%16 = getelementptr inbounds i8, i8* %3, i64 2
store i8 %15, i8* %16
%17 = load i32, i32* @S_IXUSR
%18 = and i32 %17, %0
%19 = icmp eq i32 %18, 0
%20 = select i1 %19, i8 45, i8 120
%21 = getelementptr inbounds i8, i8* %3, i64 3
store i8 %20, i8* %21
%22 = load i32, i32* @S_IRGRP
%23 = and i32 %22, %0
%24 = icmp eq i32 %23, 0
%25 = select i1 %24, i8 45, i8 114
%26 = getelementptr inbounds i8, i8* %3, i64 4
store i8 %25, i8* %26
%27 = load i32, i32* @S_IWGRP
%28 = and i32 %27, %0
%29 = icmp eq i32 %28, 0
%30 = select i1 %29, i8 45, i8 119
%31 = getelementptr inbounds i8, i8* %3, i64 5
store i8 %30, i8* %31
%32 = load i32, i32* @S_IXGRP
%33 = and i32 %32, %0
%34 = icmp eq i32 %33, 0
%35 = select i1 %34, i8 45, i8 120
%36 = getelementptr inbounds i8, i8* %3, i64 6
store i8 %35, i8* %36
%37 = load i32, i32* @S_IROTH
%38 = and i32 %37, %0
%39 = icmp eq i32 %38, 0
%40 = select i1 %39, i8 45, i8 114
%41 = getelementptr inbounds i8, i8* %3, i64 7
store i8 %40, i8* %41
%42 = load i32, i32* @S_IWOTH
%43 = and i32 %42, %0
%44 = icmp eq i32 %43, 0
%45 = select i1 %44, i8 45, i8 119
%46 = getelementptr inbounds i8, i8* %3, i64 8
store i8 %45, i8* %46
%47 = load i32, i32* @S_IXOTH
%48 = and i32 %47, %0
%49 = icmp eq i32 %48, 0
%50 = select i1 %49, i8 45, i8 120
%51 = getelementptr inbounds i8, i8* %3, i64 9
store i8 %50, i8* %51
%52 = getelementptr inbounds i8, i8* %3, i64 10
store i8 0, i8* %52
ret i8* %3
}