#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;
};
typedef int mode_t;

/* Variables and functions */
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
scalar_t__ lstat(char const *, struct stat *);
mode_t get_file_perms(const char *source) {
  struct stat st;
  mode_t default_perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  if (!source)
    return default_perms;
  if (lstat(source, &st) < 0)
    return default_perms;
  return st.st_mode;
}


x86 assembly (clang, -O0):
<get_file_perms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%eax
mov    0x2ebc(%rip),%rcx
or     (%rcx),%eax
mov    0x2ea3(%rip),%rcx
or     (%rcx),%eax
mov    0x2e92(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1159 <get_file_perms+0x49>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1181 <get_file_perms+0x71>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <lstat@plt>
cmp    $0x0,%rax
jge    117b <get_file_perms+0x6b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1181 <get_file_perms+0x71>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_perms>:
push   %rbx
sub    $0x10,%rsp
mov    0x2eac(%rip),%rax
mov    0x2ec5(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x2eaa(%rip),%rax
or     (%rax),%ebx
mov    0x2e99(%rip),%rax
or     (%rax),%ebx
test   %rdi,%rdi
je     1151 <get_file_perms+0x41>
lea    0x8(%rsp),%rsi
call   1030 <lstat@plt>
test   %rax,%rax
js     1151 <get_file_perms+0x41>
mov    0x8(%rsp),%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
define i32 @get_file_perms(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRGRP
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IROTH
%12 = or i32 %10, %11
store i32 %12, i32* %5
%13 = load i8*, i8** %3
%14 = icmp ne i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %1
%16 = load i32, i32* %5
store i32 %16, i32* %2
br label %26
17: ; preds = %1
%18 = load i8*, i8** %3
%19 = call i64 @lstat(i8* %18, %struct.stat* %4)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = load i32, i32* %5
store i32 %22, i32* %2
br label %26
23: ; preds = %17
%24 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%25 = load i32, i32* %24
store i32 %25, i32* %2
br label %26
26: ; preds = %23, %21, %15
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
define i32 @get_file_perms(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @S_IRUSR
%5 = load i32, i32* @S_IWUSR
%6 = or i32 %5, %4
%7 = load i32, i32* @S_IRGRP
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IROTH
%10 = or i32 %8, %9
%11 = icmp eq i8* %0, null
br i1 %11, label %18, label %12
12: ; preds = %1
%13 = call i64 @lstat(i8* %0, %struct.stat* %2)
%14 = icmp slt i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%17 = load i32, i32* %16
br label %18
18: ; preds = %12, %1, %15
%19 = phi i32 [ %17, %15 ], [ %10, %1 ], [ %10, %12 ]
ret i32 %19
}