#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*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
long getSize(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ truncate(char *, long);
int main(int argc, char *args[]) {
  if (argc < 3) {
    fprintf(stderr, "useage error: %s\n", strerror(errno));
    printf("useage:%s <pathname> <size in byte>\n", args[0]);
    printf("eg: %s ~/1.txt 20 \n", args[0]);
    exit(1);
  }
  long size = getSize(args[1]);
  printf("file %s with size:%ld bytes.\n", args[1], size);
  long arg_size = atol(args[2]);
  size = arg_size < size ? arg_size : size;
  if (truncate(args[1], size) < 0) {
    fprintf(stderr, "trun file %s failed: %s\n", args[1], strerror(errno));
    exit(1);
  }
  printf("cate first %ld bytes success.\n", size);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11f2 <main+0x82>
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <getSize@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1060 <atol@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    1249 <main+0xd9>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1251 <main+0xe1>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <truncate@plt>
cmp    $0x0,%rax
jge    12bc <main+0x14c>
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xdbe(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x2,%edi
jle    11d9 <main+0x69>
mov    0x8(%r14),%rdi
call   1090 <getSize@plt>
mov    %rax,%rbx
mov    0x8(%r14),%rsi
lea    0xeb8(%rip),%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x10(%r14),%rdi
call   1060 <atol@plt>
cmp    %rbx,%rax
cmovl  %rax,%rbx
mov    0x8(%r14),%rdi
mov    %rbx,%rsi
call   1050 <truncate@plt>
test   %rax,%rax
js     122f <main+0xbf>
lea    0xebd(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e00(%rip),%rax
mov    (%rax),%ebx
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe09(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%r14),%rsi
lea    0xe05(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%r14),%rsi
lea    0xe19(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%ebx
mov    0x8(%r14),%r14
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe18(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [18 x i8] c"useage error: %s\0A\00"
@errno = global i32 0
@.str.1 = [37 x i8] c"useage:%s <pathname> <size in byte>\0A\00"
@.str.2 = [20 x i8] c"eg: %s ~/1.txt 20 \0A\00"
@.str.3 = [30 x i8] c"file %s with size:%ld bytes.\0A\00"
@.str.4 = [25 x i8] c"trun file %s failed: %s\0A\00"
@.str.5 = [31 x i8] c"cate first %ld bytes success.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 3
br i1 %9, label %10, label %24
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i64 @getSize(i8* %27)
store i64 %28, i64* %6
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = load i64, i64* %6
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %31, i64 %32)
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 2
%36 = load i8*, i8** %35
%37 = call i64 @atol(i8* %36)
store i64 %37, i64* %7
%38 = load i64, i64* %7
%39 = load i64, i64* %6
%40 = icmp slt i64 %38, %39
br i1 %40, label %41, label %43
41: ; preds = %24
%42 = load i64, i64* %7
br label %45
43: ; preds = %24
%44 = load i64, i64* %6
br label %45
45: ; preds = %43, %41
%46 = phi i64 [ %42, %41 ], [ %44, %43 ]
store i64 %46, i64* %6
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = load i8*, i8** %48
%50 = load i64, i64* %6
%51 = call i64 @truncate(i8* %49, i64 %50)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %62
53: ; preds = %45
%54 = load i32, i32* @stderr
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 1
%57 = load i8*, i8** %56
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %57, i8* %59)
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %45
%63 = load i64, i64* %6
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i64 %63)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [18 x i8] c"useage error: %s\0A\00"
@errno = global i32 0
@.str.1 = [37 x i8] c"useage:%s <pathname> <size in byte>\0A\00"
@.str.2 = [20 x i8] c"eg: %s ~/1.txt 20 \0A\00"
@.str.3 = [30 x i8] c"file %s with size:%ld bytes.\0A\00"
@.str.4 = [25 x i8] c"trun file %s failed: %s\0A\00"
@.str.5 = [31 x i8] c"cate first %ld bytes success.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %14
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i32, i32* @errno
%7 = tail call i8* @strerror(i32 %6)
%8 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i8*, i8** %1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = load i8*, i8** %1
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i64 @getSize(i8* %16)
%18 = load i8*, i8** %15
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %18, i64 %17)
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = tail call i64 @atol(i8* %21)
%23 = icmp slt i64 %22, %17
%24 = select i1 %23, i64 %22, i64 %17
%25 = load i8*, i8** %15
%26 = tail call i64 @truncate(i8* %25, i64 %24)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %14
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %15
%31 = load i32, i32* @errno
%32 = tail call i8* @strerror(i32 %31)
%33 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %30, i8* %32)
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %14
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i64 %24)
ret i32 0
}