#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*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int LINE_MAX;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ stderr;
char *strrchr(char *, char);
scalar_t__ strstr(char *, char *);
unsigned long strtoul(char *, int /*<<< orphan*/ *, int);
unsigned long read_flags_from_fdinfo(const char *file) {
  FILE *fp = fopen(file, "r");
  char line[LINE_MAX];
  unsigned long flags = 0;
  if (!fp) {
    fprintf(stderr, "Cannot open %s\n", file);
    exit(EXIT_FAILURE);
  }
  while (fgets(line, sizeof(line), fp) != NULL) {
    if (strstr(line, "flags:")) {
      char *p = strrchr(line, '\t');
      flags = strtoul(p + 1, NULL, 8);
      break;
    }
  }
  fclose(fp);
  return flags;
}


x86 assembly (clang, -O0):
<read_flags_from_fdinfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    120a <read_flags_from_fdinfo+0x8a>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe0b(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    120f <read_flags_from_fdinfo+0x8f>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     127c <read_flags_from_fdinfo+0xfc>
mov    -0x38(%rbp),%rdi
lea    0xddb(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
je     1277 <read_flags_from_fdinfo+0xf7>
mov    -0x38(%rbp),%rdi
mov    $0x9,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x8,%edx
call   1080 <strtoul@plt>
mov    %rax,-0x28(%rbp)
jmp    127c <read_flags_from_fdinfo+0xfc>
jmp    120f <read_flags_from_fdinfo+0x8f>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_flags_from_fdinfo>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
lea    0xe68(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r14
mov    0x2e41(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %r14,%r14
je     122d <read_flags_from_fdinfo+0xad>
xor    %r15d,%r15d
lea    0xe47(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1213 <read_flags_from_fdinfo+0x93>
mov    %rbx,%rdi
mov    %r13,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     11d0 <read_flags_from_fdinfo+0x50>
mov    %rbx,%rdi
mov    $0x9,%esi
call   1040 <strrchr@plt>
lea    0x1(%rax),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <strtoul@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %r15,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
mov    %r15,%rdx
call   1060 <fprintf@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@LINE_MAX = global i32 0
@stderr = global i32 0
@.str.1 = [16 x i8] c"Cannot open %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [7 x i8] c"flags:\00"
define i64 @read_flags_from_fdinfo(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %3
%10 = load i32, i32* @LINE_MAX
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %4
%13 = alloca i8, i64 %11
store i64 %11, i64* %5
store i64 0, i64* %6
%14 = load i32*, i32** %3
%15 = icmp ne i32* %14, null
br i1 %15, label %22, label %16
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %2
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %1
br label %23
23: ; preds = %36, %22
%24 = trunc i64 %11 to i32
%25 = load i32*, i32** %3
%26 = call i32* @fgets(i8* %13, i32 %24, i32* %25)
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %37
28: ; preds = %23
%29 = call i64 @strstr(i8* %13, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = call i8* @strrchr(i8* %13, i8 signext 9)
store i8* %32, i8** %7
%33 = load i8*, i8** %7
%34 = getelementptr inbounds i8, i8* %33, i64 1
%35 = call i64 @strtoul(i8* %34, i32* null, i32 8)
store i64 %35, i64* %6
br label %37
36: ; preds = %28
br label %23
37: ; preds = %31, %23
%38 = load i32*, i32** %3
%39 = call i32 @fclose(i32* %38)
%40 = load i64, i64* %6
%41 = load i8*, i8** %4
ret i64 %40
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@LINE_MAX = global i32 0
@stderr = global i32 0
@.str.1 = [16 x i8] c"Cannot open %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [7 x i8] c"flags:\00"
define i64 @read_flags_from_fdinfo(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = load i32, i32* @LINE_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = icmp eq i32* %2, null
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %1, %15
%13 = call i32* @fgets(i8* %5, i32 %3, i32* %2)
%14 = icmp eq i32* %13, null
br i1 %14, label %22, label %15
15: ; preds = %12
%16 = call i64 @strstr(i8* %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %12, label %18
18: ; preds = %15
%19 = call i8* @strrchr(i8* %5, i8 signext 9)
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = call i64 @strtoul(i8* nocapture %20, i32* null, i32 8)
br label %22
22: ; preds = %12, %18
%23 = phi i64 [ %21, %18 ], [ 0, %12 ]
%24 = call i32 @fclose(i32* %2)
ret i64 %23
}