#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/kernel/fw_update/fw_info/fw_version", O_RDONLY);
  if (fw_info < 0) {
    fprintf(stderr, "failed to open %s ",
            "/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    0xec1(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    0xe73(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11c8 <read_fw_revision+0x68>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe7a(%rip),%rsi
lea    0xe4a(%rip),%rdx
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12eb <read_fw_revision+0x18b>
mov    -0x1c(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0xff,%edx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1208 <read_fw_revision+0xa8>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xe40(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12dd <read_fw_revision+0x17d>
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     1255 <read_fw_revision+0xf5>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
setl   %al
mov    %al,-0x141(%rbp)
mov    -0x141(%rbp),%al
test   $0x1,%al
jne    1268 <read_fw_revision+0x108>
jmp    12d6 <read_fw_revision+0x176>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rdx
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xde0(%rip),%rsi
call   1070 <sscanf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x20(%rbp)
je     12b3 <read_fw_revision+0x153>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xdbe(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12dd <read_fw_revision+0x17d>
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    1233 <read_fw_revision+0xd3>
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),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
js     1241 <read_fw_revision+0xe1>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0xff,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1261 <read_fw_revision+0x101>
mov    %ebp,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xe79(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rdx
call   1040 <strtok_r@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     128e <read_fw_revision+0x12e>
test   %r15d,%r15d
jle    128e <read_fw_revision+0x12e>
mov    %r15d,%r12d
mov    $0x1,%r15d
lea    0x8(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rax,%rdi
lea    0xe58(%rip),%rsi
mov    %rbx,%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    1273 <read_fw_revision+0x113>
xor    %ebp,%ebp
xor    %edi,%edi
lea    0xe18(%rip),%rsi
mov    %r13,%rdx
call   1040 <strtok_r@plt>
test   %rax,%rax
je     128e <read_fw_revision+0x12e>
add    $0x4,%rbx
lea    0x1(%r15),%rcx
cmp    %r12,%r15
mov    %rcx,%r15
jb     1200 <read_fw_revision+0xa0>
jmp    128e <read_fw_revision+0x12e>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
lea    0xda8(%rip),%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1299 <read_fw_revision+0x139>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
jmp    1285 <read_fw_revision+0x125>
mov    %eax,%ebp
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%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 = [41 x i8] c"/sys/kernel/fw_update/fw_info/fw_version\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.2 = [19 x i8] c"failed to open %s \00"
@.str.3 = [38 x i8] c"failed to read fw_revision, ret = %d\0A\00"
@.str.4 = [3 x i8] c"%x\00"
@.str.5 = [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 ([41 x i8], [41 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 %21
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* %7
store i32 %20, i32* %3
br label %71
21: ; preds = %2
%22 = load i32, i32* %7
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 255)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = load i32, i32* %8
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %29)
br label %67
31: ; preds = %21
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 %33
store i8 0, i8* %34
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%36 = load i8*, i8** %9
%37 = call i8* @strtok_r(i8* %35, i8* %36, i8** %11)
store i8* %37, i8** %10
store i32 0, i32* %6
br label %38
38: ; preds = %63, %31
%39 = load i8*, i8** %10
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %45
41: ; preds = %38
%42 = load i32, i32* %6
%43 = load i32, i32* %5
%44 = icmp slt i32 %42, %43
br label %45
45: ; preds = %41, %38
%46 = phi i1 [ false, %38 ], [ %44, %41 ]
br i1 %46, label %47, label %66
47: ; preds = %45
%48 = load i8*, i8** %10
%49 = load i32*, i32** %4
%50 = load i32, i32* %6
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %49, i64 %51
%53 = call i32 @sscanf(i8* %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %52)
store i32 %53, i32* %8
%54 = load i32, i32* %8
%55 = icmp ne i32 %54, 1
br i1 %55, label %56, label %60
56: ; preds = %47
%57 = load i32, i32* @stderr
%58 = load i32, i32* %8
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 %58)
br label %67
60: ; preds = %47
%61 = load i8*, i8** %9
%62 = call i8* @strtok_r(i8* null, i8* %61, i8** %11)
store i8* %62, i8** %10
br label %63
63: ; preds = %60
%64 = load i32, i32* %6
%65 = add nsw i32 %64, 1
store i32 %65, i32* %6
br label %38
66: ; preds = %45
store i32 0, i32* %8
br label %67
67: ; preds = %66, %56, %27
%68 = load i32, i32* %7
%69 = call i32 @close(i32 %68)
%70 = load i32, i32* %8
store i32 %70, i32* %3
br label %71
71: ; preds = %67, %17
%72 = load i32, i32* %3
ret i32 %72
}

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