#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;
  int fileSize = 0;
  sprintf(userIdString, "%d", userId);
  fd = fopen("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);
  int foundFlag = 0;
  token = strtok(buf, tokenDelim);
  while (token != NULL) {
    if (strcmp(token, name) == 0) {
      token = strtok(NULL, tokenDelim);
      foundFlag = 1;
    }
    if ((strcmp(token, userIdString) == 0) && foundFlag) {
      token = strtok(NULL, tokenDelim);
      break;
    }
    if ((int)(token[0]) > 65 && (int)(token[0]) < 122) {
      foundFlag = 0;
    }
    token = strtok(NULL, tokenDelim);
  }
  switch (atoi(token)) {
  case 0:
    return 0;
  case 1:
    return 0;
  case 2:
    return 1;
  case 3:
    return 1;
  default:
    return 0;
  }
}


x86 assembly (clang, -O0):
<allowedAccess>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe5a(%rip),%ax
mov    %ax,-0x47(%rbp)
movl   $0x0,-0x54(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe43(%rip),%rsi
call   10c0 <sprintf@plt>
lea    0xe3a(%rip),%rdi
lea    0xe3f(%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    0x2de7(%rip),%rcx
cmp    (%rcx),%eax
jne    120e <allowedAccess+0x6e>
jmp    121c <allowedAccess+0x7c>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    11eb <allowedAccess+0x4b>
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
movslq -0x54(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x54(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x60(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x45(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xdaa(%rip),%rsi
call   10c0 <sprintf@plt>
mov    %eax,-0x4c(%rbp)
movl   $0x0,-0x64(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x47(%rbp),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1336 <allowedAccess+0x196>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12c2 <allowedAccess+0x122>
lea    -0x47(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <strtok@plt>
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x64(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12f9 <allowedAccess+0x159>
cmpl   $0x0,-0x64(%rbp)
je     12f9 <allowedAccess+0x159>
lea    -0x47(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    1336 <allowedAccess+0x196>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x41,%eax
jle    1320 <allowedAccess+0x180>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7a,%eax
jge    1320 <allowedAccess+0x180>
movl   $0x0,-0x64(%rbp)
lea    -0x47(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    1288 <allowedAccess+0xe8>
mov    -0x28(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ecx
mov    %rcx,-0x70(%rbp)
sub    $0x3,%eax
ja     1392 <allowedAccess+0x1f2>
mov    -0x70(%rbp),%rax
lea    0xca7(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x0,-0x4(%rbp)
jmp    1399 <allowedAccess+0x1f9>
movl   $0x0,-0x4(%rbp)
jmp    1399 <allowedAccess+0x1f9>
movl   $0x1,-0x4(%rbp)
jmp    1399 <allowedAccess+0x1f9>
movl   $0x1,-0x4(%rbp)
jmp    1399 <allowedAccess+0x1f9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%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,0x8(%rsp)
lea    0xe3e(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %r15d,%edx
call   10c0 <sprintf@plt>
lea    0xe2d(%rip),%rdi
lea    0xe32(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
mov    $0xffffffff,%ebp
mov    0x2df7(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <fgetc@plt>
add    $0x1,%ebp
cmp    (%r12),%eax
jne    1200 <allowedAccess+0x60>
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xdbc(%rip),%rsi
lea    0xb(%rsp),%rdi
mov    %r15d,%edx
call   10c0 <sprintf@plt>
lea    0x8(%rsp),%rsi
mov    %r12,%rdi
call   10a0 <strtok@plt>
xor    %r13d,%r13d
test   %rax,%rax
je     12e2 <allowedAccess+0x142>
mov    %rax,%rbx
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r12
xor    %ebp,%ebp
jmp    129d <allowedAccess+0xfd>
nopw   0x0(%rax,%rax,1)
movzbl (%rbx),%eax
add    $0xbe,%al
cmp    $0x38,%al
cmovb  %r13d,%ebp
xor    %edi,%edi
mov    %r15,%rsi
call   10a0 <strtok@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12e2 <allowedAccess+0x142>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    12bf <allowedAccess+0x11f>
xor    %edi,%edi
mov    %r15,%rsi
call   10a0 <strtok@plt>
mov    %rax,%rbx
mov    $0x1,%ebp
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1280 <allowedAccess+0xe0>
test   %ebp,%ebp
je     1280 <allowedAccess+0xe0>
lea    0x8(%rsp),%rsi
xor    %edi,%edi
call   10a0 <strtok@plt>
mov    %rax,%r13
mov    %r13,%rdi
call   10b0 <atoi@plt>
and    $0xfffffffe,%eax
xor    %ecx,%ecx
cmp    $0x2,%eax
sete   %cl
mov    %ecx,%eax
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 = [12 x i8] c"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 i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [20 x i8]
%10 = alloca [5 x i8]
%11 = alloca [2 x i8]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i64
%16 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%17 = bitcast [2 x i8]* %11 to i8*
store i32 0, i32* %14
%18 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%19 = load i32, i32* %4
%20 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 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* %21, i32** %6
br label %22
22: ; preds = %2, %29
%23 = load i32*, i32** %6
%24 = call i32 @fgetc(i32* %23)
store i32 %24, i32* %13
%25 = load i32, i32* %13
%26 = load i32, i32* @EOF
%27 = icmp eq i32 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %22
br label %32
29: ; preds = %22
%30 = load i32, i32* %14
%31 = add nsw i32 %30, 1
store i32 %31, i32* %14
br label %22
32: ; preds = %28
%33 = load i32*, i32** %6
%34 = call i32 @rewind(i32* %33)
%35 = load i32, i32* %14
%36 = sext i32 %35 to i64
%37 = mul i64 %36, 1
%38 = trunc i64 %37 to i32
%39 = call i8* @malloc(i32 %38)
store i8* %39, i8** %7
%40 = load i8*, i8** %7
%41 = load i32, i32* %14
%42 = load i32*, i32** %6
%43 = call i64 @fread(i8* %40, i32 1, i32 %41, i32* %42)
store i64 %43, i64* %15
%44 = load i32*, i32** %6
%45 = call i32 @fclose(i32* %44)
%46 = getelementptr inbounds [5 x i8], [5 x i8]* %10, i64 0, i64 0
%47 = load i32, i32* %4
%48 = call i32 @sprintf(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %47)
store i32 %48, i32* %12
store i32 0, i32* %16
%49 = load i8*, i8** %7
%50 = getelementptr inbounds [2 x i8], [2 x i8]* %11, i64 0, i64 0
%51 = call i8* @strtok(i8* %49, i8* %50)
store i8* %51, i8** %8
br label %52
52: ; preds = %87, %32
%53 = load i8*, i8** %8
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %90
55: ; preds = %52
%56 = load i8*, i8** %8
%57 = load i8*, i8** %5
%58 = call i64 @strcmp(i8* %56, i8* %57)
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %55
%61 = getelementptr inbounds [2 x i8], [2 x i8]* %11, i64 0, i64 0
%62 = call i8* @strtok(i8* null, i8* %61)
store i8* %62, i8** %8
store i32 1, i32* %16
br label %63
63: ; preds = %60, %55
%64 = load i8*, i8** %8
%65 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%66 = call i64 @strcmp(i8* %64, i8* %65)
%67 = icmp eq i64 %66, 0
br i1 %67, label %68, label %74
68: ; preds = %63
%69 = load i32, i32* %16
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %68
%72 = getelementptr inbounds [2 x i8], [2 x i8]* %11, i64 0, i64 0
%73 = call i8* @strtok(i8* null, i8* %72)
store i8* %73, i8** %8
br label %90
74: ; preds = %68, %63
%75 = load i8*, i8** %8
%76 = getelementptr inbounds i8, i8* %75, i64 0
%77 = load i8, i8* %76
%78 = sext i8 %77 to i32
%79 = icmp sgt i32 %78, 65
br i1 %79, label %80, label %87
80: ; preds = %74
%81 = load i8*, i8** %8
%82 = getelementptr inbounds i8, i8* %81, i64 0
%83 = load i8, i8* %82
%84 = sext i8 %83 to i32
%85 = icmp slt i32 %84, 122
br i1 %85, label %86, label %87
86: ; preds = %80
store i32 0, i32* %16
br label %87
87: ; preds = %86, %80, %74
%88 = getelementptr inbounds [2 x i8], [2 x i8]* %11, i64 0, i64 0
%89 = call i8* @strtok(i8* null, i8* %88)
store i8* %89, i8** %8
br label %52
90: ; preds = %71, %52
%91 = load i8*, i8** %8
%92 = call i32 @atoi(i8* %91)
switch i32 %92, label %97 [
i32 0, label %93
i32 1, label %94
i32 2, label %95
i32 3, label %96
]
93: ; preds = %90
store i32 0, i32* %3
br label %98
94: ; preds = %90
store i32 0, i32* %3
br label %98
95: ; preds = %90
store i32 1, i32* %3
br label %98
96: ; preds = %90
store i32 1, i32* %3
br label %98
97: ; preds = %90
store i32 0, i32* %3
br label %98
98: ; preds = %97, %96, %95, %94, %93
%99 = load i32, i32* %3
ret i32 %99
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [12 x i8] c"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 ([12 x i8], [12 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 %48, label %25
25: ; preds = %17, %41
%26 = phi i32 [ %45, %41 ], [ 0, %17 ]
%27 = phi i8* [ %46, %41 ], [ %23, %17 ]
%28 = call i64 @strcmp(i8* %27, i8* %1)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i8* @strtok(i8* null, i8* %8)
br label %32
32: ; preds = %30, %25
%33 = phi i8* [ %31, %30 ], [ %27, %25 ]
%34 = phi i32 [ 1, %30 ], [ %26, %25 ]
%35 = call i64 @strcmp(i8* %33, i8* %6)
%36 = icmp eq i64 %35, 0
%37 = icmp ne i32 %34, 0
%38 = select i1 %36, i1 %37, i1 false
br i1 %38, label %39, label %41
39: ; preds = %32
%40 = call i8* @strtok(i8* null, i8* %8)
br label %48
41: ; preds = %32
%42 = load i8, i8* %33
%43 = add i8 %42, -66
%44 = icmp ult i8 %43, 56
%45 = select i1 %44, i32 0, i32 %34
%46 = call i8* @strtok(i8* null, i8* %8)
%47 = icmp eq i8* %46, null
br i1 %47, label %48, label %25
48: ; preds = %41, %17, %39
%49 = phi i8* [ %40, %39 ], [ null, %17 ], [ null, %41 ]
%50 = call i32 @atoi(i8* %49)
%51 = and i32 %50, -2
%52 = icmp eq i32 %51, 2
%53 = zext i1 %52 to i32
ret i32 %53
}