#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 sg_io_hdr {
  int status;
  scalar_t__ host_status;
  int driver_status;
  unsigned char *sbp;
  int sb_len_wr;
};

/* Variables and functions */
int scsi_error(struct sg_io_hdr *hdr) {
  hdr->status &= 0x7e;
  if ((hdr->status == 0) && (hdr->host_status == 0) &&
      (hdr->driver_status == 0)) {
    return 0;
  }
  if ((hdr->status == 0x2) || (hdr->status == 0x22) ||
      ((hdr->driver_status & 0xf) == 0x08)) {
    if (hdr->sbp && (hdr->sb_len_wr > 2)) {
      int sense_key;
      unsigned char *sense_buffer = hdr->sbp;
      if (sense_buffer[0] & 0x2)
        sense_key = sense_buffer[1] & 0xf;
      else
        sense_key = sense_buffer[2] & 0xf;
      if (sense_key == 0x01)
        return 0;
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<scsi_error>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
and    $0x7e,%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jne    1149 <scsi_error+0x49>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    1149 <scsi_error+0x49>
mov    -0x10(%rbp),%rax
cmpl   $0x0,0x10(%rax)
jne    1149 <scsi_error+0x49>
movl   $0x0,-0x4(%rbp)
jmp    11fa <scsi_error+0xfa>
mov    -0x10(%rbp),%rax
cmpl   $0x2,(%rax)
je     1176 <scsi_error+0x76>
mov    -0x10(%rbp),%rax
cmpl   $0x22,(%rax)
je     1176 <scsi_error+0x76>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%eax
and    $0xf,%eax
cmp    $0x8,%eax
jne    11f3 <scsi_error+0xf3>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x18(%rax)
je     11ee <scsi_error+0xee>
mov    -0x10(%rbp),%rax
cmpl   $0x2,0x20(%rax)
jle    11ee <scsi_error+0xee>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     11c5 <scsi_error+0xc5>
mov    -0x20(%rbp),%rax
movzbl 0x1(%rax),%eax
and    $0xf,%eax
mov    %eax,-0x14(%rbp)
jmp    11d3 <scsi_error+0xd3>
mov    -0x20(%rbp),%rax
movzbl 0x2(%rax),%eax
and    $0xf,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    11e9 <scsi_error+0xe9>
movl   $0x0,-0x4(%rbp)
jmp    11fa <scsi_error+0xfa>
jmp    11ee <scsi_error+0xee>
jmp    11f3 <scsi_error+0xf3>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<scsi_error>:
mov    (%rdi),%eax
and    $0x7e,%eax
mov    %eax,(%rdi)
cmp    $0x22,%eax
je     112d <scsi_error+0x2d>
cmp    $0x2,%eax
je     112d <scsi_error+0x2d>
test   %eax,%eax
jne    1122 <scsi_error+0x22>
cmpq   $0x0,0x8(%rdi)
jne    1122 <scsi_error+0x22>
cmpl   $0x0,0x10(%rdi)
je     115b <scsi_error+0x5b>
mov    0x10(%rdi),%eax
and    $0xf,%eax
cmp    $0x8,%eax
jne    1155 <scsi_error+0x55>
mov    0x18(%rdi),%rax
test   %rax,%rax
je     1155 <scsi_error+0x55>
cmpl   $0x3,0x20(%rdi)
jl     1155 <scsi_error+0x55>
mov    (%rax),%cl
and    $0x2,%cl
cmp    $0x1,%cl
adc    $0x0,%rax
mov    0x1(%rax),%cl
and    $0xf,%cl
xor    %eax,%eax
cmp    $0x1,%cl
je     115a <scsi_error+0x5a>
mov    $0x1,%eax
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
%struct.sg_io_hdr = type { i32, i64, i32, i8*, i32 }
define i32 @scsi_error(%struct.sg_io_hdr* %0) {
%2 = alloca i32
%3 = alloca %struct.sg_io_hdr*
%4 = alloca i32
%5 = alloca i8*
store %struct.sg_io_hdr* %0, %struct.sg_io_hdr** %3
%6 = load %struct.sg_io_hdr*, %struct.sg_io_hdr** %3
%7 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %6, i32 0, i32 0
%8 = load i32, i32* %7
%9 = and i32 %8, 126
store i32 %9, i32* %7
%10 = load %struct.sg_io_hdr*, %struct.sg_io_hdr** %3
%11 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %10, i32 0, i32 0
%12 = load i32, i32* %11
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %25
14: ; preds = %1
%15 = load %struct.sg_io_hdr*, %struct.sg_io_hdr** %3
%16 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %15, i32 0, i32 1
%17 = load i64, i64* %16
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %14
%20 = load %struct.sg_io_hdr*, %struct.sg_io_hdr** %3
%21 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %20, i32 0, i32 2
%22 = load i32, i32* %21
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 0, i32* %2
br label %80
25: ; preds = %19, %14, %1
%26 = load %struct.sg_io_hdr*, %struct.sg_io_hdr** %3
%27 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = icmp eq i32 %28, 2
br i1 %29, label %41, label %30
30: ; preds = %25
%31 = load %struct.sg_io_hdr*, %struct.sg_io_hdr** %3
%32 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = icmp eq i32 %33, 34
br i1 %34, label %41, label %35
35: ; preds = %30
%36 = load %struct.sg_io_hdr*, %struct.sg_io_hdr** %3
%37 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %36, i32 0, i32 2
%38 = load i32, i32* %37
%39 = and i32 %38, 15
%40 = icmp eq i32 %39, 8
br i1 %40, label %41, label %79
41: ; preds = %35, %30, %25
%42 = load %struct.sg_io_hdr*, %struct.sg_io_hdr** %3
%43 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %42, i32 0, i32 3
%44 = load i8*, i8** %43
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %78
46: ; preds = %41
%47 = load %struct.sg_io_hdr*, %struct.sg_io_hdr** %3
%48 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %47, i32 0, i32 4
%49 = load i32, i32* %48
%50 = icmp sgt i32 %49, 2
br i1 %50, label %51, label %78
51: ; preds = %46
%52 = load %struct.sg_io_hdr*, %struct.sg_io_hdr** %3
%53 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %52, i32 0, i32 3
%54 = load i8*, i8** %53
store i8* %54, i8** %5
%55 = load i8*, i8** %5
%56 = getelementptr inbounds i8, i8* %55, i64 0
%57 = load i8, i8* %56
%58 = zext i8 %57 to i32
%59 = and i32 %58, 2
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %67
61: ; preds = %51
%62 = load i8*, i8** %5
%63 = getelementptr inbounds i8, i8* %62, i64 1
%64 = load i8, i8* %63
%65 = zext i8 %64 to i32
%66 = and i32 %65, 15
store i32 %66, i32* %4
br label %73
67: ; preds = %51
%68 = load i8*, i8** %5
%69 = getelementptr inbounds i8, i8* %68, i64 2
%70 = load i8, i8* %69
%71 = zext i8 %70 to i32
%72 = and i32 %71, 15
store i32 %72, i32* %4
br label %73
73: ; preds = %67, %61
%74 = load i32, i32* %4
%75 = icmp eq i32 %74, 1
br i1 %75, label %76, label %77
76: ; preds = %73
store i32 0, i32* %2
br label %80
77: ; preds = %73
br label %78
78: ; preds = %77, %46, %41
br label %79
79: ; preds = %78, %35
store i32 1, i32* %2
br label %80
80: ; preds = %79, %76, %24
%81 = load i32, i32* %2
ret i32 %81
}

LLVM IR (-O2):
%struct.sg_io_hdr = type { i32, i64, i32, i8*, i32 }
define i32 @scsi_error(%struct.sg_io_hdr* nocapture %0) {
%2 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %0, i64 0, i32 0
%3 = load i32, i32* %2
%4 = and i32 %3, 126
store i32 %4, i32* %2
switch i32 %4, label %13 [
i32 0, label %5
i32 2, label %18
i32 34, label %18
]
5: ; preds = %1
%6 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %0, i64 0, i32 1
%7 = load i64, i64* %6
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %5
%10 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %0, i64 0, i32 2
%11 = load i32, i32* %10
%12 = icmp eq i32 %11, 0
br i1 %12, label %36, label %13
13: ; preds = %1, %5, %9
%14 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %0, i64 0, i32 2
%15 = load i32, i32* %14
%16 = and i32 %15, 15
%17 = icmp eq i32 %16, 8
br i1 %17, label %18, label %35
18: ; preds = %1, %1, %13
%19 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %0, i64 0, i32 3
%20 = load i8*, i8** %19
%21 = icmp eq i8* %20, null
br i1 %21, label %35, label %22
22: ; preds = %18
%23 = getelementptr inbounds %struct.sg_io_hdr, %struct.sg_io_hdr* %0, i64 0, i32 4
%24 = load i32, i32* %23
%25 = icmp sgt i32 %24, 2
br i1 %25, label %26, label %35
26: ; preds = %22
%27 = load i8, i8* %20
%28 = and i8 %27, 2
%29 = icmp eq i8 %28, 0
%30 = select i1 %29, i64 2, i64 1
%31 = getelementptr inbounds i8, i8* %20, i64 %30
%32 = load i8, i8* %31
%33 = and i8 %32, 15
%34 = icmp eq i8 %33, 1
br i1 %34, label %36, label %35
35: ; preds = %26, %18, %22, %13
br label %36
36: ; preds = %26, %9, %35
%37 = phi i32 [ 0, %26 ], [ 1, %35 ], [ 0, %9 ]
ret i32 %37
}