#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 */
struct srec_data {
  int dummy;
};

/* Variables and functions */
unsigned int get_hex32(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ process_s3_crc16(struct srec_data *, char *, int, unsigned int);
scalar_t__ process_s3_data(struct srec_data *, char *, int, unsigned int);
scalar_t__ process_s3_fw_info(struct srec_data *, char *, int, unsigned int);
scalar_t__ process_s3_plug(struct srec_data *, char *, int, unsigned int);
int process_s3(struct srec_data *srec, char *data, int len) {
  unsigned int addr;
  if (len < 4) {
    printf("Too short S3 len=%i\n", len);
    return 1;
  }
  if (len - 4 > 128) {
    printf("Too long S3 len=%i\n", len);
    return 1;
  }
  addr = get_hex32(data);
  len -= 4;
  data += 8;
  if (addr & 0xff000000) {
    if (addr == 0xff000000) {
      if (process_s3_plug(srec, data, len, addr))
        return 1;
    } else if (addr == 0xff100000) {
      if (process_s3_crc16(srec, data, len, addr))
        return 1;
    } else if ((addr & 0xffff0000) == 0xff200000) {
      if (process_s3_fw_info(srec, data, len, addr))
        return 1;
    } else {
      printf("Unknown special S3 record: '%s'\n", data);
      return 1;
    }
  } else {
    if (process_s3_data(srec, data, len, addr))
      return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<process_s3>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x4,-0x1c(%rbp)
jge    119a <process_s3+0x3a>
mov    -0x1c(%rbp),%esi
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1313 <process_s3+0x1b3>
mov    -0x1c(%rbp),%eax
sub    $0x4,%eax
cmp    $0x80,%eax
jle    11c8 <process_s3+0x68>
mov    -0x1c(%rbp),%esi
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1313 <process_s3+0x1b3>
mov    -0x18(%rbp),%rdi
call   1050 <get_hex32@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
and    $0xff000000,%eax
cmp    $0x0,%eax
je     12de <process_s3+0x17e>
cmpl   $0xff000000,-0x20(%rbp)
jne    1235 <process_s3+0xd5>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1080 <process_s3_plug@plt>
cmp    $0x0,%rax
je     1230 <process_s3+0xd0>
movl   $0x1,-0x4(%rbp)
jmp    1313 <process_s3+0x1b3>
jmp    12d9 <process_s3+0x179>
cmpl   $0xff100000,-0x20(%rbp)
jne    1270 <process_s3+0x110>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1030 <process_s3_crc16@plt>
cmp    $0x0,%rax
je     126b <process_s3+0x10b>
movl   $0x1,-0x4(%rbp)
jmp    1313 <process_s3+0x1b3>
jmp    12d4 <process_s3+0x174>
mov    -0x20(%rbp),%eax
and    $0xffff0000,%eax
cmp    $0xff200000,%eax
jne    12b1 <process_s3+0x151>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1060 <process_s3_fw_info@plt>
cmp    $0x0,%rax
je     12ac <process_s3+0x14c>
movl   $0x1,-0x4(%rbp)
jmp    1313 <process_s3+0x1b3>
jmp    12cf <process_s3+0x16f>
mov    -0x18(%rbp),%rsi
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1313 <process_s3+0x1b3>
jmp    12d4 <process_s3+0x174>
jmp    12d9 <process_s3+0x179>
jmp    130c <process_s3+0x1ac>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1070 <process_s3_data@plt>
cmp    $0x0,%rax
je     1307 <process_s3+0x1a7>
movl   $0x1,-0x4(%rbp)
jmp    1313 <process_s3+0x1b3>
jmp    130c <process_s3+0x1ac>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_s3>:
push   %r15
push   %r14
push   %rbx
mov    %edx,%ebx
cmp    $0x3,%edx
jg     1175 <process_s3+0x15>
lea    0xe8d(%rip),%rdi
jmp    1184 <process_s3+0x24>
cmp    $0x85,%ebx
jb     1198 <process_s3+0x38>
lea    0xe91(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rsi,%r14
mov    %rdi,%r15
add    $0xfffffffc,%ebx
mov    %rsi,%rdi
call   1050 <get_hex32@plt>
add    $0x8,%r14
cmp    $0x1000000,%eax
jae    11c5 <process_s3+0x65>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
mov    %eax,%ecx
call   1070 <process_s3_data@plt>
jmp    121a <process_s3+0xba>
cmp    $0xff100000,%eax
je     11e7 <process_s3+0x87>
cmp    $0xff000000,%eax
jne    11fb <process_s3+0x9b>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
mov    $0xff000000,%ecx
call   1080 <process_s3_plug@plt>
jmp    121a <process_s3+0xba>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
mov    $0xff100000,%ecx
call   1030 <process_s3_crc16@plt>
jmp    121a <process_s3+0xba>
mov    %eax,%ecx
and    $0xffff0000,%ecx
cmp    $0xff200000,%ecx
jne    1232 <process_s3+0xd2>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
mov    %eax,%ecx
call   1060 <process_s3_fw_info@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    1192 <process_s3+0x32>
xor    %eax,%eax
jmp    1192 <process_s3+0x32>
lea    0xdf0(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    118d <process_s3+0x2d>

LLVM IR (-O0):
%struct.srec_data = type { i32 }
@.str = [21 x i8] c"Too short S3 len=%i\0A\00"
@.str.1 = [20 x i8] c"Too long S3 len=%i\0A\00"
@.str.2 = [33 x i8] c"Unknown special S3 record: '%s'\0A\00"
define i32 @process_s3(%struct.srec_data* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.srec_data*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store %struct.srec_data* %0, %struct.srec_data** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32, i32* %7
%10 = icmp slt i32 %9, 4
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i32, i32* %7
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 1, i32* %4
br label %84
14: ; preds = %3
%15 = load i32, i32* %7
%16 = sub nsw i32 %15, 4
%17 = icmp sgt i32 %16, 128
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* %7
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %19)
store i32 1, i32* %4
br label %84
21: ; preds = %14
%22 = load i8*, i8** %6
%23 = call i32 @get_hex32(i8* %22)
store i32 %23, i32* %8
%24 = load i32, i32* %7
%25 = sub nsw i32 %24, 4
store i32 %25, i32* %7
%26 = load i8*, i8** %6
%27 = getelementptr inbounds i8, i8* %26, i64 8
store i8* %27, i8** %6
%28 = load i32, i32* %8
%29 = and i32 %28, -16777216
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %74
31: ; preds = %21
%32 = load i32, i32* %8
%33 = icmp eq i32 %32, -16777216
br i1 %33, label %34, label %43
34: ; preds = %31
%35 = load %struct.srec_data*, %struct.srec_data** %5
%36 = load i8*, i8** %6
%37 = load i32, i32* %7
%38 = load i32, i32* %8
%39 = call i64 @process_s3_plug(%struct.srec_data* %35, i8* %36, i32 %37, i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %34
store i32 1, i32* %4
br label %84
42: ; preds = %34
br label %73
43: ; preds = %31
%44 = load i32, i32* %8
%45 = icmp eq i32 %44, -15728640
br i1 %45, label %46, label %55
46: ; preds = %43
%47 = load %struct.srec_data*, %struct.srec_data** %5
%48 = load i8*, i8** %6
%49 = load i32, i32* %7
%50 = load i32, i32* %8
%51 = call i64 @process_s3_crc16(%struct.srec_data* %47, i8* %48, i32 %49, i32 %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %46
store i32 1, i32* %4
br label %84
54: ; preds = %46
br label %72
55: ; preds = %43
%56 = load i32, i32* %8
%57 = and i32 %56, -65536
%58 = icmp eq i32 %57, -14680064
br i1 %58, label %59, label %68
59: ; preds = %55
%60 = load %struct.srec_data*, %struct.srec_data** %5
%61 = load i8*, i8** %6
%62 = load i32, i32* %7
%63 = load i32, i32* %8
%64 = call i64 @process_s3_fw_info(%struct.srec_data* %60, i8* %61, i32 %62, i32 %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %59
store i32 1, i32* %4
br label %84
67: ; preds = %59
br label %71
68: ; preds = %55
%69 = load i8*, i8** %6
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %69)
store i32 1, i32* %4
br label %84
71: ; preds = %67
br label %72
72: ; preds = %71, %54
br label %73
73: ; preds = %72, %42
br label %83
74: ; preds = %21
%75 = load %struct.srec_data*, %struct.srec_data** %5
%76 = load i8*, i8** %6
%77 = load i32, i32* %7
%78 = load i32, i32* %8
%79 = call i64 @process_s3_data(%struct.srec_data* %75, i8* %76, i32 %77, i32 %78)
%80 = icmp ne i64 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %74
store i32 1, i32* %4
br label %84
82: ; preds = %74
br label %83
83: ; preds = %82, %73
store i32 0, i32* %4
br label %84
84: ; preds = %83, %81, %68, %66, %53, %41, %18, %11
%85 = load i32, i32* %4
ret i32 %85
}

LLVM IR (-O2):
%struct.srec_data = type { i32 }
@.str = [21 x i8] c"Too short S3 len=%i\0A\00"
@.str.1 = [20 x i8] c"Too long S3 len=%i\0A\00"
@.str.2 = [33 x i8] c"Unknown special S3 record: '%s'\0A\00"
define i32 @process_s3(%struct.srec_data* %0, i8* %1, i32 %2) {
%4 = icmp slt i32 %2, 4
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %2)
br label %35
7: ; preds = %3
%8 = add nsw i32 %2, -4
%9 = icmp ugt i32 %2, 132
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %2)
br label %35
12: ; preds = %7
%13 = tail call i32 @get_hex32(i8* %1)
%14 = getelementptr inbounds i8, i8* %1, i64 8
%15 = icmp ult i32 %13, 16777216
br i1 %15, label %31, label %16
16: ; preds = %12
switch i32 %13, label %23 [
i32 -16777216, label %17
i32 -15728640, label %20
]
17: ; preds = %16
%18 = tail call i64 @process_s3_plug(%struct.srec_data* %0, i8* %14, i32 %8, i32 -16777216)
%19 = icmp eq i64 %18, 0
br i1 %19, label %34, label %35
20: ; preds = %16
%21 = tail call i64 @process_s3_crc16(%struct.srec_data* %0, i8* %14, i32 %8, i32 -15728640)
%22 = icmp eq i64 %21, 0
br i1 %22, label %34, label %35
23: ; preds = %16
%24 = and i32 %13, -65536
%25 = icmp eq i32 %24, -14680064
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = tail call i64 @process_s3_fw_info(%struct.srec_data* %0, i8* %14, i32 %8, i32 %13)
%28 = icmp eq i64 %27, 0
br i1 %28, label %34, label %35
29: ; preds = %23
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %14)
br label %35
31: ; preds = %12
%32 = tail call i64 @process_s3_data(%struct.srec_data* %0, i8* %14, i32 %8, i32 %13)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %31, %17, %26, %20
br label %35
35: ; preds = %31, %26, %20, %17, %34, %29, %10, %5
%36 = phi i32 [ 1, %5 ], [ 1, %10 ], [ 0, %34 ], [ 1, %29 ], [ 1, %17 ], [ 1, %20 ], [ 1, %26 ], [ 1, %31 ]
ret i32 %36
}