#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*/ snprintf(char *, int, char *, int, ...);
int sg_mode_page_offset(const unsigned char *resp, int resp_len,
                        int mode_sense_6, char *err_buff, int err_buff_len) {
  int bd_len;
  int calc_len;
  int offset;
  if ((NULL == resp) || (resp_len < 4) || ((!mode_sense_6) && (resp_len < 8))) {
    if (err_buff_len > 0)
      snprintf(err_buff, err_buff_len,
               "given response length too "
               "short: %d\n",
               resp_len);
    return -1;
  }
  if (mode_sense_6) {
    calc_len = resp[0] + 1;
    bd_len = resp[3];
    offset = bd_len + 4;
  } else {
    calc_len = (resp[0] << 8) + resp[1] + 2;
    bd_len = (resp[6] << 8) + resp[7];
    offset = bd_len + 8;
  }
  if ((offset + 2) > resp_len) {
    if (err_buff_len > 0)
      snprintf(err_buff, err_buff_len,
               "given response length "
               "too small, offset=%d given_len=%d bd_len=%d\n",
               offset, resp_len, bd_len);
    offset = -1;
  } else if ((offset + 2) > calc_len) {
    if (err_buff_len > 0)
      snprintf(err_buff, err_buff_len,
               "calculated response "
               "length too small, offset=%d calc_len=%d bd_len=%d\n",
               offset, calc_len, bd_len);
    offset = -1;
  }
  return offset;
}


