#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))) {
    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) {
    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) {
    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    1178 <sg_mode_page_offset+0x68>
cmpl   $0x8,-0x14(%rbp)
jge    1178 <sg_mode_page_offset+0x68>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x14(%rbp),%ecx
lea    0xe9b(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125f <sg_mode_page_offset+0x14f>
cmpl   $0x0,-0x18(%rbp)
je     11a8 <sg_mode_page_offset+0x98>
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    11e3 <sg_mode_page_offset+0xd3>
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    121e <sg_mode_page_offset+0x10e>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x30(%rbp),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x28(%rbp),%r9d
lea    0xe1a(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
movl   $0xffffffff,-0x30(%rbp)
jmp    1259 <sg_mode_page_offset+0x149>
mov    -0x30(%rbp),%eax
add    $0x2,%eax
cmp    -0x2c(%rbp),%eax
jle    1254 <sg_mode_page_offset+0x144>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x30(%rbp),%ecx
mov    -0x2c(%rbp),%r8d
mov    -0x28(%rbp),%r9d
lea    0xe22(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
movl   $0xffffffff,-0x30(%rbp)
jmp    1259 <sg_mode_page_offset+0x149>
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   %rax
mov    %esi,%r10d
test   %rdi,%rdi
je     1129 <sg_mode_page_offset+0x19>
cmp    $0x4,%r10d
jl     1129 <sg_mode_page_offset+0x19>
cmp    $0x7,%r10d
ja     1142 <sg_mode_page_offset+0x32>
test   %edx,%edx
jne    1142 <sg_mode_page_offset+0x32>
lea    0xed0(%rip),%rdx
mov    %rcx,%rdi
mov    %r8d,%esi
mov    %r10d,%ecx
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    11b2 <sg_mode_page_offset+0xa2>
movzbl (%rdi),%r11d
test   %edx,%edx
je     1159 <sg_mode_page_offset+0x49>
add    $0x1,%r11d
movzbl 0x3(%rdi),%r9d
lea    0x4(%r9),%eax
jmp    1178 <sg_mode_page_offset+0x68>
shl    $0x8,%r11d
movzbl 0x1(%rdi),%eax
add    %eax,%r11d
add    $0x2,%r11d
movzwl 0x6(%rdi),%r9d
shl    $0x10,%r9d
bswap  %r9d
lea    0x8(%r9),%eax
lea    0x2(%rax),%edx
cmp    %r10d,%edx
jle    1194 <sg_mode_page_offset+0x84>
lea    0xe9e(%rip),%rdx
mov    %rcx,%rdi
mov    %r8d,%esi
mov    %eax,%ecx
mov    %r10d,%r8d
jmp    11ab <sg_mode_page_offset+0x9b>
cmp    %r11d,%edx
jle    11b7 <sg_mode_page_offset+0xa7>
lea    0xec8(%rip),%rdx
mov    %rcx,%rdi
mov    %r8d,%esi
mov    %eax,%ecx
mov    %r11d,%r8d
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret

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 %31, label %23
23: ; preds = %20
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 8
br i1 %25, label %26, label %31
26: ; preds = %23, %17, %5
%27 = load i8*, i8** %10
%28 = load i32, i32* %11
%29 = load i32, i32* %8
%30 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %27, i32 %28, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %29)
store i32 -1, i32* %6
br label %97
31: ; preds = %23, %20
%32 = load i32, i32* %9
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %46
34: ; preds = %31
%35 = load i8*, i8** %7
%36 = getelementptr inbounds i8, i8* %35, i64 0
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = add nsw i32 %38, 1
store i32 %39, i32* %13
%40 = load i8*, i8** %7
%41 = getelementptr inbounds i8, i8* %40, i64 3
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
store i32 %43, i32* %12
%44 = load i32, i32* %12
%45 = add nsw i32 %44, 4
store i32 %45, i32* %14
br label %70
46: ; preds = %31
%47 = load i8*, i8** %7
%48 = getelementptr inbounds i8, i8* %47, i64 0
%49 = load i8, i8* %48
%50 = zext i8 %49 to i32
%51 = shl i32 %50, 8
%52 = load i8*, i8** %7
%53 = getelementptr inbounds i8, i8* %52, i64 1
%54 = load i8, i8* %53
%55 = zext i8 %54 to i32
%56 = add nsw i32 %51, %55
%57 = add nsw i32 %56, 2
store i32 %57, i32* %13
%58 = load i8*, i8** %7
%59 = getelementptr inbounds i8, i8* %58, i64 6
%60 = load i8, i8* %59
%61 = zext i8 %60 to i32
%62 = shl i32 %61, 8
%63 = load i8*, i8** %7
%64 = getelementptr inbounds i8, i8* %63, i64 7
%65 = load i8, i8* %64
%66 = zext i8 %65 to i32
%67 = add nsw i32 %62, %66
store i32 %67, i32* %12
%68 = load i32, i32* %12
%69 = add nsw i32 %68, 8
store i32 %69, i32* %14
br label %70
70: ; preds = %46, %34
%71 = load i32, i32* %14
%72 = add nsw i32 %71, 2
%73 = load i32, i32* %8
%74 = icmp sgt i32 %72, %73
br i1 %74, label %75, label %82
75: ; preds = %70
%76 = load i8*, i8** %10
%77 = load i32, i32* %11
%78 = load i32, i32* %14
%79 = load i32, i32* %8
%80 = load i32, i32* %12
%81 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %76, i32 %77, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0), i32 %78, i32 %79, i32 %80)
store i32 -1, i32* %14
br label %95
82: ; preds = %70
%83 = load i32, i32* %14
%84 = add nsw i32 %83, 2
%85 = load i32, i32* %13
%86 = icmp sgt i32 %84, %85
br i1 %86, label %87, label %94
87: ; preds = %82
%88 = load i8*, i8** %10
%89 = load i32, i32* %11
%90 = load i32, i32* %14
%91 = load i32, i32* %13
%92 = load i32, i32* %12
%93 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %88, i32 %89, i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.2, i64 0, i64 0), i32 %90, i32 %91, i32 %92)
store i32 -1, i32* %14
br label %94
94: ; preds = %87, %82
br label %95
95: ; preds = %94, %75
%96 = load i32, i32* %14
store i32 %96, i32* %6
br label %97
97: ; preds = %95, %26
%98 = load i32, i32* %6
ret i32 %98
}

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 %15
13: ; preds = %9, %5
%14 = 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 %52
15: ; preds = %9
%16 = load i8, i8* %0
%17 = zext i8 %16 to i32
br i1 %10, label %24, label %18
18: ; preds = %15
%19 = add nuw nsw i32 %17, 1
%20 = getelementptr inbounds i8, i8* %0, i64 3
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = add nuw nsw i32 %22, 4
br label %40
24: ; preds = %15
%25 = shl nuw nsw i32 %17, 8
%26 = getelementptr inbounds i8, i8* %0, i64 1
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = or i32 %25, %28
%30 = add nuw nsw i32 %29, 2
%31 = getelementptr inbounds i8, i8* %0, i64 6
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = shl nuw nsw i32 %33, 8
%35 = getelementptr inbounds i8, i8* %0, i64 7
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = or i32 %34, %37
%39 = add nuw nsw i32 %38, 8
br label %40
40: ; preds = %24, %18
%41 = phi i32 [ %22, %18 ], [ %38, %24 ]
%42 = phi i32 [ %19, %18 ], [ %30, %24 ]
%43 = phi i32 [ %23, %18 ], [ %39, %24 ]
%44 = add nsw i32 %43, 2
%45 = icmp sgt i32 %44, %1
br i1 %45, label %46, label %48
46: ; preds = %40
%47 = 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 %43, i32 %1, i32 %41)
br label %52
48: ; preds = %40
%49 = icmp sgt i32 %44, %42
br i1 %49, label %50, label %52
50: ; preds = %48
%51 = 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 %43, i32 %42, i32 %41)
br label %52
52: ; preds = %46, %50, %48, %13
%53 = phi i32 [ -1, %13 ], [ -1, %46 ], [ -1, %50 ], [ %43, %48 ]
ret i32 %53
}