#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 inquiry(char *, char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scsi_close();
scalar_t__ scsi_open(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int);
void scan_bus() {
  char vendor[9], model[17], rev[5];
  char d[1024];
  int bus, dev, type;
  int firstbus, lastbus;
  int firstdev, lastdev;
  firstbus = 0;
  firstdev = 0;
  lastbus = 0;
  lastdev = 0;
  printf(
      "Device                   Manufacturer  Model              Revision\n"
      "-----------------------  ------------- ------------------ --------\n");
  for (bus = firstbus; bus <= lastbus; bus++) {
    for (dev = firstdev; dev <= lastdev; dev++) {
      snprintf(d, 1024, "/dev/scsi/sc%dd%dl0", bus, dev);
      if (scsi_open(d) == 0) {
        if ((type = inquiry(vendor, model, rev)) >= 0) {
          type = type & 0x1f;
          printf("%-23s  %-13s %-18s %-8s %s\n", d, vendor, model, rev,
                 (type == 0x5 ? "[CD-ROM]" : ""));
        }
        scsi_close();
      }
    }
  }
}


x86 assembly (clang, -O0):
<scan_bus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x440(%rbp)
movl   $0x0,-0x448(%rbp)
movl   $0x0,-0x444(%rbp)
movl   $0x0,-0x44c(%rbp)
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x440(%rbp),%eax
mov    %eax,-0x434(%rbp)
mov    -0x434(%rbp),%eax
cmp    -0x444(%rbp),%eax
jg     12ac <scan_bus+0x15c>
mov    -0x448(%rbp),%eax
mov    %eax,-0x438(%rbp)
mov    -0x438(%rbp),%eax
cmp    -0x44c(%rbp),%eax
jg     1293 <scan_bus+0x143>
lea    -0x430(%rbp),%rdi
mov    -0x434(%rbp),%ecx
mov    -0x438(%rbp),%r8d
mov    $0x400,%esi
lea    0xe9a(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x430(%rbp),%rdi
call   1060 <scsi_open@plt>
cmp    $0x0,%rax
jne    127a <scan_bus+0x12a>
lea    -0x9(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x25(%rbp),%rdx
call   1070 <inquiry@plt>
mov    %eax,-0x43c(%rbp)
cmp    $0x0,%eax
jl     1273 <scan_bus+0x123>
mov    -0x43c(%rbp),%eax
and    $0x1f,%eax
mov    %eax,-0x43c(%rbp)
lea    -0x430(%rbp),%rsi
lea    -0x9(%rbp),%rdx
lea    -0x20(%rbp),%rcx
lea    -0x25(%rbp),%r8
mov    -0x43c(%rbp),%edi
lea    0xe2f(%rip),%r9
lea    0xe59(%rip),%rax
cmp    $0x5,%edi
cmove  %rax,%r9
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <scsi_close@plt>
jmp    127f <scan_bus+0x12f>
mov    -0x438(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x438(%rbp)
jmp    11bb <scan_bus+0x6b>
jmp    1298 <scan_bus+0x148>
mov    -0x434(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x434(%rbp)
jmp    119d <scan_bus+0x4d>
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_bus>:
push   %rbx
sub    $0x430,%rsp
lea    0xeca(%rip),%rdi
call   1030 <puts@plt>
lea    0xe85(%rip),%rdx
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1070 <scsi_open@plt>
test   %rax,%rax
jne    11f8 <scan_bus+0x98>
lea    0x7(%rsp),%rdi
lea    0x10(%rsp),%rsi
lea    0x2(%rsp),%rdx
call   1080 <inquiry@plt>
test   %eax,%eax
js     11f1 <scan_bus+0x91>
and    $0x1f,%eax
cmp    $0x5,%eax
lea    0xe6c(%rip),%rax
lea    0xe64(%rip),%r9
cmove  %rax,%r9
lea    0xe3e(%rip),%rdi
lea    0x30(%rsp),%rsi
lea    0x7(%rsp),%rdx
lea    0x10(%rsp),%rcx
lea    0x2(%rsp),%r8
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <scsi_close@plt>
add    $0x430,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [135 x i8] c"Device Manufacturer Model Revision\0A----------------------- ------------- ------------------ --------\0A\00"
@.str.1 = [20 x i8] c"/dev/scsi/sc%dd%dl0\00"
@.str.2 = [28 x i8] c"%-23s %-13s %-18s %-8s %s\0A\00"
@.str.3 = [9 x i8] c"[CD-ROM]\00"
@.str.4 = [1 x i8] zeroinitializer
define void @scan_bus() {
%1 = alloca [9 x i8]
%2 = alloca [17 x i8]
%3 = alloca [5 x i8]
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %8
store i32 0, i32* %10
store i32 0, i32* %9
store i32 0, i32* %11
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([135 x i8], [135 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* %8
store i32 %13, i32* %5
br label %14
14: ; preds = %57, %0
%15 = load i32, i32* %5
%16 = load i32, i32* %9
%17 = icmp sle i32 %15, %16
br i1 %17, label %18, label %60
18: ; preds = %14
%19 = load i32, i32* %10
store i32 %19, i32* %6
br label %20
20: ; preds = %53, %18
%21 = load i32, i32* %6
%22 = load i32, i32* %11
%23 = icmp sle i32 %21, %22
br i1 %23, label %24, label %56
24: ; preds = %20
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%26 = load i32, i32* %5
%27 = load i32, i32* %6
%28 = call i32 @snprintf(i8* %25, i32 1024, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %26, i32 %27)
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%30 = call i64 @scsi_open(i8* %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %52
32: ; preds = %24
%33 = getelementptr inbounds [9 x i8], [9 x i8]* %1, i64 0, i64 0
%34 = getelementptr inbounds [17 x i8], [17 x i8]* %2, i64 0, i64 0
%35 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%36 = call i32 @inquiry(i8* %33, i8* %34, i8* %35)
store i32 %36, i32* %7
%37 = icmp sge i32 %36, 0
br i1 %37, label %38, label %50
38: ; preds = %32
%39 = load i32, i32* %7
%40 = and i32 %39, 31
store i32 %40, i32* %7
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%42 = getelementptr inbounds [9 x i8], [9 x i8]* %1, i64 0, i64 0
%43 = getelementptr inbounds [17 x i8], [17 x i8]* %2, i64 0, i64 0
%44 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%45 = load i32, i32* %7
%46 = icmp eq i32 %45, 5
%47 = zext i1 %46 to i64
%48 = select i1 %46, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0)
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %41, i8* %42, i8* %43, i8* %44, i8* %48)
br label %50
50: ; preds = %38, %32
%51 = call i32 (...) @scsi_close()
br label %52
52: ; preds = %50, %24
br label %53
53: ; preds = %52
%54 = load i32, i32* %6
%55 = add nsw i32 %54, 1
store i32 %55, i32* %6
br label %20
56: ; preds = %20
br label %57
57: ; preds = %56
%58 = load i32, i32* %5
%59 = add nsw i32 %58, 1
store i32 %59, i32* %5
br label %14
60: ; preds = %14
ret void
}

LLVM IR (-O2):
@.str.1 = [20 x i8] c"/dev/scsi/sc%dd%dl0\00"
@.str.2 = [28 x i8] c"%-23s %-13s %-18s %-8s %s\0A\00"
@.str.3 = [9 x i8] c"[CD-ROM]\00"
@.str.4 = [1 x i8] zeroinitializer
@str = [134 x i8] c"Device Manufacturer Model Revision\0A----------------------- ------------- ------------------ --------\00"
define void @scan_bus() {
%1 = alloca [9 x i8]
%2 = alloca [17 x i8]
%3 = alloca [5 x i8]
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [9 x i8], [9 x i8]* %1, i64 0, i64 0
%6 = getelementptr inbounds [17 x i8], [17 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([134 x i8], [134 x i8]* @str, i64 0, i64 0))
%10 = call i32 @snprintf(i8* %8, i32 1024, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 0, i32 0)
%11 = call i64 @scsi_open(i8* %8)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %23
13: ; preds = %0
%14 = call i32 @inquiry(i8* %5, i8* %6, i8* %7)
%15 = icmp sgt i32 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = and i32 %14, 31
%18 = icmp eq i32 %17, 5
%19 = select i1 %18, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0)
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %8, i8* %5, i8* %6, i8* %7, i8* %19)
br label %21
21: ; preds = %16, %13
%22 = call i32 (...) @scsi_close()
br label %23
23: ; preds = %0, %21
ret void
}