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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int crack_update_fw_pti_field(const char *fw_file, uint8_t *pti_field) {
  FILE *fd;
  int tmp = 0;
  int location;
  uint8_t smip_data;
  if ((fd = fopen(fw_file, "rb")) == NULL) {
    fprintf(stderr, "fopen error: Unable to open file\n");
    return -1;
  }
  *pti_field = 0x00000000;
  while (tmp != 0x50494D53) {
    int cur;
    fread(&tmp, sizeof(int), 1, fd);
    if (ferror(fd) || feof(fd)) {
      fprintf(stderr, "find SMIP_PATTERN failed\n");
      fclose(fd);
      return -1;
    }
    cur = ftell(fd) - sizeof(int);
    fseek(fd, cur + sizeof(char), SEEK_SET);
    if (ferror(fd) || feof(fd)) {
      fprintf(stderr, "reposition to SMIP_PATTERN failed\n");
      fclose(fd);
      return -1;
    }
  }
  location = ftell(fd) - sizeof(char);
  fseek(fd, location + 0x030C, SEEK_SET);
  if (ferror(fd) || feof(fd)) {
    fprintf(stderr, "reposition to PTI field offset failed\n");
    fclose(fd);
    return -1;
  }
  fread(&smip_data, 1, 1, fd);
  if (ferror(fd) || feof(fd)) {
    fprintf(stderr, "read of FIP_header failed\n");
    fclose(fd);
    return -1;
  }
  *pti_field = smip_data;
  fclose(fd);
  return 0;
}