x86 assembly (clang, -O0):
<sg_mode_page_offset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
je     1154 <sg_mode_page_offset+0x44>
cmpl   $0x4,-0x14(%rbp)
jl     1154 <sg_mode_page_offset+0x44>
cmpl   $0x0,-0x18(%rbp)
jne    1182 <sg_mode_page_offset+0x72>
cmpl   $0x8,-0x14(%rbp)
jge    1182 <sg_mode_page_offset+0x72>
cmpl   $0x0,-0x24(%rbp)
jle    1176 <sg_mode_page_offset+0x66>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x14(%rbp),%ecx
lea    0xe91(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127d <sg_mode_page_offset+0x16d>
cmpl   $0x0,-0x18(%rbp)
je     11b2 <sg_mode_page_offset+0xa2>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x3(%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x30(%rbp)
jmp    11ed <sg_mode_page_offset+0xdd>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
shl    $0x8,%eax
mov    -0x10(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
add    %ecx,%eax
add    $0x2,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x6(%rax),%eax
shl    $0x8,%eax
mov    -0x10(%rbp),%rcx
movzbl 0x7(%rcx),%ecx
add    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
add    $0x2,%eax
cmp    -0x14(%rbp),%eax
jle    1232 <sg_mode_page_offset+0x122>
cmpl   $0x0,-0x24(%rbp)
jle    1226 <sg_mode_page_offset+0x116>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x30(%rbp),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x28(%rbp),%r9d
lea    0xe06(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
movl   $0xffffffff,-0x30(%rbp)
jmp    1277 <sg_mode_page_offset+0x167>
mov    -0x30(%rbp),%eax
add    $0x2,%eax
cmp    -0x2c(%rbp),%eax
jle    1272 <sg_mode_page_offset+0x162>
cmpl   $0x0,-0x24(%rbp)
jle    126b <sg_mode_page_offset+0x15b>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x30(%rbp),%ecx
mov    -0x2c(%rbp),%r8d
mov    -0x28(%rbp),%r9d
lea    0xe04(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
movl   $0xffffffff,-0x30(%rbp)
jmp    1277 <sg_mode_page_offset+0x167>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sg_mode_page_offset>:
push   %rbx
mov    %esi,%eax
test   %rdi,%rdi
je     1126 <sg_mode_page_offset+0x16>
cmp    $0x4,%eax
jl     1126 <sg_mode_page_offset+0x16>
cmp    $0x7,%eax
ja     114a <sg_mode_page_offset+0x3a>
test   %edx,%edx
jne    114a <sg_mode_page_offset+0x3a>
mov    $0xffffffff,%ebx
test   %r8d,%r8d
jle    1146 <sg_mode_page_offset+0x36>
lea    0xec9(%rip),%rdx
mov    %rcx,%rdi
mov    %r8d,%esi
mov    %eax,%ecx
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %ebx,%eax
pop    %rbx
ret
movzbl (%rdi),%r10d
test   %edx,%edx
je     1161 <sg_mode_page_offset+0x51>
add    $0x1,%r10d
movzbl 0x3(%rdi),%r9d
lea    0x4(%r9),%r11d
jmp    1180 <sg_mode_page_offset+0x70>
shl    $0x8,%r10d
movzbl 0x1(%rdi),%edx
add    %edx,%r10d
add    $0x2,%r10d
movzwl 0x6(%rdi),%r9d
shl    $0x10,%r9d
bswap  %r9d
lea    0x8(%r9),%r11d
lea    0x2(%r11),%edx
cmp    %eax,%edx
jle    11a7 <sg_mode_page_offset+0x97>
mov    $0xffffffff,%ebx
test   %r8d,%r8d
jle    1146 <sg_mode_page_offset+0x36>
lea    0xe8c(%rip),%rdx
mov    %rcx,%rdi
mov    %r8d,%esi
mov    %r11d,%ecx
mov    %eax,%r8d
jmp    11c9 <sg_mode_page_offset+0xb9>
cmp    %r10d,%edx
jle    11d5 <sg_mode_page_offset+0xc5>
mov    $0xffffffff,%ebx
test   %r8d,%r8d
jle    1146 <sg_mode_page_offset+0x36>
lea    0xeab(%rip),%rdx
mov    %rcx,%rdi
mov    %r8d,%esi
mov    %r11d,%ecx
mov    %r10d,%r8d
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    1146 <sg_mode_page_offset+0x36>
mov    %r11d,%ebx
jmp    1146 <sg_mode_page_offset+0x36>

LLVM IR (-O0):
@.str = [37 x i8] c"given response length too short: %d\0A\00"
@.str.1 = [67 x i8] c"given response length too small, offset=%d given_len=%d bd_len=%d\0A\00"
@.str.2 = [71 x i8] c"calculated response length too small, offset=%d calc_len=%d bd_len=%d\0A\00"
define i32 @sg_mode_page_offset(i8* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%15 = load i8*, i8** %7
%16 = icmp eq i8* null, %15
br i1 %16, label %26, label %17
17: ; preds = %5
%18 = load i32, i32* %8
%19 = icmp slt i32 %18, 4
br i1 %19, label %26, label %20
20: ; preds = %17
%21 = load i32, i32* %9
%22 = icmp ne i32 %21, 0
br i1 %22, label %35, label %23
23: ; preds = %20
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 8
br i1 %25, label %26, label %35
26: ; preds = %23, %17, %5
%27 = load i32, i32* %11
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i8*, i8** %10
%31 = load i32, i32* %11
%32 = load i32, i32* %8
%33 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %30, i32 %31, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %29, %26
store i32 -1, i32* %6
br label %109
35: ; preds = %23, %20
%36 = load i32, i32* %9
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %50
38: ; preds = %35
%39 = load i8*, i8** %7
%40 = getelementptr inbounds i8, i8* %39, i64 0
%41 = load i8, i8* %40
%42 = zext i8 %41 to i32
%43 = add nsw i32 %42, 1
store i32 %43, i32* %13
%44 = load i8*, i8** %7
%45 = getelementptr inbounds i8, i8* %44, i64 3
%46 = load i8, i8* %45
%47 = zext i8 %46 to i32
store i32 %47, i32* %12
%48 = load i32, i32* %12
%49 = add nsw i32 %48, 4
store i32 %49, i32* %14
br label %74
50: ; preds = %35
%51 = load i8*, i8** %7
%52 = getelementptr inbounds i8, i8* %51, i64 0
%53 = load i8, i8* %52
%54 = zext i8 %53 to i32
%55 = shl i32 %54, 8
%56 = load i8*, i8** %7
%57 = getelementptr inbounds i8, i8* %56, i64 1
%58 = load i8, i8* %57
%59 = zext i8 %58 to i32
%60 = add nsw i32 %55, %59
%61 = add nsw i32 %60, 2
store i32 %61, i32* %13
%62 = load i8*, i8** %7
%63 = getelementptr inbounds i8, i8* %62, i64 6
%64 = load i8, i8* %63
%65 = zext i8 %64 to i32
%66 = shl i32 %65, 8
%67 = load i8*, i8** %7
%68 = getelementptr inbounds i8, i8* %67, i64 7
%69 = load i8, i8* %68
%70 = zext i8 %69 to i32
%71 = add nsw i32 %66, %70
store i32 %71, i32* %12
%72 = load i32, i32* %12
%73 = add nsw i32 %72, 8
store i32 %73, i32* %14
br label %74
74: ; preds = %50, %38
%75 = load i32, i32* %14
%76 = add nsw i32 %75, 2
%77 = load i32, i32* %8
%78 = icmp sgt i32 %76, %77
br i1 %78, label %79, label %90
79: ; preds = %74
%80 = load i32, i32* %11
%81 = icmp sgt i32 %80, 0
br i1 %81, label %82, label %89
82: ; preds = %79
%83 = load i8*, i8** %10
%84 = load i32, i32* %11
%85 = load i32, i32* %14
%86 = load i32, i32* %8
%87 = load i32, i32* %12
%88 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %83, i32 %84, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0), i32 %85, i32 %86, i32 %87)
br label %89
89: ; preds = %82, %79
store i32 -1, i32* %14
br label %107
90: ; preds = %74
%91 = load i32, i32* %14
%92 = add nsw i32 %91, 2
%93 = load i32, i32* %13
%94 = icmp sgt i32 %92, %93
br i1 %94, label %95, label %106
95: ; preds = %90
%96 = load i32, i32* %11
%97 = icmp sgt i32 %96, 0
br i1 %97, label %98, label %105
98: ; preds = %95
%99 = load i8*, i8** %10
%100 = load i32, i32* %11
%101 = load i32, i32* %14
%102 = load i32, i32* %13
%103 = load i32, i32* %12
%104 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %99, i32 %100, i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.2, i64 0, i64 0), i32 %101, i32 %102, i32 %103)
br label %105
105: ; preds = %98, %95
store i32 -1, i32* %14
br label %106
106: ; preds = %105, %90
br label %107
107: ; preds = %106, %89
%108 = load i32, i32* %14
store i32 %108, i32* %6
br label %109
109: ; preds = %107, %34
%110 = load i32, i32* %6
ret i32 %110
}

LLVM IR (-O2):
@.str = [37 x i8] c"given response length too short: %d\0A\00"
@.str.1 = [67 x i8] c"given response length too small, offset=%d given_len=%d bd_len=%d\0A\00"
@.str.2 = [71 x i8] c"calculated response length too small, offset=%d calc_len=%d bd_len=%d\0A\00"
define i32 @sg_mode_page_offset(i8* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = icmp eq i8* %0, null
%7 = icmp slt i32 %1, 4
%8 = or i1 %6, %7
br i1 %8, label %13, label %9
9: ; preds = %5
%10 = icmp eq i32 %2, 0
%11 = icmp ult i32 %1, 8
%12 = and i1 %11, %10
br i1 %12, label %13, label %17
13: ; preds = %9, %5
%14 = icmp sgt i32 %4, 0
br i1 %14, label %15, label %58
15: ; preds = %13
%16 = tail call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %3, i32 %4, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %58
17: ; preds = %9
%18 = load i8, i8* %0
%19 = zext i8 %18 to i32
br i1 %10, label %26, label %20
20: ; preds = %17
%21 = add nuw nsw i32 %19, 1
%22 = getelementptr inbounds i8, i8* %0, i64 3
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = add nuw nsw i32 %24, 4
br label %42
26: ; preds = %17
%27 = shl nuw nsw i32 %19, 8
%28 = getelementptr inbounds i8, i8* %0, i64 1
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = or i32 %27, %30
%32 = add nuw nsw i32 %31, 2
%33 = getelementptr inbounds i8, i8* %0, i64 6
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = shl nuw nsw i32 %35, 8
%37 = getelementptr inbounds i8, i8* %0, i64 7
%38 = load i8, i8* %37
%39 = zext i8 %38 to i32
%40 = or i32 %36, %39
%41 = add nuw nsw i32 %40, 8
br label %42
42: ; preds = %26, %20
%43 = phi i32 [ %24, %20 ], [ %40, %26 ]
%44 = phi i32 [ %21, %20 ], [ %32, %26 ]
%45 = phi i32 [ %25, %20 ], [ %41, %26 ]
%46 = add nsw i32 %45, 2
%47 = icmp sgt i32 %46, %1
br i1 %47, label %48, label %52
48: ; preds = %42
%49 = icmp sgt i32 %4, 0
br i1 %49, label %50, label %58
50: ; preds = %48
%51 = tail call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %3, i32 %4, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0), i32 %45, i32 %1, i32 %43)
br label %58
52: ; preds = %42
%53 = icmp sgt i32 %46, %44
br i1 %53, label %54, label %58
54: ; preds = %52
%55 = icmp sgt i32 %4, 0
br i1 %55, label %56, label %58
56: ; preds = %54
%57 = tail call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %3, i32 %4, i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.2, i64 0, i64 0), i32 %45, i32 %44, i32 %43)
br label %58
58: ; preds = %52, %50, %48, %56, %54, %13, %15
%59 = phi i32 [ -1, %15 ], [ -1, %13 ], [ %45, %52 ], [ -1, %50 ], [ -1, %48 ], [ -1, %56 ], [ -1, %54 ]
ret i32 %59
}