#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 */
typedef int /*<<< orphan*/ off_t;
typedef int mode_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int O_CREAT;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int ftruncate(int, int /*<<< orphan*/);
int /*<<< orphan*/ get_size(char const *);
int getopt(int, char **, char *);
int open(char const *, int, ...);
size_t optind;
int optopt;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usage(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  const char *fname;
  const char *szstr;
  off_t sz;
  int create = 0;
  int option;
  int fd;
  while ((option = getopt(argc, argv, "c")) != -1) {
    switch (option) {
    case 'c':
      create = 1;
      break;
    case '?':
      fprintf(stderr,
              "trunc"
              ": Unrecognized option `%c'.\n\n",
              optopt);
      usage(stderr);
      exit(EXIT_FAILURE);
    default:
      abort();
    }
  }
  if (argv[optind] == NULL || argv[optind + 1] == NULL ||
      argv[optind + 2] != NULL) {
    usage(stderr);
    exit(EXIT_FAILURE);
  }
  fname = argv[optind];
  szstr = argv[optind + 1];
  sz = get_size(szstr);
  if (create) {
    mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    fd = open(fname, O_WRONLY | O_CREAT, mode);
  } else {
    fd = open(fname, O_WRONLY);
  }
  if (fd == -1) {
    perror("trunc"
           ": open");
    exit(EXIT_FAILURE);
  }
  if (ftruncate(fd, sz) == -1) {
    perror("trunc"
           ": truncate");
    exit(EXIT_FAILURE);
  }
  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)
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe45(%rip),%rdx
call   10a0 <getopt@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
je     1243 <main+0xb3>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x38(%rbp)
sub    $0x3f,%eax
je     11fd <main+0x6d>
jmp    11e0 <main+0x50>
mov    -0x38(%rbp),%eax
sub    $0x63,%eax
jne    1237 <main+0xa7>
jmp    11f1 <main+0x61>
movl   $0x1,-0x28(%rbp)
jmp    123e <main+0xae>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
lea    0xdec(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   1070 <usage@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1040 <abort@plt>
jmp    11ad <main+0x1d>
mov    -0x10(%rbp),%rax
mov    0x2d7a(%rip),%rcx
mov    (%rcx),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1290 <main+0x100>
mov    -0x10(%rbp),%rax
mov    0x2d61(%rip),%rcx
mov    (%rcx),%rcx
cmpq   $0x0,0x8(%rax,%rcx,8)
je     1290 <main+0x100>
mov    -0x10(%rbp),%rax
mov    0x2d47(%rip),%rcx
mov    (%rcx),%rcx
cmpq   $0x0,0x10(%rax,%rcx,8)
je     12ac <main+0x11c>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
call   1070 <usage@plt>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x2d11(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2cfb(%rip),%rcx
mov    (%rcx),%rcx
mov    0x8(%rax,%rcx,8),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <get_size@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1350 <main+0x1c0>
mov    0x2ca2(%rip),%rax
mov    (%rax),%eax
mov    0x2cd1(%rip),%rcx
or     (%rcx),%eax
mov    0x2ca8(%rip),%rcx
or     (%rcx),%eax
mov    0x2ccf(%rip),%rcx
or     (%rcx),%eax
mov    0x2c8e(%rip),%rcx
or     (%rcx),%eax
mov    0x2c5d(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2c55(%rip),%rax
mov    (%rax),%esi
mov    0x2cbc(%rip),%rax
or     (%rax),%esi
mov    -0x34(%rbp),%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x30(%rbp)
jmp    1367 <main+0x1d7>
mov    -0x18(%rbp),%rdi
mov    0x2c2d(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    138b <main+0x1fb>
lea    0xcad(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c1c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x30(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    13b9 <main+0x229>
lea    0xc8b(%rip),%rdi
call   1090 <perror@plt>
mov    0x2bee(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebp
mov    $0x1,%cl
lea    0xe5b(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ecx,%ebx
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   10a0 <getopt@plt>
xor    %ecx,%ecx
cmp    $0x63,%eax
je     11b0 <main+0x20>
cmp    $0xffffffff,%eax
jne    1293 <main+0x103>
mov    0x2df2(%rip),%rax
mov    (%rax),%rax
mov    (%r15,%rax,8),%r14
test   %r14,%r14
je     12d2 <main+0x142>
mov    0x8(%r15,%rax,8),%rdi
test   %rdi,%rdi
je     12d2 <main+0x142>
cmpq   $0x0,0x10(%r15,%rax,8)
jne    12d2 <main+0x142>
call   1030 <get_size@plt>
mov    %eax,%ebp
test   $0x1,%bl
je     1221 <main+0x91>
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1080 <open@plt>
jmp    1273 <main+0xe3>
mov    0x2d70(%rip),%rax
mov    0x2da1(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d76(%rip),%rax
or     (%rax),%edx
mov    0x2d9d(%rip),%rax
or     (%rax),%edx
mov    0x2d5c(%rip),%rax
or     (%rax),%edx
mov    0x2d2b(%rip),%rax
or     (%rax),%edx
mov    0x2d2a(%rip),%rax
mov    0x2d93(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12dd <main+0x14d>
mov    %eax,%edi
mov    %ebp,%esi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     12eb <main+0x15b>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0x3f,%eax
jne    12cb <main+0x13b>
mov    0x2d19(%rip),%rbx
mov    (%rbx),%edi
mov    0x2d30(%rip),%rax
mov    (%rax),%edx
lea    0xd51(%rip),%rsi
call   1060 <fprintf@plt>
mov    (%rbx),%edi
call   1070 <usage@plt>
mov    0x2cdc(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
xor    %eax,%eax
call   1040 <abort@plt>
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
jmp    12b8 <main+0x128>
lea    0xd41(%rip),%rdi
call   1090 <perror@plt>
jmp    12bd <main+0x12d>
lea    0xd3f(%rip),%rdi
call   1090 <perror@plt>
jmp    12bd <main+0x12d>

LLVM IR (-O0):
@.str = [2 x i8] c"c\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"trunc: Unrecognized option `%c'.\0A\0A\00"
@optopt = global i32 0
@EXIT_FAILURE = global i32 0
@optind = global i64 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.2 = [12 x i8] c"trunc: open\00"
@.str.3 = [16 x i8] c"trunc: truncate\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
br label %13
13: ; preds = %31, %2
%14 = load i32, i32* %4
%15 = load i8**, i8*** %5
%16 = call i32 @getopt(i32 %14, i8** %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 %16, i32* %10
%17 = icmp ne i32 %16, -1
br i1 %17, label %18, label %32
18: ; preds = %13
%19 = load i32, i32* %10
switch i32 %19, label %29 [
i32 99, label %20
i32 63, label %21
]
20: ; preds = %18
store i32 1, i32* %9
br label %31
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = load i32, i32* @optopt
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = load i32, i32* @stderr
%26 = call i32 @usage(i32 %25)
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %18
%30 = call i32 (...) @abort()
unreachable
31: ; preds = %20
br label %13
32: ; preds = %13
%33 = load i8**, i8*** %5
%34 = load i64, i64* @optind
%35 = getelementptr inbounds i8*, i8** %33, i64 %34
%36 = load i8*, i8** %35
%37 = icmp eq i8* %36, null
br i1 %37, label %52, label %38
38: ; preds = %32
%39 = load i8**, i8*** %5
%40 = load i64, i64* @optind
%41 = add i64 %40, 1
%42 = getelementptr inbounds i8*, i8** %39, i64 %41
%43 = load i8*, i8** %42
%44 = icmp eq i8* %43, null
br i1 %44, label %52, label %45
45: ; preds = %38
%46 = load i8**, i8*** %5
%47 = load i64, i64* @optind
%48 = add i64 %47, 2
%49 = getelementptr inbounds i8*, i8** %46, i64 %48
%50 = load i8*, i8** %49
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %57
52: ; preds = %45, %38, %32
%53 = load i32, i32* @stderr
%54 = call i32 @usage(i32 %53)
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %45
%58 = load i8**, i8*** %5
%59 = load i64, i64* @optind
%60 = getelementptr inbounds i8*, i8** %58, i64 %59
%61 = load i8*, i8** %60
store i8* %61, i8** %6
%62 = load i8**, i8*** %5
%63 = load i64, i64* @optind
%64 = add i64 %63, 1
%65 = getelementptr inbounds i8*, i8** %62, i64 %64
%66 = load i8*, i8** %65
store i8* %66, i8** %7
%67 = load i8*, i8** %7
%68 = call i32 @get_size(i8* %67)
store i32 %68, i32* %8
%69 = load i32, i32* %9
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %89
71: ; preds = %57
%72 = load i32, i32* @S_IRUSR
%73 = load i32, i32* @S_IWUSR
%74 = or i32 %72, %73
%75 = load i32, i32* @S_IRGRP
%76 = or i32 %74, %75
%77 = load i32, i32* @S_IWGRP
%78 = or i32 %76, %77
%79 = load i32, i32* @S_IROTH
%80 = or i32 %78, %79
%81 = load i32, i32* @S_IWOTH
%82 = or i32 %80, %81
store i32 %82, i32* %12
%83 = load i8*, i8** %6
%84 = load i32, i32* @O_WRONLY
%85 = load i32, i32* @O_CREAT
%86 = or i32 %84, %85
%87 = load i32, i32* %12
%88 = call i32 (i8*, i32, ...) @open(i8* %83, i32 %86, i32 %87)
store i32 %88, i32* %11
br label %93
89: ; preds = %57
%90 = load i8*, i8** %6
%91 = load i32, i32* @O_WRONLY
%92 = call i32 (i8*, i32, ...) @open(i8* %90, i32 %91)
store i32 %92, i32* %11
br label %93
93: ; preds = %89, %71
%94 = load i32, i32* %11
%95 = icmp eq i32 %94, -1
br i1 %95, label %96, label %100
96: ; preds = %93
%97 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%98 = load i32, i32* @EXIT_FAILURE
%99 = call i32 @exit(i32 %98)
unreachable
100: ; preds = %93
%101 = load i32, i32* %11
%102 = load i32, i32* %8
%103 = call i32 @ftruncate(i32 %101, i32 %102)
%104 = icmp eq i32 %103, -1
br i1 %104, label %105, label %109
105: ; preds = %100
%106 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%107 = load i32, i32* @EXIT_FAILURE
%108 = call i32 @exit(i32 %107)
unreachable
109: ; preds = %100
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"c\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"trunc: Unrecognized option `%c'.\0A\0A\00"
@optopt = global i32 0
@EXIT_FAILURE = global i32 0
@optind = global i64 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.2 = [12 x i8] c"trunc: open\00"
@.str.3 = [16 x i8] c"trunc: truncate\00"
define i32 @main(i32 %0, i8** %1) {
br label %3
3: ; preds = %3, %2
%4 = phi i1 [ true, %2 ], [ false, %3 ]
%5 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
switch i32 %5, label %14 [
i32 -1, label %16
i32 99, label %3
i32 63, label %6
]
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = load i32, i32* @optopt
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @stderr
%11 = tail call i32 @usage(i32 %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %3
%15 = tail call i32 (...) @abort()
unreachable
16: ; preds = %3
%17 = load i64, i64* @optind
%18 = getelementptr inbounds i8*, i8** %1, i64 %17
%19 = load i8*, i8** %18
%20 = icmp eq i8* %19, null
br i1 %20, label %31, label %21
21: ; preds = %16
%22 = add i64 %17, 1
%23 = getelementptr inbounds i8*, i8** %1, i64 %22
%24 = load i8*, i8** %23
%25 = icmp eq i8* %24, null
br i1 %25, label %31, label %26
26: ; preds = %21
%27 = add i64 %17, 2
%28 = getelementptr inbounds i8*, i8** %1, i64 %27
%29 = load i8*, i8** %28
%30 = icmp eq i8* %29, null
br i1 %30, label %36, label %31
31: ; preds = %26, %21, %16
%32 = load i32, i32* @stderr
%33 = tail call i32 @usage(i32 %32)
%34 = load i32, i32* @EXIT_FAILURE
%35 = tail call i32 @exit(i32 %34)
unreachable
36: ; preds = %26
%37 = tail call i32 @get_size(i8* %24)
br i1 %4, label %54, label %38
38: ; preds = %36
%39 = load i32, i32* @S_IRUSR
%40 = load i32, i32* @S_IWUSR
%41 = or i32 %40, %39
%42 = load i32, i32* @S_IRGRP
%43 = or i32 %41, %42
%44 = load i32, i32* @S_IWGRP
%45 = or i32 %43, %44
%46 = load i32, i32* @S_IROTH
%47 = or i32 %45, %46
%48 = load i32, i32* @S_IWOTH
%49 = or i32 %47, %48
%50 = load i32, i32* @O_WRONLY
%51 = load i32, i32* @O_CREAT
%52 = or i32 %51, %50
%53 = tail call i32 (i8*, i32, ...) @open(i8* %19, i32 %52, i32 %49)
br label %57
54: ; preds = %36
%55 = load i32, i32* @O_WRONLY
%56 = tail call i32 (i8*, i32, ...) @open(i8* %19, i32 %55)
br label %57
57: ; preds = %54, %38
%58 = phi i32 [ %53, %38 ], [ %56, %54 ]
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %64
60: ; preds = %57
%61 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%62 = load i32, i32* @EXIT_FAILURE
%63 = tail call i32 @exit(i32 %62)
unreachable
64: ; preds = %57
%65 = tail call i32 @ftruncate(i32 %58, i32 %37)
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %71
67: ; preds = %64
%68 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%69 = load i32, i32* @EXIT_FAILURE
%70 = tail call i32 @exit(i32 %69)
unreachable
71: ; preds = %64
ret i32 0
}