#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_size;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int F_OK;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
scalar_t__ access(char const *, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ stat(char const *, struct stat *);
int check_tlv_path(const char *path, int flag_access) {
  int tlv_size = (1 + 3);
  struct stat st_path;
  if (access(path, F_OK | flag_access) < 0) {
    printf("[utils.c|check_tlv_path]file %s not exist\n", path);
    return -1;
  }
  if (stat(path, &st_path) < 0) {
    printf("[utils.c|check_tlv_path] error stat \n");
    return -1;
  }
  if (!S_ISREG(st_path.st_mode)) {
    printf("[utils.c|check_tlv_path]file %s not regular\n", path);
    return -1;
  }
  if (st_path.st_size > ((1 << ((3) * 8)) - 1)) {
    printf("[utils.c|check_tlv_path]file %s is to large\n", path);
    return -1;
  }
  tlv_size = st_path.st_size;
  return tlv_size;
}


x86 assembly (clang, -O0):
<check_tlv_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x4,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
or     -0x14(%rbp),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
jge    1193 <check_tlv_path+0x53>
mov    -0x10(%rbp),%rsi
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <check_tlv_path+0xea>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    11c4 <check_tlv_path+0x84>
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <check_tlv_path+0xea>
mov    -0x1c(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%eax
jne    11f3 <check_tlv_path+0xb3>
mov    -0x10(%rbp),%rsi
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <check_tlv_path+0xea>
cmpl   $0xffffff,-0x20(%rbp)
jle    121e <check_tlv_path+0xde>
mov    -0x10(%rbp),%rsi
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <check_tlv_path+0xea>
mov    -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_tlv_path>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2e79(%rip),%rax
or     (%rax),%esi
call   1070 <access@plt>
test   %rax,%rax
js     119e <check_tlv_path+0x4e>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %rax,%rax
js     11a7 <check_tlv_path+0x57>
mov    0xc(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %eax,%eax
je     11b5 <check_tlv_path+0x65>
mov    0x8(%rsp),%eax
cmp    $0x1000000,%eax
jl     11cb <check_tlv_path+0x7b>
lea    0xebc(%rip),%rdi
jmp    11bc <check_tlv_path+0x6c>
lea    0xe5b(%rip),%rdi
jmp    11bc <check_tlv_path+0x6c>
lea    0xed7(%rip),%rdi
call   1030 <puts@plt>
jmp    11c6 <check_tlv_path+0x76>
lea    0xe6f(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@F_OK = global i32 0
@.str = [43 x i8] c"[utils.c|check_tlv_path]file %s not exist\0A\00"
@.str.1 = [38 x i8] c"[utils.c|check_tlv_path] error stat \0A\00"
@.str.2 = [45 x i8] c"[utils.c|check_tlv_path]file %s not regular\0A\00"
@.str.3 = [45 x i8] c"[utils.c|check_tlv_path]file %s is to large\0A\00"
define i32 @check_tlv_path(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 4, i32* %6
%8 = load i8*, i8** %4
%9 = load i32, i32* @F_OK
%10 = load i32, i32* %5
%11 = or i32 %9, %10
%12 = call i64 @access(i8* %8, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
br label %42
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call i64 @stat(i8* %18, %struct.stat* %7)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %42
23: ; preds = %17
%24 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 1
%25 = load i32, i32* %24
%26 = call i32 @S_ISREG(i32 %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %23
%29 = load i8*, i8** %4
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %29)
store i32 -1, i32* %3
br label %42
31: ; preds = %23
%32 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%33 = load i32, i32* %32
%34 = icmp sgt i32 %33, 16777215
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i8*, i8** %4
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i8* %36)
store i32 -1, i32* %3
br label %42
38: ; preds = %31
%39 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%40 = load i32, i32* %39
store i32 %40, i32* %6
%41 = load i32, i32* %6
store i32 %41, i32* %3
br label %42
42: ; preds = %38, %35, %28, %21, %14
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@F_OK = global i32 0
@.str = [43 x i8] c"[utils.c|check_tlv_path]file %s not exist\0A\00"
@.str.2 = [45 x i8] c"[utils.c|check_tlv_path]file %s not regular\0A\00"
@.str.3 = [45 x i8] c"[utils.c|check_tlv_path]file %s is to large\0A\00"
@str = [37 x i8] c"[utils.c|check_tlv_path] error stat \00"
define i32 @check_tlv_path(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @F_OK
%6 = or i32 %5, %1
%7 = tail call i64 @access(i8* %0, i32 %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %29
11: ; preds = %2
%12 = call i64 @stat(i8* %0, %struct.stat* %3)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
br label %29
16: ; preds = %11
%17 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%18 = load i32, i32* %17
%19 = call i32 @S_ISREG(i32 %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %29
23: ; preds = %16
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%25 = load i32, i32* %24
%26 = icmp sgt i32 %25, 16777215
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %29
29: ; preds = %23, %27, %21, %14, %9
%30 = phi i32 [ -1, %9 ], [ -1, %14 ], [ -1, %27 ], [ -1, %21 ], [ %25, %23 ]
ret i32 %30
}