#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*/);
int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len) {
  int type_num = 0;
  char *eptr;
  const char *type = "generic";
  type_num = strtoul(from, &eptr, 0);
  if (eptr != from) {
    switch (type_num) {
    case 1:
      type = "rbc";
      break;
    case 2:
      type = "atapi";
      break;
    case 3:
      type = "tape";
      break;
    case 4:
      type = "floppy";
      break;
    case 6:
      type = "scsi";
      break;
    default:
      break;
    }
  }
  strncpy(to, type, (strlen(type) < len ? strlen(type) : len));
  return type_num;
}


x86 assembly (clang, -O0):
<set_usb_mass_storage_ifsubtype>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    0xec6(%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     11f9 <set_usb_mass_storage_ifsubtype+0xc9>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x38(%rbp)
sub    $0x5,%eax
ja     11ef <set_usb_mass_storage_ifsubtype+0xbf>
mov    -0x38(%rbp),%rax
lea    0xe6a(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe7a(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11f4 <set_usb_mass_storage_ifsubtype+0xc4>
lea    0xe6e(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11f4 <set_usb_mass_storage_ifsubtype+0xc4>
lea    0xe64(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11f4 <set_usb_mass_storage_ifsubtype+0xc4>
lea    0xe59(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11f4 <set_usb_mass_storage_ifsubtype+0xc4>
lea    0xe50(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11f4 <set_usb_mass_storage_ifsubtype+0xc4>
jmp    11f4 <set_usb_mass_storage_ifsubtype+0xc4>
jmp    11f9 <set_usb_mass_storage_ifsubtype+0xc9>
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    122e <set_usb_mass_storage_ifsubtype+0xfe>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x50(%rbp)
jmp    1236 <set_usb_mass_storage_ifsubtype+0x106>
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_usb_mass_storage_ifsubtype>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1050 <strtoul@plt>
mov    %eax,%r15d
lea    0xea2(%rip),%rbp
cmp    %rbx,0x8(%rsp)
je     117d <set_usb_mass_storage_ifsubtype+0x4d>
add    $0xffffffff,%eax
cmp    $0x5,%eax
ja     117d <set_usb_mass_storage_ifsubtype+0x4d>
cltq
lea    0xeae(%rip),%rcx
movslq (%rcx,%rax,4),%rbp
add    %rcx,%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
cmp    %r12,%rax
cmovb  %rax,%r12
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
call   1030 <strncpy@plt>
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"generic\00"
@.str.1 = [4 x i8] c"rbc\00"
@.str.2 = [6 x i8] c"atapi\00"
@.str.3 = [5 x i8] c"tape\00"
@.str.4 = [7 x i8] c"floppy\00"
@.str.5 = [5 x i8] c"scsi\00"
define i32 @set_usb_mass_storage_ifsubtype(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 i32 0, i32* %7
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 %24
15: ; preds = %3
%16 = load i32, i32* %7
switch i32 %16, label %22 [
i32 1, label %17
i32 2, label %18
i32 3, label %19
i32 4, label %20
i32 6, label %21
]
17: ; preds = %15
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %9
br label %23
18: ; preds = %15
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8** %9
br label %23
19: ; preds = %15
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8** %9
br label %23
20: ; preds = %15
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8** %9
br label %23
21: ; preds = %15
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8** %9
br label %23
22: ; preds = %15
br label %23
23: ; preds = %22, %21, %20, %19, %18, %17
br label %24
24: ; preds = %23, %3
%25 = load i8*, i8** %4
%26 = load i8*, i8** %9
%27 = load i8*, i8** %9
%28 = call i64 @strlen(i8* %27)
%29 = load i64, i64* %6
%30 = icmp ult i64 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = load i8*, i8** %9
%33 = call i64 @strlen(i8* %32)
br label %36
34: ; preds = %24
%35 = load i64, i64* %6
br label %36
36: ; preds = %34, %31
%37 = phi i64 [ %33, %31 ], [ %35, %34 ]
%38 = call i32 @strncpy(i8* %25, i8* %26, i64 %37)
%39 = load i32, i32* %7
ret i32 %39
}

LLVM IR (-O2):
@.str = [8 x i8] c"generic\00"
@.str.1 = [4 x i8] c"rbc\00"
@.str.2 = [6 x i8] c"atapi\00"
@.str.3 = [5 x i8] c"tape\00"
@.str.4 = [7 x i8] c"floppy\00"
@.str.5 = [5 x i8] c"scsi\00"
@reltable.set_usb_mass_storage_ifsubtype = [6 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.1 to i64), i64 ptrtoint ([6 x i32]* @reltable.set_usb_mass_storage_ifsubtype to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([6 x i8]* @.str.2 to i64), i64 ptrtoint ([6 x i32]* @reltable.set_usb_mass_storage_ifsubtype to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.3 to i64), i64 ptrtoint ([6 x i32]* @reltable.set_usb_mass_storage_ifsubtype to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([7 x i8]* @.str.4 to i64), i64 ptrtoint ([6 x i32]* @reltable.set_usb_mass_storage_ifsubtype to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([8 x i8]* @.str to i64), i64 ptrtoint ([6 x i32]* @reltable.set_usb_mass_storage_ifsubtype to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.5 to i64), i64 ptrtoint ([6 x i32]* @reltable.set_usb_mass_storage_ifsubtype to i64)) to i32)]
define i32 @set_usb_mass_storage_ifsubtype(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 eq i8* %7, %1
br i1 %8, label %16, label %9
9: ; preds = %3
%10 = add i32 %6, -1
%11 = icmp ult i32 %10, 6
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = sext i32 %10 to i64
%14 = shl i64 %13, 2
%15 = call i8* @llvm.load.relative.i64(i8* bitcast ([6 x i32]* @reltable.set_usb_mass_storage_ifsubtype to i8*), i64 %14)
br label %16
16: ; preds = %12, %9, %3
%17 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %9 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %3 ], [ %15, %12 ]
%18 = tail call i64 @strlen(i8* dereferenceable(1) %17)
%19 = icmp ult i64 %18, %2
%20 = select i1 %19, i64 %18, i64 %2
%21 = tail call i32 @strncpy(i8* %0, i8* %17, i64 %20)
ret i32 %6
}