#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*/ *);
scalar_t__ smack_accesses_add(struct smack_accesses *, char const *,
                              char const *, char const *);
char *strtok_r(char *, char *, char **);
int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd) {
  FILE *file = NULL;
  char buf[512];
  char *ptr;
  const char *subject, *object, *access;
  int newfd;
  newfd = dup(fd);
  if (newfd == -1)
    return -1;
  file = fdopen(newfd, "r");
  if (file == NULL) {
    close(newfd);
    return -1;
  }
  while (fgets(buf, 512, file) != NULL) {
    subject = strtok_r(buf, " \t\n", &ptr);
    object = strtok_r(NULL, " \t\n", &ptr);
    access = 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 (smack_accesses_add(accesses, subject, object, access)) {
      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    $0x250,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <dup@plt>
mov    %eax,-0x244(%rbp)
cmpl   $0xffffffff,-0x244(%rbp)
jne    11c1 <smack_accesses_add_from_file+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1366 <smack_accesses_add_from_file+0x1e6>
mov    -0x244(%rbp),%edi
lea    0xe32(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f9 <smack_accesses_add_from_file+0x79>
mov    -0x244(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1366 <smack_accesses_add_from_file+0x1e6>
jmp    11fe <smack_accesses_add_from_file+0x7e>
lea    -0x220(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x200,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     132e <smack_accesses_add_from_file+0x1ae>
lea    -0x220(%rbp),%rdi
lea    0xdd7(%rip),%rsi
lea    -0x228(%rbp),%rdx
call   1070 <strtok_r@plt>
mov    %rax,-0x230(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xdb9(%rip),%rsi
lea    -0x228(%rbp),%rdx
call   1070 <strtok_r@plt>
mov    %rax,-0x238(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd9b(%rip),%rsi
lea    -0x228(%rbp),%rdx
call   1070 <strtok_r@plt>
mov    %rax,-0x240(%rbp)
cmpq   $0x0,-0x230(%rbp)
je     12c5 <smack_accesses_add_from_file+0x145>
cmpq   $0x0,-0x238(%rbp)
je     12c5 <smack_accesses_add_from_file+0x145>
cmpq   $0x0,-0x240(%rbp)
je     12c5 <smack_accesses_add_from_file+0x145>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd53(%rip),%rsi
lea    -0x228(%rbp),%rdx
call   1070 <strtok_r@plt>
cmp    $0x0,%rax
je     12ec <smack_accesses_add_from_file+0x16c>
mov    0x2d0c(%rip),%rax
mov    (%rax),%ecx
mov    0x2d0b(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1366 <smack_accesses_add_from_file+0x1e6>
mov    -0x10(%rbp),%rdi
mov    -0x230(%rbp),%rsi
mov    -0x238(%rbp),%rdx
mov    -0x240(%rbp),%rcx
call   1090 <smack_accesses_add@plt>
cmp    $0x0,%rax
je     1329 <smack_accesses_add_from_file+0x1a9>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1366 <smack_accesses_add_from_file+0x1e6>
jmp    11fe <smack_accesses_add_from_file+0x7e>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1356 <smack_accesses_add_from_file+0x1d6>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1366 <smack_accesses_add_from_file+0x1e6>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x250,%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    $0x228,%rsp
mov    %rdi,0x10(%rsp)
mov    %esi,%edi
call   1050 <dup@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     12c1 <smack_accesses_add_from_file+0x141>
mov    %eax,%ebx
lea    0xe4c(%rip),%rsi
mov    %eax,%edi
call   10a0 <fdopen@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     1278 <smack_accesses_add_from_file+0xf8>
lea    0x20(%rsp),%rbx
lea    0x18(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x200,%esi
mov    0x8(%rsp),%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     12a4 <smack_accesses_add_from_file+0x124>
mov    %rbx,%r13
mov    %rbx,%rdi
lea    0xdfa(%rip),%rbx
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1070 <strtok_r@plt>
mov    %rax,%r14
xor    %edi,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1070 <strtok_r@plt>
mov    %rax,%r15
xor    %edi,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1070 <strtok_r@plt>
test   %r14,%r14
je     1281 <smack_accesses_add_from_file+0x101>
test   %r15,%r15
je     1281 <smack_accesses_add_from_file+0x101>
mov    %rax,%r12
test   %rax,%rax
je     1281 <smack_accesses_add_from_file+0x101>
xor    %edi,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1070 <strtok_r@plt>
test   %rax,%rax
jne    1281 <smack_accesses_add_from_file+0x101>
mov    0x10(%rsp),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
call   1090 <smack_accesses_add@plt>
test   %rax,%rax
mov    %r13,%rbx
je     11e0 <smack_accesses_add_from_file+0x60>
jmp    1293 <smack_accesses_add_from_file+0x113>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    12c1 <smack_accesses_add_from_file+0x141>
mov    0x2d50(%rip),%rax
mov    (%rax),%eax
mov    0x2d4f(%rip),%rcx
mov    %eax,(%rcx)
mov    0x8(%rsp),%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%ebp
jmp    12c1 <smack_accesses_add_from_file+0x141>
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <ferror@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1040 <fclose@plt>
neg    %rbx
sbb    %ebp,%ebp
mov    %ebp,%eax
add    $0x228,%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 = [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 [512 x i8]
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
store %struct.smack_accesses* %0, %struct.smack_accesses** %4
store i32 %1, i32* %5
store i32* null, i32** %6
%13 = load i32, i32* %5
%14 = call i32 @dup(i32 %13)
store i32 %14, i32* %12
%15 = load i32, i32* %12
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -1, i32* %3
br label %73
18: ; preds = %2
%19 = load i32, i32* %12
%20 = call i32* @fdopen(i32 %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %6
%21 = load i32*, i32** %6
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32, i32* %12
%25 = call i32 @close(i32 %24)
store i32 -1, i32* %3
br label %73
26: ; preds = %18
br label %27
27: ; preds = %62, %26
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%29 = load i32*, i32** %6
%30 = call i32* @fgets(i8* %28, i32 512, i32* %29)
%31 = icmp ne i32* %30, null
br i1 %31, label %32, label %63
32: ; preds = %27
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%34 = call i8* @strtok_r(i8* %33, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %8)
store i8* %34, i8** %9
%35 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %8)
store i8* %35, i8** %10
%36 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %8)
store i8* %36, i8** %11
%37 = load i8*, i8** %9
%38 = icmp eq i8* %37, null
br i1 %38, label %48, label %39
39: ; preds = %32
%40 = load i8*, i8** %10
%41 = icmp eq i8* %40, null
br i1 %41, label %48, label %42
42: ; preds = %39
%43 = load i8*, i8** %11
%44 = icmp eq i8* %43, null
br i1 %44, label %48, label %45
45: ; preds = %42
%46 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %8)
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %52
48: ; preds = %45, %42, %39, %32
%49 = load i32, i32* @EINVAL
store i32 %49, i32* @errno
%50 = load i32*, i32** %6
%51 = call i32 @fclose(i32* %50)
store i32 -1, i32* %3
br label %73
52: ; preds = %45
%53 = load %struct.smack_accesses*, %struct.smack_accesses** %4
%54 = load i8*, i8** %9
%55 = load i8*, i8** %10
%56 = load i8*, i8** %11
%57 = call i64 @smack_accesses_add(%struct.smack_accesses* %53, i8* %54, i8* %55, i8* %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %52
%60 = load i32*, i32** %6
%61 = call i32 @fclose(i32* %60)
store i32 -1, i32* %3
br label %73
62: ; preds = %52
br label %27
63: ; preds = %27
%64 = load i32*, i32** %6
%65 = call i64 @ferror(i32* %64)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %63
%68 = load i32*, i32** %6
%69 = call i32 @fclose(i32* %68)
store i32 -1, i32* %3
br label %73
70: ; preds = %63
%71 = load i32*, i32** %6
%72 = call i32 @fclose(i32* %71)
store i32 0, i32* %3
br label %73
73: ; preds = %70, %67, %59, %48, %23, %17
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.smack_accesses = type { i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [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 [512 x i8]
%4 = alloca i8*
%5 = getelementptr inbounds [512 x i8], [512 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 %42, 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 %12, label %14
12: ; preds = %9
%13 = tail call i32 @close(i32 %7)
br label %42
14: ; preds = %9, %32
%15 = call i32* @fgets(i8* %5, i32 512, i32* %10)
%16 = icmp eq i32* %15, null
br i1 %16, label %37, label %17
17: ; preds = %14
%18 = call i8* @strtok_r(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %4)
%19 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %4)
%20 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %4)
%21 = icmp eq i8* %18, null
%22 = icmp eq i8* %19, null
%23 = select i1 %21, i1 true, i1 %22
%24 = icmp eq i8* %20, null
%25 = select i1 %23, i1 true, i1 %24
br i1 %25, label %29, label %26
26: ; preds = %17
%27 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %4)
%28 = icmp eq i8* %27, null
br i1 %28, label %32, label %29
29: ; preds = %26, %17
%30 = load i32, i32* @EINVAL
store i32 %30, i32* @errno
%31 = call i32 @fclose(i32* %10)
br label %42
32: ; preds = %26
%33 = call i64 @smack_accesses_add(%struct.smack_accesses* %0, i8* %18, i8* %19, i8* %20)
%34 = icmp eq i64 %33, 0
br i1 %34, label %14, label %35
35: ; preds = %32
%36 = call i32 @fclose(i32* %10)
br label %42
37: ; preds = %14
%38 = call i64 @ferror(i32* %10)
%39 = icmp ne i64 %38, 0
%40 = call i32 @fclose(i32* %10)
%41 = sext i1 %39 to i32
br label %42
42: ; preds = %37, %2, %35, %29, %12
%43 = phi i32 [ -1, %12 ], [ -1, %29 ], [ -1, %35 ], [ -1, %2 ], [ %41, %37 ]
ret i32 %43
}