#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int sscanf(char *, char *, unsigned int *);
int /*<<< orphan*/ stderr;
char *strtok_r(char *, char const *, char **);
int read_fw_revision(unsigned int *fw_revision, int len) {
  int i, fw_info, ret;
  const char *sep = " ";
  char *p, *save;
  char buf[256];
  fw_info =
      open("/sys/devices/ipc/intel_fw_update.0/fw_info/fw_version", O_RDONLY);
  if (fw_info < 0) {
    fw_info = open("/sys/kernel/fw_update/fw_info/fw_version", O_RDONLY);
    if (fw_info < 0) {
      fprintf(stderr, "failed to open %s or %s",
              "/sys/devices/ipc/intel_fw_update.0/fw_info/fw_version",
              "/sys/kernel/fw_update/fw_info/fw_version");
      return fw_info;
    }
  }
  ret = read(fw_info, buf, 256 - 1);
  if (ret < 0) {
    fprintf(stderr, "failed to read fw_revision, ret = %d\n", ret);
    goto err;
  }
  buf[ret] = 0;
  p = strtok_r(buf, sep, &save);
  for (i = 0; p && i < len; i++) {
    ret = sscanf(p, "%x", &fw_revision[i]);
    if (ret != 1) {
      fprintf(stderr, "failed to parse fw_revision, ret = %d\n", ret);
      goto err;
    }
    p = strtok_r(NULL, sep, &save);
  }
  ret = 0;
err:
  close(fw_info);
  return ret;
}


