#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 smack_accesses {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int smack_accesses_add(struct smack_accesses *, char const *, char const *,
                       char const *);
int smack_accesses_add_modify(struct smack_accesses *, char const *,
                              char const *, char const *, char const *);
scalar_t__ strcmp(char *, char *);
char *strtok_r(char *, char *, char **);
int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd) {
  FILE *file = NULL;
  char buf[(2 * (255 + 1) + 2 * 5 + 1) + 1];
  char *ptr;
  const char *subject, *object, *access, *access2;
  int newfd;
  int ret;
  newfd = dup(fd);
  if (newfd == -1)
    return -1;
  file = fdopen(newfd, "r");
  if (file == NULL) {
    close(newfd);
    return -1;
  }
  while (fgets(buf, (2 * (255 + 1) + 2 * 5 + 1) + 1, file) != NULL) {
    if (strcmp(buf, "\n") == 0)
      continue;
    subject = strtok_r(buf, " \t\n", &ptr);
    object = strtok_r(NULL, " \t\n", &ptr);
    access = strtok_r(NULL, " \t\n", &ptr);
    access2 = strtok_r(NULL, " \t\n", &ptr);
    if (subject == NULL || object == NULL || access == NULL ||
        strtok_r(NULL, " \t\n", &ptr) != NULL) {
      errno = EINVAL;
      fclose(file);
      return -1;
    }
    if (access2 == NULL)
      ret = smack_accesses_add(accesses, subject, object, access);
    else
      ret =
          smack_accesses_add_modify(accesses, subject, object, access, access2);
    if (ret) {
      fclose(file);
      return -1;
    }
  }
  if (ferror(file)) {
    fclose(file);
    return -1;
  }
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<smack_accesses_add_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <dup@plt>
mov    %eax,-0x25c(%rbp)
cmpl   $0xffffffff,-0x25c(%rbp)
jne    11e1 <smack_accesses_add_from_file+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    140d <smack_accesses_add_from_file+0x26d>
mov    -0x25c(%rbp),%edi
lea    0xe12(%rip),%rsi
call   10c0 <fdopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1219 <smack_accesses_add_from_file+0x79>
mov    -0x25c(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    140d <smack_accesses_add_from_file+0x26d>
jmp    121e <smack_accesses_add_from_file+0x7e>
lea    -0x230(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x20c,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
je     13d5 <smack_accesses_add_from_file+0x235>
lea    -0x230(%rbp),%rdi
lea    0xdb9(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    125f <smack_accesses_add_from_file+0xbf>
jmp    121e <smack_accesses_add_from_file+0x7e>
lea    -0x230(%rbp),%rdi
lea    0xd95(%rip),%rsi
lea    -0x238(%rbp),%rdx
call   1080 <strtok_r@plt>
mov    %rax,-0x240(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd77(%rip),%rsi
lea    -0x238(%rbp),%rdx
call   1080 <strtok_r@plt>
mov    %rax,-0x248(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd59(%rip),%rsi
lea    -0x238(%rbp),%rdx
call   1080 <strtok_r@plt>
mov    %rax,-0x250(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd3b(%rip),%rsi
lea    -0x238(%rbp),%rdx
call   1080 <strtok_r@plt>
mov    %rax,-0x258(%rbp)
cmpq   $0x0,-0x240(%rbp)
je     1325 <smack_accesses_add_from_file+0x185>
cmpq   $0x0,-0x248(%rbp)
je     1325 <smack_accesses_add_from_file+0x185>
cmpq   $0x0,-0x250(%rbp)
je     1325 <smack_accesses_add_from_file+0x185>
xor    %eax,%eax
mov    %eax,%edi
lea    0xcf3(%rip),%rsi
lea    -0x238(%rbp),%rdx
call   1080 <strtok_r@plt>
cmp    $0x0,%rax
je     134c <smack_accesses_add_from_file+0x1ac>
mov    0x2cac(%rip),%rax
mov    (%rax),%ecx
mov    0x2cab(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    140d <smack_accesses_add_from_file+0x26d>
cmpq   $0x0,-0x258(%rbp)
jne    1383 <smack_accesses_add_from_file+0x1e3>
mov    -0x10(%rbp),%rdi
mov    -0x240(%rbp),%rsi
mov    -0x248(%rbp),%rdx
mov    -0x250(%rbp),%rcx
call   10b0 <smack_accesses_add@plt>
mov    %eax,-0x260(%rbp)
jmp    13ae <smack_accesses_add_from_file+0x20e>
mov    -0x10(%rbp),%rdi
mov    -0x240(%rbp),%rsi
mov    -0x248(%rbp),%rdx
mov    -0x250(%rbp),%rcx
mov    -0x258(%rbp),%r8
call   1060 <smack_accesses_add_modify@plt>
mov    %eax,-0x260(%rbp)
cmpl   $0x0,-0x260(%rbp)
je     13d0 <smack_accesses_add_from_file+0x230>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    140d <smack_accesses_add_from_file+0x26d>
jmp    121e <smack_accesses_add_from_file+0x7e>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     13fd <smack_accesses_add_from_file+0x25d>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    140d <smack_accesses_add_from_file+0x26d>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smack_accesses_add_from_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x238,%rsp
mov    %rdi,0x10(%rsp)
mov    %esi,%edi
call   1050 <dup@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     1346 <smack_accesses_add_from_file+0x1a6>
mov    %eax,%ebp
lea    0xe2b(%rip),%rsi
mov    %eax,%edi
call   10c0 <fdopen@plt>
test   %rax,%rax
je     12fb <smack_accesses_add_from_file+0x15b>
mov    %rax,%rbx
lea    0x20(%rsp),%rdi
mov    $0x20c,%esi
mov    %rax,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     1304 <smack_accesses_add_from_file+0x164>
lea    0x20(%rsp),%rbp
mov    %rbx,0x8(%rsp)
jmp    124f <smack_accesses_add_from_file+0xaf>
nop
mov    0x10(%rsp),%rdi
mov    %r14,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %rbp,%r8
call   1060 <smack_accesses_add_modify@plt>
test   %eax,%eax
mov    0x8(%rsp),%rbx
mov    %r15,%rbp
jne    1338 <smack_accesses_add_from_file+0x198>
mov    %rbp,%rdi
mov    $0x20c,%esi
mov    %rbx,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     1304 <smack_accesses_add_from_file+0x164>
mov    %rbp,%rdi
lea    0xdab(%rip),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1236 <smack_accesses_add_from_file+0x96>
mov    %rbp,%r15
mov    %rbp,%rdi
lea    0xd92(%rip),%rbp
mov    %rbp,%rsi
lea    0x18(%rsp),%rbx
mov    %rbx,%rdx
call   1080 <strtok_r@plt>
mov    %rax,%r14
xor    %edi,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1080 <strtok_r@plt>
mov    %rax,%r12
xor    %edi,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1080 <strtok_r@plt>
mov    %rax,%r13
xor    %edi,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1080 <strtok_r@plt>
test   %r14,%r14
je     131f <smack_accesses_add_from_file+0x17f>
test   %r12,%r12
je     131f <smack_accesses_add_from_file+0x17f>
test   %r13,%r13
je     131f <smack_accesses_add_from_file+0x17f>
mov    %rax,%rbp
xor    %edi,%edi
lea    0xd37(%rip),%rsi
lea    0x18(%rsp),%rdx
call   1080 <strtok_r@plt>
test   %rax,%rax
jne    131f <smack_accesses_add_from_file+0x17f>
test   %rbp,%rbp
jne    1210 <smack_accesses_add_from_file+0x70>
mov    0x10(%rsp),%rdi
mov    %r14,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   10b0 <smack_accesses_add@plt>
jmp    1226 <smack_accesses_add_from_file+0x86>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    1346 <smack_accesses_add_from_file+0x1a6>
mov    %rbx,%rdi
call   1030 <ferror@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1040 <fclose@plt>
neg    %rbp
sbb    %r14d,%r14d
jmp    1346 <smack_accesses_add_from_file+0x1a6>
mov    0x2cb2(%rip),%rax
mov    (%rax),%eax
mov    0x2cb1(%rip),%rcx
mov    %eax,(%rcx)
mov    0x8(%rsp),%rdi
jmp    133b <smack_accesses_add_from_file+0x19b>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x238,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.smack_accesses = type { i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [4 x i8] c" \09\0A\00"
@EINVAL = global i32 0
@errno = global i32 0
define i32 @smack_accesses_add_from_file(%struct.smack_accesses* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.smack_accesses*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca [524 x i8]
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
store %struct.smack_accesses* %0, %struct.smack_accesses** %4
store i32 %1, i32* %5
store i32* null, i32** %6
%15 = load i32, i32* %5
%16 = call i32 @dup(i32 %15)
store i32 %16, i32* %13
%17 = load i32, i32* %13
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 -1, i32* %3
br label %93
20: ; preds = %2
%21 = load i32, i32* %13
%22 = call i32* @fdopen(i32 %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %22, i32** %6
%23 = load i32*, i32** %6
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i32, i32* %13
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %3
br label %93
28: ; preds = %20
br label %29
29: ; preds = %82, %38, %28
%30 = getelementptr inbounds [524 x i8], [524 x i8]* %7, i64 0, i64 0
%31 = load i32*, i32** %6
%32 = call i32* @fgets(i8* %30, i32 524, i32* %31)
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %83
34: ; preds = %29
%35 = getelementptr inbounds [524 x i8], [524 x i8]* %7, i64 0, i64 0
%36 = call i64 @strcmp(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %34
br label %29
39: ; preds = %34
%40 = getelementptr inbounds [524 x i8], [524 x i8]* %7, i64 0, i64 0
%41 = call i8* @strtok_r(i8* %40, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %8)
store i8* %41, i8** %9
%42 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %8)
store i8* %42, i8** %10
%43 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %8)
store i8* %43, i8** %11
%44 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %8)
store i8* %44, i8** %12
%45 = load i8*, i8** %9
%46 = icmp eq i8* %45, null
br i1 %46, label %56, label %47
47: ; preds = %39
%48 = load i8*, i8** %10
%49 = icmp eq i8* %48, null
br i1 %49, label %56, label %50
50: ; preds = %47
%51 = load i8*, i8** %11
%52 = icmp eq i8* %51, null
br i1 %52, label %56, label %53
53: ; preds = %50
%54 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %8)
%55 = icmp ne i8* %54, null
br i1 %55, label %56, label %60
56: ; preds = %53, %50, %47, %39
%57 = load i32, i32* @EINVAL
store i32 %57, i32* @errno
%58 = load i32*, i32** %6
%59 = call i32 @fclose(i32* %58)
store i32 -1, i32* %3
br label %93
60: ; preds = %53
%61 = load i8*, i8** %12
%62 = icmp eq i8* %61, null
br i1 %62, label %63, label %69
63: ; preds = %60
%64 = load %struct.smack_accesses*, %struct.smack_accesses** %4
%65 = load i8*, i8** %9
%66 = load i8*, i8** %10
%67 = load i8*, i8** %11
%68 = call i32 @smack_accesses_add(%struct.smack_accesses* %64, i8* %65, i8* %66, i8* %67)
store i32 %68, i32* %14
br label %76
69: ; preds = %60
%70 = load %struct.smack_accesses*, %struct.smack_accesses** %4
%71 = load i8*, i8** %9
%72 = load i8*, i8** %10
%73 = load i8*, i8** %11
%74 = load i8*, i8** %12
%75 = call i32 @smack_accesses_add_modify(%struct.smack_accesses* %70, i8* %71, i8* %72, i8* %73, i8* %74)
store i32 %75, i32* %14
br label %76
76: ; preds = %69, %63
%77 = load i32, i32* %14
%78 = icmp ne i32 %77, 0
br i1 %78, label %79, label %82
79: ; preds = %76
%80 = load i32*, i32** %6
%81 = call i32 @fclose(i32* %80)
store i32 -1, i32* %3
br label %93
82: ; preds = %76
br label %29
83: ; preds = %29
%84 = load i32*, i32** %6
%85 = call i64 @ferror(i32* %84)
%86 = icmp ne i64 %85, 0
br i1 %86, label %87, label %90
87: ; preds = %83
%88 = load i32*, i32** %6
%89 = call i32 @fclose(i32* %88)
store i32 -1, i32* %3
br label %93
90: ; preds = %83
%91 = load i32*, i32** %6
%92 = call i32 @fclose(i32* %91)
store i32 0, i32* %3
br label %93
93: ; preds = %90, %87, %79, %56, %25, %19
%94 = load i32, i32* %3
ret i32 %94
}

LLVM IR (-O2):
%struct.smack_accesses = type { i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [4 x i8] c" \09\0A\00"
@EINVAL = global i32 0
@errno = global i32 0
define i32 @smack_accesses_add_from_file(%struct.smack_accesses* %0, i32 %1) {
%3 = alloca [524 x i8]
%4 = alloca i8*
%5 = getelementptr inbounds [524 x i8], [524 x i8]* %3, i64 0, i64 0
%6 = bitcast i8** %4 to i8*
%7 = tail call i32 @dup(i32 %1)
%8 = icmp eq i32 %7, -1
br i1 %8, label %55, label %9
9: ; preds = %2
%10 = tail call i32* @fdopen(i32 %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = call i32* @fgets(i8* %5, i32 524, i32* %10)
%14 = icmp eq i32* %13, null
br i1 %14, label %50, label %17
15: ; preds = %9
%16 = tail call i32 @close(i32 %7)
br label %55
17: ; preds = %12, %20
%18 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17, %45
%21 = call i32* @fgets(i8* %5, i32 524, i32* %10)
%22 = icmp eq i32* %21, null
br i1 %22, label %50, label %17
23: ; preds = %17
%24 = call i8* @strtok_r(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %4)
%25 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %4)
%26 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %4)
%27 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %4)
%28 = icmp eq i8* %24, null
%29 = icmp eq i8* %25, null
%30 = select i1 %28, i1 true, i1 %29
%31 = icmp eq i8* %26, null
%32 = select i1 %30, i1 true, i1 %31
br i1 %32, label %36, label %33
33: ; preds = %23
%34 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %4)
%35 = icmp eq i8* %34, null
br i1 %35, label %39, label %36
36: ; preds = %33, %23
%37 = load i32, i32* @EINVAL
store i32 %37, i32* @errno
%38 = call i32 @fclose(i32* %10)
br label %55
39: ; preds = %33
%40 = icmp eq i8* %27, null
br i1 %40, label %41, label %43
41: ; preds = %39
%42 = call i32 @smack_accesses_add(%struct.smack_accesses* %0, i8* %24, i8* %25, i8* %26)
br label %45
43: ; preds = %39
%44 = call i32 @smack_accesses_add_modify(%struct.smack_accesses* %0, i8* %24, i8* %25, i8* %26, i8* %27)
br label %45
45: ; preds = %43, %41
%46 = phi i32 [ %42, %41 ], [ %44, %43 ]
%47 = icmp eq i32 %46, 0
br i1 %47, label %20, label %48
48: ; preds = %45
%49 = call i32 @fclose(i32* %10)
br label %55
50: ; preds = %20, %12
%51 = call i64 @ferror(i32* %10)
%52 = icmp ne i64 %51, 0
%53 = call i32 @fclose(i32* %10)
%54 = sext i1 %52 to i32
br label %55
55: ; preds = %50, %2, %48, %36, %15
%56 = phi i32 [ -1, %15 ], [ -1, %36 ], [ -1, %48 ], [ -1, %2 ], [ %54, %50 ]
ret i32 %56
}