#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 /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, long);
int strlen(char *);
long strtol(char *, int /*<<< orphan*/ *, int);
char toupper(char);
scalar_t__ truncate(char *, long);
int main(int argc, char *argv[]) {
  long off = strtol(argv[2], NULL, 10);
  char unit = toupper(argv[2][strlen(argv[2]) - 1]);
  switch (unit) {
  case 'K':
    off *= 1024;
    break;
  case 'M':
    off *= 1024 * 1024;
    break;
  case 'G':
    off *= 1024 * 1024 * 1024;
    break;
  default:
    unit = ' ';
  }
  printf("truncating %s to %li\n", argv[1], off);
  if (truncate(argv[1], off) < 0) {
    perror("truncate error");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    %rdi,-0x28(%rbp)
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
add    $0xffffffff,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <toupper@plt>
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%eax
mov    %eax,-0x20(%rbp)
sub    $0x47,%eax
je     1212 <main+0xb2>
jmp    11ce <main+0x6e>
mov    -0x20(%rbp),%eax
sub    $0x4b,%eax
je     11f0 <main+0x90>
jmp    11df <main+0x7f>
mov    -0x20(%rbp),%eax
sub    $0x4d,%eax
je     1201 <main+0xa1>
jmp    1223 <main+0xc3>
mov    -0x18(%rbp),%rax
shl    $0xa,%rax
mov    %rax,-0x18(%rbp)
jmp    1227 <main+0xc7>
mov    -0x18(%rbp),%rax
shl    $0x14,%rax
mov    %rax,-0x18(%rbp)
jmp    1227 <main+0xc7>
mov    -0x18(%rbp),%rax
shl    $0x1e,%rax
mov    %rax,-0x18(%rbp)
jmp    1227 <main+0xc7>
movb   $0x20,-0x19(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x18(%rbp),%rdx
lea    0xdc6(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <truncate@plt>
cmp    $0x0,%rax
jge    1272 <main+0x112>
lea    0xdb5(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1279 <main+0x119>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    0x10(%rsi),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %rax,%r14
mov    0x10(%r15),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movsbl -0x1(%rax,%rbx,1),%edi
call   1030 <toupper@plt>
movsbl %al,%eax
cmp    $0x47,%eax
je     11ab <main+0x4b>
cmp    $0x4d,%eax
je     11b1 <main+0x51>
cmp    $0x4b,%eax
jne    11b5 <main+0x55>
shl    $0xa,%r14
jmp    11b5 <main+0x55>
shl    $0x1e,%r14
jmp    11b5 <main+0x55>
shl    $0x14,%r14
mov    0x8(%r15),%rsi
lea    0xe40(%rip),%rdi
mov    %r14,%rdx
call   1050 <printf@plt>
mov    0x8(%r15),%rdi
mov    %r14,%rsi
call   1070 <truncate@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
js     11e4 <main+0x84>
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe2b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    11de <main+0x7e>

LLVM IR (-O0):
@.str = [22 x i8] c"truncating %s to %li\0A\00"
@.str.1 = [15 x i8] c"truncate error\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i8**, i8*** %5
%9 = getelementptr inbounds i8*, i8** %8, i64 2
%10 = load i8*, i8** %9
%11 = call i64 @strtol(i8* %10, i32* null, i32 10)
store i64 %11, i64* %6
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 2
%14 = load i8*, i8** %13
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 2
%17 = load i8*, i8** %16
%18 = call i32 @strlen(i8* %17)
%19 = sub nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %14, i64 %20
%22 = load i8, i8* %21
%23 = call signext i8 @toupper(i8 signext %22)
store i8 %23, i8* %7
%24 = load i8, i8* %7
%25 = sext i8 %24 to i32
switch i32 %25, label %35 [
i32 75, label %26
i32 77, label %29
i32 71, label %32
]
26: ; preds = %2
%27 = load i64, i64* %6
%28 = mul nsw i64 %27, 1024
store i64 %28, i64* %6
br label %36
29: ; preds = %2
%30 = load i64, i64* %6
%31 = mul nsw i64 %30, 1048576
store i64 %31, i64* %6
br label %36
32: ; preds = %2
%33 = load i64, i64* %6
%34 = mul nsw i64 %33, 1073741824
store i64 %34, i64* %6
br label %36
35: ; preds = %2
store i8 32, i8* %7
br label %36
36: ; preds = %35, %32, %29, %26
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = load i8*, i8** %38
%40 = load i64, i64* %6
%41 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %39, i64 %40)
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
%45 = load i64, i64* %6
%46 = call i64 @truncate(i8* %44, i64 %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %36
%49 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %51
50: ; preds = %36
store i32 0, i32* %3
br label %51
51: ; preds = %50, %48
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@.str = [22 x i8] c"truncating %s to %li\0A\00"
@.str.1 = [15 x i8] c"truncate error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 2
%4 = load i8*, i8** %3
%5 = tail call i64 @strtol(i8* nocapture %4, i32* null, i32 10)
%6 = load i8*, i8** %3
%7 = tail call i32 @strlen(i8* %6)
%8 = add nsw i32 %7, -1
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %6, i64 %9
%11 = load i8, i8* %10
%12 = tail call signext i8 @toupper(i8 signext %11)
%13 = sext i8 %12 to i32
switch i32 %13, label %20 [
i32 75, label %14
i32 77, label %16
i32 71, label %18
]
14: ; preds = %2
%15 = shl nsw i64 %5, 10
br label %20
16: ; preds = %2
%17 = shl nsw i64 %5, 20
br label %20
18: ; preds = %2
%19 = shl nsw i64 %5, 30
br label %20
20: ; preds = %2, %18, %16, %14
%21 = phi i64 [ %19, %18 ], [ %17, %16 ], [ %15, %14 ], [ %5, %2 ]
%22 = getelementptr inbounds i8*, i8** %1, i64 1
%23 = load i8*, i8** %22
%24 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %23, i64 %21)
%25 = load i8*, i8** %22
%26 = tail call i64 @truncate(i8* %25, i64 %21)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %20
%29 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %20, %28
%31 = phi i32 [ 1, %28 ], [ 0, %20 ]
ret i32 %31
}