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

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ S_ISREG(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strerror(scalar_t__);
int strlen(char *);
int filecheck(char *file, char *sws_dir) {
  int len;
  char *pathname;
  struct stat file_stat;
  if (!file)
    return 0;
  if (file[0] == '/')
    pathname = file;
  else {
    len = strlen(sws_dir) + strlen(file) + 1;
    if ((pathname = (char *)malloc((len + 1) * sizeof(char))) == NULL) {
      fprintf(stderr, "Unable to allocate memory: %s\n", strerror(errno));
      exit(1);
    }
    strcpy(pathname, sws_dir);
    strcat(pathname, "/");
    strcat(pathname, file);
    pathname[len] = '\0';
  }
  if (stat(pathname, &file_stat) == -1) {
    if (errno == ENOENT) {
      return 0;
    } else {
      perror("Stat Error");
      return 1;
    }
  }
  if (S_ISREG(file_stat.st_mode))
    return 0;
  else {
    fprintf(stderr, "Warning: Not a regular file: %s\n", file);
    return 0;
  }
}


x86 assembly (clang, -O0):
<filecheck>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c7 <filecheck+0x27>
movl   $0x0,-0x4(%rbp)
jmp    1326 <filecheck+0x186>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11e4 <filecheck+0x44>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1294 <filecheck+0xf4>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    125e <filecheck+0xbe>
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xdb1(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rdi
lea    0xda9(%rip),%rsi
call   10a0 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <strcat@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
jne    12e8 <filecheck+0x148>
mov    0x2d27(%rip),%rax
mov    (%rax),%rax
mov    0x2d0d(%rip),%rcx
cmp    (%rcx),%rax
jne    12d0 <filecheck+0x130>
movl   $0x0,-0x4(%rbp)
jmp    1326 <filecheck+0x186>
lea    0xd4a(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1326 <filecheck+0x186>
mov    -0x30(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%rax
je     1306 <filecheck+0x166>
movl   $0x0,-0x4(%rbp)
jmp    1326 <filecheck+0x186>
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd12(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filecheck>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %rdi,%rdi
je     1271 <filecheck+0xd1>
mov    %rdi,%rbp
cmpb   $0x2f,(%rdi)
mov    %rdi,%rbx
je     1224 <filecheck+0x84>
mov    %rsi,%r15
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %eax,%r13d
lea    (%r12,%r13,1),%edi
add    $0x2,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1297 <filecheck+0xf7>
mov    %rax,%rbx
add    %r12d,%r13d
movslq %r13d,%r12
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
lea    0xe14(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <strcat@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10a0 <strcat@plt>
movb   $0x0,0x1(%r12,%rbx,1)
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     125b <filecheck+0xbb>
mov    (%rsp),%edi
call   1070 <S_ISREG@plt>
test   %rax,%rax
jne    1271 <filecheck+0xd1>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
mov    %rbp,%rdx
call   1050 <fprintf@plt>
jmp    1271 <filecheck+0xd1>
mov    0x2d76(%rip),%rax
mov    (%rax),%rax
mov    0x2d5c(%rip),%rcx
cmp    (%rcx),%rax
jne    1283 <filecheck+0xe3>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd97(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%r14d
jmp    1271 <filecheck+0xd1>
mov    0x2d42(%rip),%rax
mov    (%rax),%ebx
mov    0x2d31(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
lea    0xd4a(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [31 x i8] c"Unable to allocate memory: %s\0A\00"
@errno = global i64 0
@.str.1 = [2 x i8] c"/\00"
@ENOENT = global i64 0
@.str.2 = [11 x i8] c"Stat Error\00"
@.str.3 = [33 x i8] c"Warning: Not a regular file: %s\0A\00"
define i32 @filecheck(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.stat
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %2
store i32 0, i32* %3
br label %75
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 47
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = load i8*, i8** %4
store i8* %19, i8** %7
br label %54
20: ; preds = %12
%21 = load i8*, i8** %5
%22 = call i32 @strlen(i8* %21)
%23 = load i8*, i8** %4
%24 = call i32 @strlen(i8* %23)
%25 = add nsw i32 %22, %24
%26 = add nsw i32 %25, 1
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = mul i64 %29, 1
%31 = trunc i64 %30 to i32
%32 = call i64 @malloc(i32 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %7
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %41
35: ; preds = %20
%36 = load i32, i32* @stderr
%37 = load i64, i64* @errno
%38 = call i8* @strerror(i64 %37)
%39 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %38)
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %20
%42 = load i8*, i8** %7
%43 = load i8*, i8** %5
%44 = call i32 @strcpy(i8* %42, i8* %43)
%45 = load i8*, i8** %7
%46 = call i32 @strcat(i8* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%47 = load i8*, i8** %7
%48 = load i8*, i8** %4
%49 = call i32 @strcat(i8* %47, i8* %48)
%50 = load i8*, i8** %7
%51 = load i32, i32* %6
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8 0, i8* %53
br label %54
54: ; preds = %41, %18
%55 = load i8*, i8** %7
%56 = call i32 @stat(i8* %55, %struct.stat* %8)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %65
58: ; preds = %54
%59 = load i64, i64* @errno
%60 = load i64, i64* @ENOENT
%61 = icmp eq i64 %59, %60
br i1 %61, label %62, label %63
62: ; preds = %58
store i32 0, i32* %3
br label %75
63: ; preds = %58
%64 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %75
65: ; preds = %54
%66 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%67 = load i32, i32* %66
%68 = call i64 @S_ISREG(i32 %67)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %65
store i32 0, i32* %3
br label %75
71: ; preds = %65
%72 = load i32, i32* @stderr
%73 = load i8*, i8** %4
%74 = call i32 @fprintf(i32 %72, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %73)
store i32 0, i32* %3
br label %75
75: ; preds = %71, %70, %63, %62, %11
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [31 x i8] c"Unable to allocate memory: %s\0A\00"
@errno = global i64 0
@.str.1 = [2 x i8] c"/\00"
@ENOENT = global i64 0
@.str.2 = [11 x i8] c"Stat Error\00"
@.str.3 = [33 x i8] c"Warning: Not a regular file: %s\0A\00"
define i32 @filecheck(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i8* %0, null
br i1 %5, label %48, label %6
6: ; preds = %2
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 47
br i1 %8, label %30, label %9
9: ; preds = %6
%10 = tail call i32 @strlen(i8* %1)
%11 = tail call i32 @strlen(i8* %0)
%12 = add nsw i32 %11, %10
%13 = add nsw i32 %12, 2
%14 = tail call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %23
17: ; preds = %9
%18 = load i32, i32* @stderr
%19 = load i64, i64* @errno
%20 = tail call i8* @strerror(i64 %19)
%21 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %9
%24 = add nsw i32 %12, 1
%25 = tail call i32 @strcpy(i8* %15, i8* %1)
%26 = tail call i32 @strcat(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%27 = tail call i32 @strcat(i8* %15, i8* %0)
%28 = sext i32 %24 to i64
%29 = getelementptr inbounds i8, i8* %15, i64 %28
store i8 0, i8* %29
br label %30
30: ; preds = %6, %23
%31 = phi i8* [ %15, %23 ], [ %0, %6 ]
%32 = call i32 @stat(i8* %31, %struct.stat* %3)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %40
34: ; preds = %30
%35 = load i64, i64* @errno
%36 = load i64, i64* @ENOENT
%37 = icmp eq i64 %35, %36
br i1 %37, label %48, label %38
38: ; preds = %34
%39 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %48
40: ; preds = %30
%41 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%42 = load i32, i32* %41
%43 = tail call i64 @S_ISREG(i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = load i32, i32* @stderr
%47 = tail call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %48
48: ; preds = %40, %34, %2, %45, %38
%49 = phi i32 [ 1, %38 ], [ 0, %45 ], [ 0, %2 ], [ 0, %34 ], [ 0, %40 ]
ret i32 %49
}