#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 */
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
int strtoul(char const *, char **, int /*<<< orphan*/);
void set_scsi_type(char *to, const char *from, size_t len) {
  int type_num;
  char *eptr;
  const char *type = "generic";
  type_num = strtoul(from, &eptr, 0);
  if (eptr != from) {
    switch (type_num) {
    case 0:
    case 0xe:
      type = "disk";
      break;
    case 1:
      type = "tape";
      break;
    case 4:
    case 7:
    case 0xf:
      type = "optical";
      break;
    case 5:
      type = "cd";
      break;
    default:
      break;
    }
  }
  strncpy(to, type, (strlen(type) < len ? strlen(type) : len));
}


x86 assembly (clang, -O0):
<set_scsi_type>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xef5(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1050 <strtoul@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11de <set_scsi_type+0xae>
mov    -0x1c(%rbp),%eax
mov    %rax,-0x38(%rbp)
sub    $0xf,%rax
ja     11d4 <set_scsi_type+0xa4>
mov    -0x38(%rbp),%rax
lea    0xe75(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xead(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11d9 <set_scsi_type+0xa9>
lea    0xea2(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11d9 <set_scsi_type+0xa9>
lea    0xe97(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11d9 <set_scsi_type+0xa9>
lea    0xe8f(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11d9 <set_scsi_type+0xa9>
jmp    11d9 <set_scsi_type+0xa9>
jmp    11de <set_scsi_type+0xae>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
cmp    -0x18(%rbp),%rax
jae    1213 <set_scsi_type+0xe3>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x50(%rbp)
jmp    121b <set_scsi_type+0xeb>
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rdx
call   1030 <strncpy@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_scsi_type>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rsp,%rsi
mov    %r12,%rdi
xor    %edx,%edx
call   1050 <strtoul@plt>
lea    0xeab(%rip),%rbx
cmp    %r12,(%rsp)
je     1170 <set_scsi_type+0x40>
cmp    $0xf,%eax
ja     1170 <set_scsi_type+0x40>
cltq
lea    0xeb7(%rip),%rcx
movslq (%rcx,%rax,4),%rbx
add    %rcx,%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    %r15,%rax
cmovb  %rax,%r15
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <strncpy@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"generic\00"
@.str.1 = [5 x i8] c"disk\00"
@.str.2 = [5 x i8] c"tape\00"
@.str.3 = [8 x i8] c"optical\00"
@.str.4 = [3 x i8] c"cd\00"
define void @set_scsi_type(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %9
%10 = load i8*, i8** %5
%11 = call i32 @strtoul(i8* %10, i8** %8, i32 0)
store i32 %11, i32* %7
%12 = load i8*, i8** %8
%13 = load i8*, i8** %5
%14 = icmp ne i8* %12, %13
br i1 %14, label %15, label %23
15: ; preds = %3
%16 = load i32, i32* %7
switch i32 %16, label %21 [
i32 0, label %17
i32 14, label %17
i32 1, label %18
i32 4, label %19
i32 7, label %19
i32 15, label %19
i32 5, label %20
]
17: ; preds = %15, %15
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %9
br label %22
18: ; preds = %15
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %9
br label %22
19: ; preds = %15, %15, %15
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8** %9
br label %22
20: ; preds = %15
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8** %9
br label %22
21: ; preds = %15
br label %22
22: ; preds = %21, %20, %19, %18, %17
br label %23
23: ; preds = %22, %3
%24 = load i8*, i8** %4
%25 = load i8*, i8** %9
%26 = load i8*, i8** %9
%27 = call i64 @strlen(i8* %26)
%28 = load i64, i64* %6
%29 = icmp ult i64 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %23
%31 = load i8*, i8** %9
%32 = call i64 @strlen(i8* %31)
br label %35
33: ; preds = %23
%34 = load i64, i64* %6
br label %35
35: ; preds = %33, %30
%36 = phi i64 [ %32, %30 ], [ %34, %33 ]
%37 = call i32 @strncpy(i8* %24, i8* %25, i64 %36)
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"generic\00"
@.str.1 = [5 x i8] c"disk\00"
@.str.2 = [5 x i8] c"tape\00"
@.str.3 = [8 x i8] c"optical\00"
@.str.4 = [3 x i8] c"cd\00"
@reltable.set_scsi_type = [16 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.1 to i64), i64 ptrtoint ([16 x i32]* @reltable.set_scsi_type to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.2 to i64), i64 ptrtoint ([16 x i32]* @reltable.set_scsi_type to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([8 x i8]* @.str to i64), i64 ptrtoint ([16 x i32]* @reltable.set_scsi_type to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([8 x i8]* @.str to i64), i64 ptrtoint ([16 x i32]* @reltable.set_scsi_type to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([8 x i8]* @.str.3 to i64), i64 ptrtoint ([16 x i32]* @reltable.set_scsi_type to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.4 to i64), i64 ptrtoint ([16 x i32]* @reltable.set_scsi_type to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([8 x i8]* @.str to i64), i64 ptrtoint ([16 x i32]* @reltable.set_scsi_type to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([8 x i8]* @.str.3 to i64), i64 ptrtoint ([16 x i32]* @reltable.set_scsi_type to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([8 x i8]* @.str to i64), i64 ptrtoint ([16 x i32]* @reltable.set_scsi_type to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([8 x i8]* @.str to i64), i64 ptrtoint ([16 x i32]* @reltable.set_scsi_type to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([8 x i8]* @.str to i64), i64 ptrtoint ([16 x i32]* @reltable.set_scsi_type to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([8 x i8]* @.str to i64), i64 ptrtoint ([16 x i32]* @reltable.set_scsi_type to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([8 x i8]* @.str to i64), i64 ptrtoint ([16 x i32]* @reltable.set_scsi_type to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([8 x i8]* @.str to i64), i64 ptrtoint ([16 x i32]* @reltable.set_scsi_type to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.1 to i64), i64 ptrtoint ([16 x i32]* @reltable.set_scsi_type to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([8 x i8]* @.str.3 to i64), i64 ptrtoint ([16 x i32]* @reltable.set_scsi_type to i64)) to i32)]
define void @set_scsi_type(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = call i32 @strtoul(i8* %1, i8** %4, i32 0)
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, %1
%9 = icmp ult i32 %6, 16
%10 = select i1 %8, i1 %9, i1 false
br i1 %10, label %11, label %15
11: ; preds = %3
%12 = sext i32 %6 to i64
%13 = shl i64 %12, 2
%14 = call i8* @llvm.load.relative.i64(i8* bitcast ([16 x i32]* @reltable.set_scsi_type to i8*), i64 %13)
br label %15
15: ; preds = %11, %3
%16 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %3 ], [ %14, %11 ]
%17 = tail call i64 @strlen(i8* dereferenceable(1) %16)
%18 = icmp ult i64 %17, %2
%19 = select i1 %18, i64 %17, i64 %2
%20 = tail call i32 @strncpy(i8* %0, i8* %16, i64 %19)
ret void
}