#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 */
typedef int /*<<< orphan*/ tmp;
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ eout(char *, ...);
int /*<<< orphan*/ errno;
scalar_t__ lstat(char *, struct stat *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ *strchr(char *, char);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int strlen(char *);
char *strtok(char *, char *);
int do_sanitize_page_files(char *dir, char *filelist) {
  char *p, *copy, tmp[300];
  struct stat stb;
  int n = 0;
  int l;
  l = strlen(filelist);
  if (l == 0)
    return 0;
  copy = malloc(l + 1);
  if (copy == NULL) {
    eout("in do_sanitize: cannot malloc() %d bytes, abort\n", l);
    return -1;
  }
  memcpy(copy, filelist, l + 1);
  p = strtok(copy, " \t\n");
  while (p != NULL) {
    if (strchr(p, '/') != NULL) {
      eout("non-local file name: '%s', abort\n", p);
      return -1;
    }
    if (strlen(dir) + strlen(p) + 3 >= sizeof(tmp)) {
      eout("file name '%s' too long, abort\n", p);
      return -1;
    }
    sprintf(tmp, "%s/%s", dir, p);
    if (lstat(tmp, &stb) < 0) {
      eout("can't stat file '%s': %s\n", tmp, strerror(errno));
      return -1;
    }
    if (!S_ISREG(stb.st_mode)) {
      eout("'%s' is not a regular file, abort\n", tmp);
      return -1;
    }
    n++;
    p = strtok(NULL, " \t\n");
  }
  return n;
}


x86 assembly (clang, -O0):
<do_sanitize_page_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x180,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x16c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x170(%rbp)
cmpl   $0x0,-0x170(%rbp)
jne    11e5 <do_sanitize_page_files+0x45>
movl   $0x0,-0x4(%rbp)
jmp    13bb <do_sanitize_page_files+0x21b>
mov    -0x170(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1222 <do_sanitize_page_files+0x82>
mov    -0x170(%rbp),%esi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   10a0 <eout@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bb <do_sanitize_page_files+0x21b>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x170(%rbp),%edx
add    $0x1,%edx
call   1060 <memcpy@plt>
mov    -0x28(%rbp),%rdi
lea    0xdee(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     13b2 <do_sanitize_page_files+0x212>
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     128d <do_sanitize_page_files+0xed>
mov    -0x20(%rbp),%rsi
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   10a0 <eout@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bb <do_sanitize_page_files+0x21b>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x174(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x174(%rbp),%eax
add    %ecx,%eax
add    $0x3,%eax
cltq
cmp    $0x12c,%rax
jb     12de <do_sanitize_page_files+0x13e>
mov    -0x20(%rbp),%rsi
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   10a0 <eout@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bb <do_sanitize_page_files+0x21b>
lea    -0x160(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xd83(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x160(%rbp),%rdi
lea    -0x168(%rbp),%rsi
call   1050 <lstat@plt>
cmp    $0x0,%rax
jge    1355 <do_sanitize_page_files+0x1b5>
lea    -0x160(%rbp),%rax
mov    %rax,-0x180(%rbp)
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x180(%rbp),%rsi
mov    %eax,%edx
lea    0xd3b(%rip),%rdi
mov    $0x0,%al
call   10a0 <eout@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bb <do_sanitize_page_files+0x21b>
mov    -0x168(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%eax
jne    138a <do_sanitize_page_files+0x1ea>
lea    -0x160(%rbp),%rsi
lea    0xd20(%rip),%rdi
mov    $0x0,%al
call   10a0 <eout@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bb <do_sanitize_page_files+0x21b>
mov    -0x16c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x16c(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xc8d(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    124c <do_sanitize_page_files+0xac>
mov    -0x16c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x180,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_sanitize_page_files>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x148,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <strlen@plt>
xor    %ebx,%ebx
test   %eax,%eax
je     130d <do_sanitize_page_files+0x16d>
mov    %eax,%ebp
lea    0x1(%rbp),%r12d
mov    %r12d,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     12a9 <do_sanitize_page_files+0x109>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1060 <memcpy@plt>
lea    0xe39(%rip),%rsi
mov    %r13,%rdi
call   1090 <strtok@plt>
test   %rax,%rax
je     130d <do_sanitize_page_files+0x16d>
mov    %rax,%rbp
xor    %ebx,%ebx
lea    0x10(%rsp),%r12
lea    0x8(%rsp),%r13
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
test   %rax,%rax
jne    12bb <do_sanitize_page_files+0x11b>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%r15d
mov    %rbp,%rdi
call   1030 <strlen@plt>
add    %r15d,%eax
add    $0xfffffed7,%eax
cmp    $0xfffffed3,%eax
jbe    12c4 <do_sanitize_page_files+0x124>
mov    %r12,%rdi
lea    0xe15(%rip),%rsi
mov    %r14,%rdx
mov    %rbp,%rcx
call   10b0 <sprintf@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   1050 <lstat@plt>
test   %rax,%rax
js     12d0 <do_sanitize_page_files+0x130>
mov    0x8(%rsp),%edi
call   1070 <S_ISREG@plt>
test   %eax,%eax
je     12f5 <do_sanitize_page_files+0x155>
add    $0x1,%ebx
xor    %edi,%edi
lea    0xd9b(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1220 <do_sanitize_page_files+0x80>
jmp    130d <do_sanitize_page_files+0x16d>
lea    0xd50(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   10a0 <eout@plt>
jmp    1308 <do_sanitize_page_files+0x168>
lea    0xd73(%rip),%rdi
jmp    12cb <do_sanitize_page_files+0x12b>
lea    0xd8c(%rip),%rdi
mov    %rbp,%rsi
jmp    1301 <do_sanitize_page_files+0x161>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd98(%rip),%rdi
lea    0x10(%rsp),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10a0 <eout@plt>
jmp    1308 <do_sanitize_page_files+0x168>
lea    0xd9b(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   10a0 <eout@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x148,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [49 x i8] c"in do_sanitize: cannot malloc() %d bytes, abort\0A\00"
@.str.1 = [4 x i8] c" \09\0A\00"
@.str.2 = [34 x i8] c"non-local file name: '%s', abort\0A\00"
@.str.3 = [32 x i8] c"file name '%s' too long, abort\0A\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [26 x i8] c"can't stat file '%s': %s\0A\00"
@errno = global i32 0
@.str.6 = [35 x i8] c"'%s' is not a regular file, abort\0A\00"
define i32 @do_sanitize_page_files(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [300 x i8]
%9 = alloca %struct.stat
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %10
%12 = load i8*, i8** %5
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %11
%14 = load i32, i32* %11
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 0, i32* %3
br label %83
17: ; preds = %2
%18 = load i32, i32* %11
%19 = add nsw i32 %18, 1
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** %7
%21 = load i8*, i8** %7
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = load i32, i32* %11
%25 = call i32 (i8*, ...) @eout(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %24)
store i32 -1, i32* %3
br label %83
26: ; preds = %17
%27 = load i8*, i8** %7
%28 = load i8*, i8** %5
%29 = load i32, i32* %11
%30 = add nsw i32 %29, 1
%31 = call i32 @memcpy(i8* %27, i8* %28, i32 %30)
%32 = load i8*, i8** %7
%33 = call i8* @strtok(i8* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i8* %33, i8** %6
br label %34
34: ; preds = %77, %26
%35 = load i8*, i8** %6
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %81
37: ; preds = %34
%38 = load i8*, i8** %6
%39 = call i32* @strchr(i8* %38, i8 signext 47)
%40 = icmp ne i32* %39, null
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = load i8*, i8** %6
%43 = call i32 (i8*, ...) @eout(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %42)
store i32 -1, i32* %3
br label %83
44: ; preds = %37
%45 = load i8*, i8** %4
%46 = call i32 @strlen(i8* %45)
%47 = load i8*, i8** %6
%48 = call i32 @strlen(i8* %47)
%49 = add nsw i32 %46, %48
%50 = add nsw i32 %49, 3
%51 = sext i32 %50 to i64
%52 = icmp uge i64 %51, 300
br i1 %52, label %53, label %56
53: ; preds = %44
%54 = load i8*, i8** %6
%55 = call i32 (i8*, ...) @eout(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %54)
store i32 -1, i32* %3
br label %83
56: ; preds = %44
%57 = getelementptr inbounds [300 x i8], [300 x i8]* %8, i64 0, i64 0
%58 = load i8*, i8** %4
%59 = load i8*, i8** %6
%60 = call i32 @sprintf(i8* %57, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %58, i8* %59)
%61 = getelementptr inbounds [300 x i8], [300 x i8]* %8, i64 0, i64 0
%62 = call i64 @lstat(i8* %61, %struct.stat* %9)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %69
64: ; preds = %56
%65 = getelementptr inbounds [300 x i8], [300 x i8]* %8, i64 0, i64 0
%66 = load i32, i32* @errno
%67 = call i32 @strerror(i32 %66)
%68 = call i32 (i8*, ...) @eout(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %65, i32 %67)
store i32 -1, i32* %3
br label %83
69: ; preds = %56
%70 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%71 = load i32, i32* %70
%72 = call i32 @S_ISREG(i32 %71)
%73 = icmp ne i32 %72, 0
br i1 %73, label %77, label %74
74: ; preds = %69
%75 = getelementptr inbounds [300 x i8], [300 x i8]* %8, i64 0, i64 0
%76 = call i32 (i8*, ...) @eout(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i8* %75)
store i32 -1, i32* %3
br label %83
77: ; preds = %69
%78 = load i32, i32* %10
%79 = add nsw i32 %78, 1
store i32 %79, i32* %10
%80 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i8* %80, i8** %6
br label %34
81: ; preds = %34
%82 = load i32, i32* %10
store i32 %82, i32* %3
br label %83
83: ; preds = %81, %74, %64, %53, %41, %23, %16
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [49 x i8] c"in do_sanitize: cannot malloc() %d bytes, abort\0A\00"
@.str.1 = [4 x i8] c" \09\0A\00"
@.str.2 = [34 x i8] c"non-local file name: '%s', abort\0A\00"
@.str.3 = [32 x i8] c"file name '%s' too long, abort\0A\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [26 x i8] c"can't stat file '%s': %s\0A\00"
@errno = global i32 0
@.str.6 = [35 x i8] c"'%s' is not a regular file, abort\0A\00"
define i32 @do_sanitize_page_files(i8* %0, i8* %1) {
%3 = alloca [300 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [300 x i8], [300 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = tail call i32 @strlen(i8* %1)
%8 = icmp eq i32 %7, 0
br i1 %8, label %54, label %9
9: ; preds = %2
%10 = add nsw i32 %7, 1
%11 = tail call i8* @malloc(i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 (i8*, ...) @eout(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %54
15: ; preds = %9
%16 = tail call i32 @memcpy(i8* %11, i8* %1, i32 %10)
%17 = tail call i8* @strtok(i8* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i8* %17, null
br i1 %18, label %54, label %19
19: ; preds = %15
%20 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %21
21: ; preds = %19, %50
%22 = phi i32 [ 0, %19 ], [ %51, %50 ]
%23 = phi i8* [ %17, %19 ], [ %52, %50 ]
%24 = tail call i32* @strchr(i8* %23, i8 signext 47)
%25 = icmp eq i32* %24, null
br i1 %25, label %28, label %26
26: ; preds = %21
%27 = tail call i32 (i8*, ...) @eout(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %23)
br label %54
28: ; preds = %21
%29 = tail call i32 @strlen(i8* %0)
%30 = tail call i32 @strlen(i8* %23)
%31 = add i32 %29, -297
%32 = add i32 %31, %30
%33 = icmp ult i32 %32, -300
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = tail call i32 (i8*, ...) @eout(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %23)
br label %54
36: ; preds = %28
%37 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %23)
%38 = call i64 @lstat(i8* %5, %struct.stat* %4)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = load i32, i32* @errno
%42 = tail call i32 @strerror(i32 %41)
%43 = call i32 (i8*, ...) @eout(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %5, i32 %42)
br label %54
44: ; preds = %36
%45 = load i32, i32* %20
%46 = tail call i32 @S_ISREG(i32 %45)
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = call i32 (i8*, ...) @eout(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i8* %5)
br label %54
50: ; preds = %44
%51 = add nuw nsw i32 %22, 1
%52 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%53 = icmp eq i8* %52, null
br i1 %53, label %54, label %21
54: ; preds = %50, %15, %2, %48, %40, %34, %26, %13
%55 = phi i32 [ -1, %13 ], [ -1, %26 ], [ -1, %34 ], [ -1, %40 ], [ -1, %48 ], [ 0, %2 ], [ 0, %15 ], [ %51, %50 ]
ret i32 %55
}