x86 assembly (clang, -O0):
<crack_update_fw_pti_field>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe5e(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11d6 <crack_update_fw_pti_field+0x56>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d1 <crack_update_fw_pti_field+0x251>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
cmpl   $0x50494d53,-0x24(%rbp)
je     12d7 <crack_update_fw_pti_field+0x157>
mov    -0x20(%rbp),%rcx
lea    -0x24(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
jne    122a <crack_update_fw_pti_field+0xaa>
mov    -0x20(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%rax
je     1254 <crack_update_fw_pti_field+0xd4>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d1 <crack_update_fw_pti_field+0x251>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
cltq
sub    $0x4,%rax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
movslq -0x30(%rbp),%rax
add    $0x1,%rax
mov    %eax,%esi
mov    0x2d5d(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
jne    12a8 <crack_update_fw_pti_field+0x128>
mov    -0x20(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%rax
je     12d2 <crack_update_fw_pti_field+0x152>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
lea    0xd87(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d1 <crack_update_fw_pti_field+0x251>
jmp    11e0 <crack_update_fw_pti_field+0x60>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
cltq
sub    $0x1,%rax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%esi
add    $0x30c,%esi
mov    0x2cdb(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
jne    132a <crack_update_fw_pti_field+0x1aa>
mov    -0x20(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%rax
je     1354 <crack_update_fw_pti_field+0x1d4>
mov    0x2caf(%rip),%rax
mov    (%rax),%edi
lea    0xd28(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d1 <crack_update_fw_pti_field+0x251>
mov    -0x20(%rbp),%rcx
lea    -0x2c(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
jne    138e <crack_update_fw_pti_field+0x20e>
mov    -0x20(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%rax
je     13b8 <crack_update_fw_pti_field+0x238>
mov    0x2c4b(%rip),%rax
mov    (%rax),%edi
lea    0xceb(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d1 <crack_update_fw_pti_field+0x251>
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crack_update_fw_pti_field>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
movl   $0x0,(%rsp)
lea    0xe67(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     127b <crack_update_fw_pti_field+0xfb>
mov    %rax,%rbx
movl   $0x0,(%r14)
mov    %rsp,%r15
mov    0x2e1d(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    1292 <crack_update_fw_pti_field+0x112>
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %rax,%rax
jne    1292 <crack_update_fw_pti_field+0x112>
mov    %rbx,%rdi
call   1070 <ftell@plt>
lea    -0x3(%rax),%esi
mov    (%r12),%edx
mov    %rbx,%rdi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    12a4 <crack_update_fw_pti_field+0x124>
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %rax,%rax
jne    12a4 <crack_update_fw_pti_field+0x124>
cmpl   $0x50494d53,(%rsp)
jne    11c0 <crack_update_fw_pti_field+0x40>
mov    %rbx,%rdi
call   1070 <ftell@plt>
lea    0x30b(%rax),%esi
mov    (%r12),%edx
mov    %rbx,%rdi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    1269 <crack_update_fw_pti_field+0xe9>
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %rax,%rax
je     12d2 <crack_update_fw_pti_field+0x152>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
jmp    12b4 <crack_update_fw_pti_field+0x134>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
lea    0xd78(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12c1 <crack_update_fw_pti_field+0x141>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
lea    0xd83(%rip),%rsi
jmp    12b4 <crack_update_fw_pti_field+0x134>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
lea    0xd8b(%rip),%rsi
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0x4(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    1303 <crack_update_fw_pti_field+0x183>
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %rax,%rax
je     1315 <crack_update_fw_pti_field+0x195>
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
lea    0xd76(%rip),%rsi
jmp    12b4 <crack_update_fw_pti_field+0x134>
mov    0x4(%rsp),%eax
mov    %eax,(%r14)
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    12c6 <crack_update_fw_pti_field+0x146>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [34 x i8] c"fopen error: Unable to open file\0A\00"
@.str.2 = [26 x i8] c"find SMIP_PATTERN failed\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [35 x i8] c"reposition to SMIP_PATTERN failed\0A\00"
@.str.4 = [39 x i8] c"reposition to PTI field offset failed\0A\00"
@.str.5 = [27 x i8] c"read of FIP_header failed\0A\00"
define i32 @crack_update_fw_pti_field(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 0, i32* %7
%11 = load i8*, i8** %4
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %106
17: ; preds = %2
%18 = load i32*, i32** %5
store i32 0, i32* %18
br label %19
19: ; preds = %62, %17
%20 = load i32, i32* %7
%21 = icmp ne i32 %20, 1346981203
br i1 %21, label %22, label %63
22: ; preds = %19
%23 = load i32*, i32** %6
%24 = call i32 @fread(i32* %7, i32 4, i32 1, i32* %23)
%25 = load i32*, i32** %6
%26 = call i64 @ferror(i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %22
%29 = load i32*, i32** %6
%30 = call i64 @feof(i32* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %28, %22
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32*, i32** %6
%36 = call i32 @fclose(i32* %35)
store i32 -1, i32* %3
br label %106
37: ; preds = %28
%38 = load i32*, i32** %6
%39 = call i32 @ftell(i32* %38)
%40 = sext i32 %39 to i64
%41 = sub i64 %40, 4
%42 = trunc i64 %41 to i32
store i32 %42, i32* %10
%43 = load i32*, i32** %6
%44 = load i32, i32* %10
%45 = sext i32 %44 to i64
%46 = add i64 %45, 1
%47 = trunc i64 %46 to i32
%48 = load i32, i32* @SEEK_SET
%49 = call i32 @fseek(i32* %43, i32 %47, i32 %48)
%50 = load i32*, i32** %6
%51 = call i64 @ferror(i32* %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %57, label %53
53: ; preds = %37
%54 = load i32*, i32** %6
%55 = call i64 @feof(i32* %54)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %62
57: ; preds = %53, %37
%58 = load i32, i32* @stderr
%59 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%60 = load i32*, i32** %6
%61 = call i32 @fclose(i32* %60)
store i32 -1, i32* %3
br label %106
62: ; preds = %53
br label %19
63: ; preds = %19
%64 = load i32*, i32** %6
%65 = call i32 @ftell(i32* %64)
%66 = sext i32 %65 to i64
%67 = sub i64 %66, 1
%68 = trunc i64 %67 to i32
store i32 %68, i32* %8
%69 = load i32*, i32** %6
%70 = load i32, i32* %8
%71 = add nsw i32 %70, 780
%72 = load i32, i32* @SEEK_SET
%73 = call i32 @fseek(i32* %69, i32 %71, i32 %72)
%74 = load i32*, i32** %6
%75 = call i64 @ferror(i32* %74)
%76 = icmp ne i64 %75, 0
br i1 %76, label %81, label %77
77: ; preds = %63
%78 = load i32*, i32** %6
%79 = call i64 @feof(i32* %78)
%80 = icmp ne i64 %79, 0
br i1 %80, label %81, label %86
81: ; preds = %77, %63
%82 = load i32, i32* @stderr
%83 = call i32 @fprintf(i32 %82, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
%84 = load i32*, i32** %6
%85 = call i32 @fclose(i32* %84)
store i32 -1, i32* %3
br label %106
86: ; preds = %77
%87 = load i32*, i32** %6
%88 = call i32 @fread(i32* %9, i32 1, i32 1, i32* %87)
%89 = load i32*, i32** %6
%90 = call i64 @ferror(i32* %89)
%91 = icmp ne i64 %90, 0
br i1 %91, label %96, label %92
92: ; preds = %86
%93 = load i32*, i32** %6
%94 = call i64 @feof(i32* %93)
%95 = icmp ne i64 %94, 0
br i1 %95, label %96, label %101
96: ; preds = %92, %86
%97 = load i32, i32* @stderr
%98 = call i32 @fprintf(i32 %97, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%99 = load i32*, i32** %6
%100 = call i32 @fclose(i32* %99)
store i32 -1, i32* %3
br label %106
101: ; preds = %92
%102 = load i32, i32* %9
%103 = load i32*, i32** %5
store i32 %102, i32* %103
%104 = load i32*, i32** %6
%105 = call i32 @fclose(i32* %104)
store i32 0, i32* %3
br label %106
106: ; preds = %101, %96, %81, %57, %32, %14
%107 = load i32, i32* %3
ret i32 %107
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [34 x i8] c"fopen error: Unable to open file\0A\00"
@.str.2 = [26 x i8] c"find SMIP_PATTERN failed\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [35 x i8] c"reposition to SMIP_PATTERN failed\0A\00"
@.str.4 = [39 x i8] c"reposition to PTI field offset failed\0A\00"
@.str.5 = [27 x i8] c"read of FIP_header failed\0A\00"
define i32 @crack_update_fw_pti_field(i8* nocapture %0, i32* nocapture writeonly %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%6 = bitcast i32* %4 to i8*
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %69
12: ; preds = %2
store i32 0, i32* %1
br label %13
13: ; preds = %12, %34
%14 = call i32 @fread(i32* %3, i32 4, i32 1, i32* %7)
%15 = tail call i64 @ferror(i32* %7)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = tail call i64 @feof(i32* %7)
%19 = icmp eq i64 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %17, %13
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 @fclose(i32* %7)
br label %69
24: ; preds = %17
%25 = tail call i32 @ftell(i32* %7)
%26 = add i32 %25, -3
%27 = load i32, i32* @SEEK_SET
%28 = tail call i32 @fseek(i32* %7, i32 %26, i32 %27)
%29 = tail call i64 @ferror(i32* %7)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %24
%32 = tail call i64 @feof(i32* %7)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* %3
%36 = icmp eq i32 %35, 1346981203
br i1 %36, label %41, label %13
37: ; preds = %31, %24
%38 = load i32, i32* @stderr
%39 = tail call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%40 = tail call i32 @fclose(i32* %7)
br label %69
41: ; preds = %34
%42 = tail call i32 @ftell(i32* %7)
%43 = add i32 %42, 779
%44 = load i32, i32* @SEEK_SET
%45 = tail call i32 @fseek(i32* %7, i32 %43, i32 %44)
%46 = tail call i64 @ferror(i32* %7)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %41
%49 = tail call i64 @feof(i32* %7)
%50 = icmp eq i64 %49, 0
br i1 %50, label %55, label %51
51: ; preds = %48, %41
%52 = load i32, i32* @stderr
%53 = tail call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
%54 = tail call i32 @fclose(i32* %7)
br label %69
55: ; preds = %48
%56 = call i32 @fread(i32* %4, i32 1, i32 1, i32* %7)
%57 = tail call i64 @ferror(i32* %7)
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %55
%60 = tail call i64 @feof(i32* %7)
%61 = icmp eq i64 %60, 0
br i1 %61, label %66, label %62
62: ; preds = %59, %55
%63 = load i32, i32* @stderr
%64 = tail call i32 @fprintf(i32 %63, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%65 = tail call i32 @fclose(i32* %7)
br label %69
66: ; preds = %59
%67 = load i32, i32* %4
store i32 %67, i32* %1
%68 = tail call i32 @fclose(i32* %7)
br label %69
69: ; preds = %20, %37, %66, %62, %51, %9
%70 = phi i32 [ -1, %9 ], [ -1, %51 ], [ -1, %62 ], [ 0, %66 ], [ -1, %37 ], [ -1, %20 ]
ret i32 %70
}