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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ strstr(char *, char *);
int mrom_is_second_boot(void) {
  int i;
  int res = 0;
  FILE *f = NULL;
  char buff[2048];
  static const char *kmsg_paths[] = {
      "/proc/last_kmsg",
      "/sys/fs/pstore/console-ramoops",
      NULL,
  };
  f = fopen("/proc/cmdline", "re");
  if (f) {
    if (fgets(buff, sizeof(buff), f) && strstr(buff, "mrom_kexecd=1")) {
      res = 1;
      goto exit;
    }
    fclose(f);
    f = NULL;
  }
  for (i = 0; !f && kmsg_paths[i]; ++i)
    f = fopen(kmsg_paths[i], "re");
  if (!f)
    return 0;
  while (fgets(buff, sizeof(buff), f)) {
    if (strstr(buff, "MultiromSaysNextBootShouldBeSecondMagic108\n")) {
      res = 1;
      goto exit;
    }
  }
exit:
  fclose(f);
  return res;
}


x86 assembly (clang, -O0):
<mrom_is_second_boot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
movl   $0x0,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xece(%rip),%rdi
lea    0xed5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d5 <mrom_is_second_boot+0x95>
lea    -0x820(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x800,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11c4 <mrom_is_second_boot+0x84>
lea    -0x820(%rbp),%rdi
lea    0xe97(%rip),%rsi
call   1060 <strstr@plt>
cmp    $0x0,%rax
je     11c4 <mrom_is_second_boot+0x84>
movl   $0x1,-0xc(%rbp)
jmp    12b6 <mrom_is_second_boot+0x176>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x8(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x821(%rbp)
jne    1208 <mrom_is_second_boot+0xc8>
movslq -0x8(%rbp),%rcx
lea    0x2e56(%rip),%rax
cmpq   $0x0,(%rax,%rcx,8)
setne  %al
mov    %al,-0x821(%rbp)
mov    -0x821(%rbp),%al
test   $0x1,%al
jne    121b <mrom_is_second_boot+0xdb>
jmp    1248 <mrom_is_second_boot+0x108>
movslq -0x8(%rbp),%rcx
lea    0x2e2a(%rip),%rax
mov    (%rax,%rcx,8),%rdi
lea    0xe0c(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11dc <mrom_is_second_boot+0x9c>
cmpq   $0x0,-0x18(%rbp)
jne    125f <mrom_is_second_boot+0x11f>
movl   $0x0,-0x4(%rbp)
jmp    12c5 <mrom_is_second_boot+0x185>
jmp    1264 <mrom_is_second_boot+0x124>
lea    -0x820(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x800,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     12b1 <mrom_is_second_boot+0x171>
lea    -0x820(%rbp),%rdi
lea    0xdbd(%rip),%rsi
call   1060 <strstr@plt>
cmp    $0x0,%rax
je     12ac <mrom_is_second_boot+0x16c>
movl   $0x1,-0xc(%rbp)
jmp    12b6 <mrom_is_second_boot+0x176>
jmp    1264 <mrom_is_second_boot+0x124>
jmp    12b6 <mrom_is_second_boot+0x176>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mrom_is_second_boot>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x808,%rsp
lea    0xedb(%rip),%rdi
lea    0xee2(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11a2 <mrom_is_second_boot+0x62>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x800,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     119a <mrom_is_second_boot+0x5a>
lea    0xebc(%rip),%rsi
mov    %rsp,%rdi
call   1060 <strstr@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    1232 <mrom_is_second_boot+0xf2>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %ebp,%ebp
lea    0x2c55(%rip),%r15
lea    0xe8b(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x0(%rbp,%r15,1),%rdi
mov    %r14,%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11e2 <mrom_is_second_boot+0xa2>
lea    0x8(%rbp),%rax
cmp    $0x8,%rbp
mov    %rax,%rbp
jne    11c0 <mrom_is_second_boot+0x80>
test   %rbx,%rbx
je     122c <mrom_is_second_boot+0xec>
mov    %rsp,%rbp
lea    0xe5d(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x800,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1230 <mrom_is_second_boot+0xf0>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     1200 <mrom_is_second_boot+0xc0>
mov    $0x1,%ebp
jmp    1232 <mrom_is_second_boot+0xf2>
xor    %ebp,%ebp
jmp    123a <mrom_is_second_boot+0xfa>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@mrom_is_second_boot.kmsg_paths = internal global [3 x i8*] [i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str = [16 x i8] c"/proc/last_kmsg\00"
@.str.1 = [31 x i8] c"/sys/fs/pstore/console-ramoops\00"
@.str.2 = [14 x i8] c"/proc/cmdline\00"
@.str.3 = [3 x i8] c"re\00"
@.str.4 = [14 x i8] c"mrom_kexecd=1\00"
@.str.5 = [44 x i8] c"MultiromSaysNextBootShouldBeSecondMagic108\0A\00"
define i32 @mrom_is_second_boot() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [2048 x i8]
store i32 0, i32* %3
store i32* null, i32** %4
%6 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %6, i32** %4
%7 = load i32*, i32** %4
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %22
9: ; preds = %0
%10 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%11 = load i32*, i32** %4
%12 = call i64 @fgets(i8* %10, i32 2048, i32* %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %9
%15 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%16 = call i64 @strstr(i8* %15, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 1, i32* %3
br label %60
19: ; preds = %14, %9
%20 = load i32*, i32** %4
%21 = call i32 @fclose(i32* %20)
store i32* null, i32** %4
br label %22
22: ; preds = %19, %0
store i32 0, i32* %2
br label %23
23: ; preds = %40, %22
%24 = load i32*, i32** %4
%25 = icmp ne i32* %24, null
br i1 %25, label %32, label %26
26: ; preds = %23
%27 = load i32, i32* %2
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [3 x i8*], [3 x i8*]* @mrom_is_second_boot.kmsg_paths, i64 0, i64 %28
%30 = load i8*, i8** %29
%31 = icmp ne i8* %30, null
br label %32
32: ; preds = %26, %23
%33 = phi i1 [ false, %23 ], [ %31, %26 ]
br i1 %33, label %34, label %43
34: ; preds = %32
%35 = load i32, i32* %2
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [3 x i8*], [3 x i8*]* @mrom_is_second_boot.kmsg_paths, i64 0, i64 %36
%38 = load i8*, i8** %37
%39 = call i32* @fopen(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %39, i32** %4
br label %40
40: ; preds = %34
%41 = load i32, i32* %2
%42 = add nsw i32 %41, 1
store i32 %42, i32* %2
br label %23
43: ; preds = %32
%44 = load i32*, i32** %4
%45 = icmp ne i32* %44, null
br i1 %45, label %47, label %46
46: ; preds = %43
store i32 0, i32* %1
br label %64
47: ; preds = %43
br label %48
48: ; preds = %58, %47
%49 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%50 = load i32*, i32** %4
%51 = call i64 @fgets(i8* %49, i32 2048, i32* %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %59
53: ; preds = %48
%54 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%55 = call i64 @strstr(i8* %54, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %53
store i32 1, i32* %3
br label %60
58: ; preds = %53
br label %48
59: ; preds = %48
br label %60
60: ; preds = %59, %57, %18
%61 = load i32*, i32** %4
%62 = call i32 @fclose(i32* %61)
%63 = load i32, i32* %3
store i32 %63, i32* %1
br label %64
64: ; preds = %60, %46
%65 = load i32, i32* %1
ret i32 %65
}

LLVM IR (-O2):
@mrom_is_second_boot.kmsg_paths = internal unnamed_addr constant [3 x i8*] [i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str = [16 x i8] c"/proc/last_kmsg\00"
@.str.1 = [31 x i8] c"/sys/fs/pstore/console-ramoops\00"
@.str.2 = [14 x i8] c"/proc/cmdline\00"
@.str.3 = [3 x i8] c"re\00"
@.str.4 = [14 x i8] c"mrom_kexecd=1\00"
@.str.5 = [44 x i8] c"MultiromSaysNextBootShouldBeSecondMagic108\0A\00"
define i32 @mrom_is_second_boot() {
%1 = alloca [2048 x i8]
%2 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %13, label %5
5: ; preds = %0
%6 = call i64 @fgets(i8* %2, i32 2048, i32* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %5
%9 = call i64 @strstr(i8* %2, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %31
11: ; preds = %8, %5
%12 = call i32 @fclose(i32* %3)
br label %13
13: ; preds = %11, %0
br label %14
14: ; preds = %13, %14
%15 = phi i64 [ %19, %14 ], [ 0, %13 ]
%16 = getelementptr inbounds [3 x i8*], [3 x i8*]* @mrom_is_second_boot.kmsg_paths, i64 0, i64 %15
%17 = load i8*, i8** %16
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%19 = add nuw nsw i64 %15, 1
%20 = icmp eq i32* %18, null
%21 = xor i1 %20, true
%22 = icmp eq i64 %19, 2
%23 = select i1 %21, i1 true, i1 %22
br i1 %23, label %24, label %14
24: ; preds = %14
br i1 %20, label %35, label %25
25: ; preds = %24, %28
%26 = call i64 @fgets(i8* %2, i32 2048, i32* %18)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = call i64 @strstr(i8* %2, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %25, label %31
31: ; preds = %28, %25, %8
%32 = phi i32 [ 1, %8 ], [ 1, %28 ], [ 0, %25 ]
%33 = phi i32* [ %3, %8 ], [ %18, %25 ], [ %18, %28 ]
%34 = call i32 @fclose(i32* %33)
br label %35
35: ; preds = %24, %31
%36 = phi i32 [ %32, %31 ], [ 0, %24 ]
ret i32 %36
}