x86 assembly (clang, -O0):
<read_fw_revision>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xe87(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    0xe75(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11f6 <read_fw_revision+0x96>
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe89(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11f1 <read_fw_revision+0x91>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    0xe90(%rip),%rsi
lea    0xe2a(%rip),%rdx
lea    0xe59(%rip),%rcx
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1319 <read_fw_revision+0x1b9>
jmp    11f6 <read_fw_revision+0x96>
mov    -0x1c(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0xff,%edx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1236 <read_fw_revision+0xd6>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xe4f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    130b <read_fw_revision+0x1ab>
movslq -0x20(%rbp),%rax
movb   $0x0,-0x140(%rbp,%rax,1)
lea    -0x140(%rbp),%rdi
mov    -0x28(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1040 <strtok_r@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x18(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x30(%rbp)
mov    %al,-0x141(%rbp)
je     1283 <read_fw_revision+0x123>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
setl   %al
mov    %al,-0x141(%rbp)
mov    -0x141(%rbp),%al
test   $0x1,%al
jne    1296 <read_fw_revision+0x136>
jmp    1304 <read_fw_revision+0x1a4>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rdx
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xdef(%rip),%rsi
call   1070 <sscanf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x20(%rbp)
je     12e1 <read_fw_revision+0x181>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xdcd(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    130b <read_fw_revision+0x1ab>
mov    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x38(%rbp),%rdx
call   1040 <strtok_r@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1261 <read_fw_revision+0x101>
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_fw_revision>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %esi,%r15d
mov    %rdi,%rbx
mov    0x2e6a(%rip),%rbp
mov    0x0(%rbp),%esi
lea    0xe7a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    11ae <read_fw_revision+0x4e>
mov    0x0(%rbp),%esi
lea    0xe9a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
js     1263 <read_fw_revision+0x103>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0xff,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1251 <read_fw_revision+0xf1>
mov    %ebp,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xe28(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rdx
call   1040 <strtok_r@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     12a5 <read_fw_revision+0x145>
test   %r15d,%r15d
jle    12a5 <read_fw_revision+0x145>
mov    %r15d,%r12d
mov    $0x1,%r15d
lea    0x8(%rsp),%r13
nopl   0x0(%rax)
mov    %rax,%rdi
lea    0xe85(%rip),%rsi
mov    %rbx,%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    128a <read_fw_revision+0x12a>
xor    %ebp,%ebp
xor    %edi,%edi
lea    0xdce(%rip),%rsi
mov    %r13,%rdx
call   1040 <strtok_r@plt>
test   %rax,%rax
je     12a5 <read_fw_revision+0x145>
add    $0x4,%rbx
lea    0x1(%r15),%rcx
cmp    %r12,%r15
mov    %rcx,%r15
jb     1210 <read_fw_revision+0xb0>
jmp    12a5 <read_fw_revision+0x145>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
jmp    129c <read_fw_revision+0x13c>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
lea    0xd88(%rip),%rdx
lea    0xdb7(%rip),%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12b0 <read_fw_revision+0x150>
mov    %eax,%ebp
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14d,%edi
call   1030 <close@plt>
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
@.str.1 = [54 x i8] c"/sys/devices/ipc/intel_fw_update.0/fw_info/fw_version\00"
@O_RDONLY = global i32 0
@.str.2 = [41 x i8] c"/sys/kernel/fw_update/fw_info/fw_version\00"
@stderr = global i32 0
@.str.3 = [24 x i8] c"failed to open %s or %s\00"
@.str.4 = [38 x i8] c"failed to read fw_revision, ret = %d\0A\00"
@.str.5 = [3 x i8] c"%x\00"
@.str.6 = [39 x i8] c"failed to parse fw_revision, ret = %d\0A\00"
define i32 @read_fw_revision(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca [256 x i8]
store i32* %0, i32** %4
store i32 %1, i32* %5
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %9
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %27
17: ; preds = %2
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* %7
store i32 %25, i32* %3
br label %77
26: ; preds = %17
br label %27
27: ; preds = %26, %2
%28 = load i32, i32* %7
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 255)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %27
%34 = load i32, i32* @stderr
%35 = load i32, i32* %8
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %35)
br label %73
37: ; preds = %27
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 %39
store i8 0, i8* %40
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%42 = load i8*, i8** %9
%43 = call i8* @strtok_r(i8* %41, i8* %42, i8** %11)
store i8* %43, i8** %10
store i32 0, i32* %6
br label %44
44: ; preds = %69, %37
%45 = load i8*, i8** %10
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %51
47: ; preds = %44
%48 = load i32, i32* %6
%49 = load i32, i32* %5
%50 = icmp slt i32 %48, %49
br label %51
51: ; preds = %47, %44
%52 = phi i1 [ false, %44 ], [ %50, %47 ]
br i1 %52, label %53, label %72
53: ; preds = %51
%54 = load i8*, i8** %10
%55 = load i32*, i32** %4
%56 = load i32, i32* %6
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i32, i32* %55, i64 %57
%59 = call i32 @sscanf(i8* %54, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %58)
store i32 %59, i32* %8
%60 = load i32, i32* %8
%61 = icmp ne i32 %60, 1
br i1 %61, label %62, label %66
62: ; preds = %53
%63 = load i32, i32* @stderr
%64 = load i32, i32* %8
%65 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i32 %64)
br label %73
66: ; preds = %53
%67 = load i8*, i8** %9
%68 = call i8* @strtok_r(i8* null, i8* %67, i8** %11)
store i8* %68, i8** %10
br label %69
69: ; preds = %66
%70 = load i32, i32* %6
%71 = add nsw i32 %70, 1
store i32 %71, i32* %6
br label %44
72: ; preds = %51
store i32 0, i32* %8
br label %73
73: ; preds = %72, %62, %33
%74 = load i32, i32* %7
%75 = call i32 @close(i32 %74)
%76 = load i32, i32* %8
store i32 %76, i32* %3
br label %77
77: ; preds = %73, %22
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
@.str.1 = [54 x i8] c"/sys/devices/ipc/intel_fw_update.0/fw_info/fw_version\00"
@O_RDONLY = global i32 0
@.str.2 = [41 x i8] c"/sys/kernel/fw_update/fw_info/fw_version\00"
@stderr = global i32 0
@.str.3 = [24 x i8] c"failed to open %s or %s\00"
@.str.4 = [38 x i8] c"failed to read fw_revision, ret = %d\0A\00"
@.str.5 = [3 x i8] c"%x\00"
@.str.6 = [39 x i8] c"failed to parse fw_revision, ret = %d\0A\00"
define i32 @read_fw_revision(i32* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca [256 x i8]
%5 = bitcast i8** %3 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %51
17: ; preds = %10, %2
%18 = phi i32 [ %12, %10 ], [ %8, %2 ]
%19 = call i32 @read(i32 %18, i8* %6, i32 255)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %19)
br label %48
24: ; preds = %17
%25 = zext i32 %19 to i64
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %25
store i8 0, i8* %26
%27 = call i8* @strtok_r(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %3)
%28 = icmp ne i8* %27, null
%29 = icmp sgt i32 %1, 0
%30 = and i1 %28, %29
br i1 %30, label %31, label %48
31: ; preds = %24
%32 = zext i32 %1 to i64
br label %33
33: ; preds = %31, %42
%34 = phi i64 [ 0, %31 ], [ %44, %42 ]
%35 = phi i8* [ %27, %31 ], [ %43, %42 ]
%36 = getelementptr inbounds i32, i32* %0, i64 %34
%37 = call i32 @sscanf(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %36)
%38 = icmp eq i32 %37, 1
br i1 %38, label %42, label %39
39: ; preds = %33
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i32 %37)
br label %48
42: ; preds = %33
%43 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %3)
%44 = add nuw nsw i64 %34, 1
%45 = icmp ne i8* %43, null
%46 = icmp ult i64 %44, %32
%47 = select i1 %45, i1 %46, i1 false
br i1 %47, label %33, label %48
48: ; preds = %42, %24, %39, %21
%49 = phi i32 [ %19, %21 ], [ %37, %39 ], [ 0, %24 ], [ 0, %42 ]
%50 = call i32 @close(i32 %18)
br label %51
51: ; preds = %48, %14
%52 = phi i32 [ %12, %14 ], [ %49, %48 ]
ret i32 %52
}