#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*/ FILE;

/* Variables and functions */
int EOF;
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int sprintf(char *, char *, int);
scalar_t__ strcmp(char *, char const *);
char *strtok(char *, char *);
int allowedAccess(int userId, const char *name) {
  FILE *fd;
  char *buf;
  char *token;
  char userIdString[20];
  char user[5];
  char tokenDelim[2] = "\n";
  int retval, ch, filePerm, userPerm;
  int fileSize = 0;
  int isAllowed = 0;
  sprintf(userIdString, "%d", userId);
  int pairFound = 0;
  fd = fopen("blp_permissions", "r");
  while (1) {
    ch = fgetc(fd);
    if (ch == EOF)
      break;
    ++fileSize;
  }
  rewind(fd);
  buf = malloc(fileSize * sizeof(char));
  size_t readCount;
  readCount = fread(buf, 1, fileSize, fd);
  fclose(fd);
  retval = sprintf(user, "%d", userId);
  token = strtok(buf, tokenDelim);
  while (token != NULL) {
    if (strcmp(token, name) == 0) {
      token = strtok(NULL, tokenDelim);
      filePerm = atoi(token);
      pairFound++;
    }
    if ((strcmp(token, userIdString) == 0)) {
      token = strtok(NULL, tokenDelim);
      userPerm = atoi(token);
      pairFound++;
      if (pairFound == 2) {
        break;
      }
    }
    token = strtok(NULL, tokenDelim);
  }
  if (userPerm <= filePerm) {
    isAllowed = 1;
  }
  return isAllowed;
}


