#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 */
long atol(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int strlen(char *);
int tolower(char);
long tosec(char *timestr) {
  int length = strlen(timestr);
  int unitchar = tolower(timestr[length - 1]);
  long unit;
  long time;
  switch (unitchar) {
  case 'd':
    unit = 60 * 60 * 24;
    break;
  case 'h':
    unit = 60 * 60;
    break;
  case 'm':
    unit = 60;
    break;
  case 's':
    unit = 1;
    break;
  default:
    perror("Invalid time unit\n");
    exit(1);
  }
  return unit * atol(timestr);
}


x86 assembly (clang, -O0):
<tosec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0xffffffff,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <tolower@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffff9c,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0xf,%eax
ja     11de <tosec+0x8e>
mov    -0x28(%rbp),%rax
lea    0xe5f(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movq   $0x15180,-0x18(%rbp)
jmp    11f4 <tosec+0xa4>
movq   $0xe10,-0x18(%rbp)
jmp    11f4 <tosec+0xa4>
movq   $0x3c,-0x18(%rbp)
jmp    11f4 <tosec+0xa4>
movq   $0x1,-0x18(%rbp)
jmp    11f4 <tosec+0xa4>
lea    0xe5b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <atol@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
imul   %rcx,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tosec>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <strlen@plt>
cltq
movsbl -0x1(%rax,%rbx,1),%edi
call   1040 <tolower@plt>
add    $0xffffff9c,%eax
cmp    $0xf,%eax
ja     11b0 <tosec+0x60>
mov    $0x15180,%r14d
lea    0xe83(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    $0xe10,%r14d
jmp    119c <tosec+0x4c>
mov    $0x3c,%r14d
jmp    119c <tosec+0x4c>
mov    $0x1,%r14d
mov    %rbx,%rdi
call   1050 <atol@plt>
imul   %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe89(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"Invalid time unit\0A\00"
define i64 @tosec(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
store i32 %8, i32* %3
%9 = load i8*, i8** %2
%10 = load i32, i32* %3
%11 = sub nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %9, i64 %12
%14 = load i8, i8* %13
%15 = call i32 @tolower(i8 signext %14)
store i32 %15, i32* %4
%16 = load i32, i32* %4
switch i32 %16, label %21 [
i32 100, label %17
i32 104, label %18
i32 109, label %19
i32 115, label %20
]
17: ; preds = %1
store i64 86400, i64* %5
br label %24
18: ; preds = %1
store i64 3600, i64* %5
br label %24
19: ; preds = %1
store i64 60, i64* %5
br label %24
20: ; preds = %1
store i64 1, i64* %5
br label %24
21: ; preds = %1
%22 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %20, %19, %18, %17
%25 = load i64, i64* %5
%26 = load i8*, i8** %2
%27 = call i64 @atol(i8* %26)
%28 = mul nsw i64 %25, %27
ret i64 %28
}

LLVM IR (-O2):
@.str = [19 x i8] c"Invalid time unit\0A\00"
define i64 @tosec(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, -1
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %0, i64 %4
%6 = load i8, i8* %5
%7 = tail call i32 @tolower(i8 signext %6)
switch i32 %7, label %11 [
i32 100, label %14
i32 104, label %8
i32 109, label %9
i32 115, label %10
]
8: ; preds = %1
br label %14
9: ; preds = %1
br label %14
10: ; preds = %1
br label %14
11: ; preds = %1
%12 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %1, %10, %9, %8
%15 = phi i64 [ 1, %10 ], [ 60, %9 ], [ 3600, %8 ], [ 86400, %1 ]
%16 = tail call i64 @atol(i8* %0)
%17 = mul nsw i64 %16, %15
ret i64 %17
}