x86 assembly (clang, -O0):
<allowedAccess>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe4a(%rip),%ax
mov    %ax,-0x47(%rbp)
movl   $0x0,-0x5c(%rbp)
movl   $0x0,-0x60(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe2c(%rip),%rsi
call   10c0 <sprintf@plt>
movl   $0x0,-0x64(%rbp)
lea    0xe1c(%rip),%rdi
lea    0xe25(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%eax
mov    0x2dd9(%rip),%rcx
cmp    (%rcx),%eax
jne    121c <allowedAccess+0x7c>
jmp    122a <allowedAccess+0x8a>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    11f9 <allowedAccess+0x59>
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
movslq -0x5c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x5c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x70(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x45(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xd8c(%rip),%rsi
call   10c0 <sprintf@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x47(%rbp),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     133e <allowedAccess+0x19e>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12d7 <allowedAccess+0x137>
lea    -0x47(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <strtok@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x54(%rbp)
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1328 <allowedAccess+0x188>
lea    -0x47(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <strtok@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x58(%rbp)
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
cmpl   $0x2,-0x64(%rbp)
jne    1323 <allowedAccess+0x183>
jmp    133e <allowedAccess+0x19e>
jmp    1328 <allowedAccess+0x188>
lea    -0x47(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    128f <allowedAccess+0xef>
mov    -0x58(%rbp),%eax
cmp    -0x54(%rbp),%eax
jg     1351 <allowedAccess+0x1b1>
movl   $0x1,-0x60(%rbp)
mov    -0x60(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allowedAccess>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
movw   $0xa,0x2(%rsp)
lea    0xe3e(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %r15d,%edx
call   10c0 <sprintf@plt>
lea    0xe2d(%rip),%rdi
lea    0xe36(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbp
mov    $0xffffffff,%ebx
mov    0x2df7(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1060 <fgetc@plt>
add    $0x1,%ebx
cmp    (%r12),%eax
jne    1200 <allowedAccess+0x60>
mov    %rbp,%rdi
call   1050 <rewind@plt>
mov    %ebx,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %rbp,%rcx
call   1030 <fread@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
lea    0xdbc(%rip),%rsi
lea    0xb(%rsp),%rdi
mov    %r15d,%edx
call   10c0 <sprintf@plt>
lea    0x2(%rsp),%rsi
mov    %r12,%rdi
call   10a0 <strtok@plt>
test   %rax,%rax
je     12ef <allowedAccess+0x14f>
mov    %rax,%rbx
xor    %r12d,%r12d
lea    0x2(%rsp),%r13
lea    0x10(%rsp),%r15
jmp    1292 <allowedAccess+0xf2>
nopl   0x0(%rax)
xor    %edi,%edi
mov    %r13,%rsi
call   10a0 <strtok@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12ef <allowedAccess+0x14f>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    12bf <allowedAccess+0x11f>
xor    %edi,%edi
mov    %r13,%rsi
call   10a0 <strtok@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   10b0 <atoi@plt>
mov    %eax,0x4(%rsp)
add    $0x1,%r12d
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1280 <allowedAccess+0xe0>
xor    %edi,%edi
mov    %r13,%rsi
call   10a0 <strtok@plt>
mov    %rax,%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebp
add    $0x1,%r12d
cmp    $0x2,%r12d
jne    1280 <allowedAccess+0xe0>
jmp    12ef <allowedAccess+0x14f>
xor    %eax,%eax
cmp    0x4(%rsp),%ebp
setle  %al
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.allowedAccess.tokenDelim = [2 x i8] c"\0A\00"
@.str = [3 x i8] c"%d\00"
@.str.1 = [16 x i8] c"blp_permissions\00"
@.str.2 = [2 x i8] c"r\00"
@EOF = global i32 0
define i32 @allowedAccess(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [20 x i8]
%9 = alloca [5 x i8]
%10 = alloca [2 x i8]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i64
store i32 %0, i32* %3
store i8* %1, i8** %4
%19 = bitcast [2 x i8]* %10 to i8*
store i32 0, i32* %15
store i32 0, i32* %16
%20 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%21 = load i32, i32* %3
%22 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %21)
store i32 0, i32* %17
%23 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %23, i32** %5
br label %24
24: ; preds = %2, %31
%25 = load i32*, i32** %5
%26 = call i32 @fgetc(i32* %25)
store i32 %26, i32* %12
%27 = load i32, i32* %12
%28 = load i32, i32* @EOF
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %24
br label %34
31: ; preds = %24
%32 = load i32, i32* %15
%33 = add nsw i32 %32, 1
store i32 %33, i32* %15
br label %24
34: ; preds = %30
%35 = load i32*, i32** %5
%36 = call i32 @rewind(i32* %35)
%37 = load i32, i32* %15
%38 = sext i32 %37 to i64
%39 = mul i64 %38, 1
%40 = trunc i64 %39 to i32
%41 = call i8* @malloc(i32 %40)
store i8* %41, i8** %6
%42 = load i8*, i8** %6
%43 = load i32, i32* %15
%44 = load i32*, i32** %5
%45 = call i64 @fread(i8* %42, i32 1, i32 %43, i32* %44)
store i64 %45, i64* %18
%46 = load i32*, i32** %5
%47 = call i32 @fclose(i32* %46)
%48 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%49 = load i32, i32* %3
%50 = call i32 @sprintf(i8* %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %49)
store i32 %50, i32* %11
%51 = load i8*, i8** %6
%52 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%53 = call i8* @strtok(i8* %51, i8* %52)
store i8* %53, i8** %7
br label %54
54: ; preds = %85, %34
%55 = load i8*, i8** %7
%56 = icmp ne i8* %55, null
br i1 %56, label %57, label %88
57: ; preds = %54
%58 = load i8*, i8** %7
%59 = load i8*, i8** %4
%60 = call i64 @strcmp(i8* %58, i8* %59)
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %69
62: ; preds = %57
%63 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%64 = call i8* @strtok(i8* null, i8* %63)
store i8* %64, i8** %7
%65 = load i8*, i8** %7
%66 = call i32 @atoi(i8* %65)
store i32 %66, i32* %13
%67 = load i32, i32* %17
%68 = add nsw i32 %67, 1
store i32 %68, i32* %17
br label %69
69: ; preds = %62, %57
%70 = load i8*, i8** %7
%71 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%72 = call i64 @strcmp(i8* %70, i8* %71)
%73 = icmp eq i64 %72, 0
br i1 %73, label %74, label %85
74: ; preds = %69
%75 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%76 = call i8* @strtok(i8* null, i8* %75)
store i8* %76, i8** %7
%77 = load i8*, i8** %7
%78 = call i32 @atoi(i8* %77)
store i32 %78, i32* %14
%79 = load i32, i32* %17
%80 = add nsw i32 %79, 1
store i32 %80, i32* %17
%81 = load i32, i32* %17
%82 = icmp eq i32 %81, 2
br i1 %82, label %83, label %84
83: ; preds = %74
br label %88
84: ; preds = %74
br label %85
85: ; preds = %84, %69
%86 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%87 = call i8* @strtok(i8* null, i8* %86)
store i8* %87, i8** %7
br label %54
88: ; preds = %83, %54
%89 = load i32, i32* %14
%90 = load i32, i32* %13
%91 = icmp sle i32 %89, %90
br i1 %91, label %92, label %93
92: ; preds = %88
store i32 1, i32* %16
br label %93
93: ; preds = %92, %88
%94 = load i32, i32* %16
ret i32 %94
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [16 x i8] c"blp_permissions\00"
@.str.2 = [2 x i8] c"r\00"
@EOF = global i32 0
define i32 @allowedAccess(i32 %0, i8* %1) {
%3 = alloca [20 x i8]
%4 = alloca [5 x i8]
%5 = alloca i16
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%8 = bitcast i16* %5 to i8*
store i16 10, i16* %5
%9 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
%10 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %11, %2
%12 = phi i32 [ 0, %2 ], [ %16, %11 ]
%13 = tail call i32 @fgetc(i32* %10)
%14 = load i32, i32* @EOF
%15 = icmp eq i32 %13, %14
%16 = add nuw nsw i32 %12, 1
br i1 %15, label %17, label %11
17: ; preds = %11
%18 = tail call i32 @rewind(i32* %10)
%19 = tail call i8* @malloc(i32 %12)
%20 = tail call i64 @fread(i8* %19, i32 1, i32 %12, i32* %10)
%21 = tail call i32 @fclose(i32* %10)
%22 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
%23 = call i8* @strtok(i8* %19, i8* %8)
%24 = icmp eq i8* %23, null
br i1 %24, label %52, label %25
25: ; preds = %17, %47
%26 = phi i8* [ %50, %47 ], [ %23, %17 ]
%27 = phi i32 [ %49, %47 ], [ 0, %17 ]
%28 = phi i32 [ %48, %47 ], [ undef, %17 ]
%29 = phi i32 [ %37, %47 ], [ undef, %17 ]
%30 = call i64 @strcmp(i8* %26, i8* %1)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %25
%33 = call i8* @strtok(i8* null, i8* %8)
%34 = call i32 @atoi(i8* %33)
%35 = add nsw i32 %27, 1
br label %36
36: ; preds = %32, %25
%37 = phi i32 [ %34, %32 ], [ %29, %25 ]
%38 = phi i32 [ %35, %32 ], [ %27, %25 ]
%39 = phi i8* [ %33, %32 ], [ %26, %25 ]
%40 = call i64 @strcmp(i8* %39, i8* %6)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %36
%43 = call i8* @strtok(i8* null, i8* %8)
%44 = call i32 @atoi(i8* %43)
%45 = add nsw i32 %38, 1
%46 = icmp eq i32 %45, 2
br i1 %46, label %52, label %47
47: ; preds = %42, %36
%48 = phi i32 [ %44, %42 ], [ %28, %36 ]
%49 = phi i32 [ %45, %42 ], [ %38, %36 ]
%50 = call i8* @strtok(i8* null, i8* %8)
%51 = icmp eq i8* %50, null
br i1 %51, label %52, label %25
52: ; preds = %47, %42, %17
%53 = phi i32 [ undef, %17 ], [ %37, %42 ], [ %37, %47 ]
%54 = phi i32 [ undef, %17 ], [ %48, %47 ], [ %44, %42 ]
%55 = icmp sle i32 %54, %53
%56 = zext i1 %55 to i32
ret i32 %56
}