#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 off_t;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int lseek(int, int, int /*<<< orphan*/);
char *malloc(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd_in, fd_out;
  off_t offset;
  char *out_name;
  char c;
  if (argc != 2 || !strcmp(argv[1], "-h")) {
    fprintf(stderr, "usage: %s FILE\n", argv[0]);
    return EXIT_FAILURE;
  }
  if ((fd_in = open(argv[1], O_RDONLY)) == -1) {
    perror("unable to open input file for reading");
    return EXIT_FAILURE;
  }
  out_name = malloc(sizeof(char) * (strlen(argv[1]) + 4));
  sprintf(out_name, "%s.inv", argv[1]);
  if ((fd_out = open(out_name, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) {
    fprintf(stderr, "unable to create and open %s for writing", out_name);
    return EXIT_FAILURE;
  }
  offset = lseek(fd_in, -1, SEEK_END);
  while (offset >= 0) {
    offset = lseek(fd_in, offset, SEEK_SET);
    read(fd_in, &c, 1);
    write(fd_out, &c, 1);
    offset--;
  }
  close(fd_in);
  close(fd_out);
  return EXIT_SUCCESS;
}


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)
cmpl   $0x2,-0x8(%rbp)
jne    11ed <main+0x3d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe21(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    121a <main+0x6a>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdff(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    137b <main+0x1cb>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    125b <main+0xab>
lea    0xdce(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    137b <main+0x1cb>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
add    $0x4,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdaa(%rip),%rsi
call   10d0 <sprintf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2cf9(%rip),%rax
mov    (%rax),%esi
mov    0x2d50(%rip),%rax
or     (%rax),%esi
mov    0x2d3f(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   10b0 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    12f5 <main+0x145>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xd61(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2cc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    137b <main+0x1cb>
mov    -0x14(%rbp),%edi
mov    0x2cd9(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%esi
call   1050 <lseek@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     135f <main+0x1af>
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    0x2c93(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    -0x18(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    130e <main+0x15e>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    0x2c32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
cmp    $0x2,%edi
jne    12dd <main+0x12d>
mov    0x8(%rbp),%rbx
lea    0xe2d(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcmp@plt>
test   %eax,%eax
je     12dd <main+0x12d>
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     1321 <main+0x171>
mov    %eax,%r12d
mov    0x8(%rbp),%rdi
call   1040 <strlen@plt>
lea    0x4(%rax),%edi
call   10a0 <malloc@plt>
mov    %rax,%r14
mov    0x8(%rbp),%rdx
lea    0xe18(%rip),%rsi
mov    %rax,%rdi
call   10d0 <sprintf@plt>
mov    0x2d68(%rip),%rax
mov    0x2dc1(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dae(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     130c <main+0x15c>
mov    %eax,%r15d
mov    0x2d72(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
mov    $0xffffffff,%esi
call   1050 <lseek@plt>
test   %eax,%eax
js     12c4 <main+0x114>
mov    %eax,%esi
mov    0x2d36(%rip),%r14
lea    0xf(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    (%r14),%edx
mov    %r12d,%edi
call   1050 <lseek@plt>
mov    %eax,%ebx
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
lea    -0x1(%rbx),%esi
test   %ebx,%ebx
jg     1290 <main+0xe0>
mov    %r12d,%edi
call   1060 <close@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    0x2ccd(%rip),%rax
jmp    12fd <main+0x14d>
mov    0x2cdc(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xd12(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2cb3(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
lea    0xd24(%rip),%rsi
mov    %r14,%rdx
jmp    12f1 <main+0x141>
lea    0xceb(%rip),%rdi
call   10c0 <perror@plt>
jmp    12f6 <main+0x146>

LLVM IR (-O0):
@.str = [3 x i8] c"-h\00"
@stderr = global i32 0
@.str.1 = [16 x i8] c"usage: %s FILE\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [38 x i8] c"unable to open input file for reading\00"
@.str.3 = [7 x i8] c"%s.inv\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.4 = [41 x i8] c"unable to create and open %s for writing\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %19, label %13
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @strcmp(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%18 = icmp ne i32 %17, 0
br i1 %18, label %26, label %19
19: ; preds = %13, %2
%20 = load i32, i32* @stderr
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = load i32, i32* @EXIT_FAILURE
store i32 %25, i32* %3
br label %88
26: ; preds = %13
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = load i32, i32* @O_RDONLY
%31 = call i32 (i8*, i32, ...) @open(i8* %29, i32 %30)
store i32 %31, i32* %6
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
store i32 %35, i32* %3
br label %88
36: ; preds = %26
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = load i8*, i8** %38
%40 = call i32 @strlen(i8* %39)
%41 = add nsw i32 %40, 4
%42 = sext i32 %41 to i64
%43 = mul i64 1, %42
%44 = trunc i64 %43 to i32
%45 = call i8* @malloc(i32 %44)
store i8* %45, i8** %9
%46 = load i8*, i8** %9
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = load i8*, i8** %48
%50 = call i32 @sprintf(i8* %46, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %49)
%51 = load i8*, i8** %9
%52 = load i32, i32* @O_WRONLY
%53 = load i32, i32* @O_CREAT
%54 = or i32 %52, %53
%55 = load i32, i32* @O_TRUNC
%56 = or i32 %54, %55
%57 = call i32 (i8*, i32, ...) @open(i8* %51, i32 %56, i32 420)
store i32 %57, i32* %7
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %64
59: ; preds = %36
%60 = load i32, i32* @stderr
%61 = load i8*, i8** %9
%62 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %61)
%63 = load i32, i32* @EXIT_FAILURE
store i32 %63, i32* %3
br label %88
64: ; preds = %36
%65 = load i32, i32* %6
%66 = load i32, i32* @SEEK_END
%67 = call i32 @lseek(i32 %65, i32 -1, i32 %66)
store i32 %67, i32* %8
br label %68
68: ; preds = %71, %64
%69 = load i32, i32* %8
%70 = icmp sge i32 %69, 0
br i1 %70, label %71, label %82
71: ; preds = %68
%72 = load i32, i32* %6
%73 = load i32, i32* %8
%74 = load i32, i32* @SEEK_SET
%75 = call i32 @lseek(i32 %72, i32 %73, i32 %74)
store i32 %75, i32* %8
%76 = load i32, i32* %6
%77 = call i32 @read(i32 %76, i8* %10, i32 1)
%78 = load i32, i32* %7
%79 = call i32 @write(i32 %78, i8* %10, i32 1)
%80 = load i32, i32* %8
%81 = add nsw i32 %80, -1
store i32 %81, i32* %8
br label %68
82: ; preds = %68
%83 = load i32, i32* %6
%84 = call i32 @close(i32 %83)
%85 = load i32, i32* %7
%86 = call i32 @close(i32 %85)
%87 = load i32, i32* @EXIT_SUCCESS
store i32 %87, i32* %3
br label %88
88: ; preds = %82, %59, %33, %19
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
@.str = [3 x i8] c"-h\00"
@stderr = global i32 0
@.str.1 = [16 x i8] c"usage: %s FILE\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [38 x i8] c"unable to open input file for reading\00"
@.str.3 = [7 x i8] c"%s.inv\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.4 = [41 x i8] c"unable to create and open %s for writing\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = icmp eq i32 %0, 2
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @strcmp(i8* dereferenceable(1) %7, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %5, %2
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %1
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %12)
br label %52
14: ; preds = %5
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 (i8*, i32, ...) @open(i8* %7, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %52
20: ; preds = %14
%21 = load i8*, i8** %6
%22 = tail call i32 @strlen(i8* %21)
%23 = add nsw i32 %22, 4
%24 = tail call i8* @malloc(i32 %23)
%25 = load i8*, i8** %6
%26 = tail call i32 @sprintf(i8* dereferenceable(1) %24, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @O_WRONLY
%28 = load i32, i32* @O_CREAT
%29 = or i32 %28, %27
%30 = load i32, i32* @O_TRUNC
%31 = or i32 %29, %30
%32 = tail call i32 (i8*, i32, ...) @open(i8* %24, i32 %31, i32 420)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %20
%35 = load i32, i32* @stderr
%36 = tail call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %24)
br label %52
37: ; preds = %20
%38 = load i32, i32* @SEEK_END
%39 = tail call i32 @lseek(i32 %16, i32 -1, i32 %38)
%40 = icmp sgt i32 %39, -1
br i1 %40, label %41, label %49
41: ; preds = %37, %41
%42 = phi i32 [ %47, %41 ], [ %39, %37 ]
%43 = load i32, i32* @SEEK_SET
%44 = tail call i32 @lseek(i32 %16, i32 %42, i32 %43)
%45 = call i32 @read(i32 %16, i8* %3, i32 1)
%46 = call i32 @write(i32 %32, i8* %3, i32 1)
%47 = add nsw i32 %44, -1
%48 = icmp sgt i32 %44, 0
br i1 %48, label %41, label %49
49: ; preds = %41, %37
%50 = tail call i32 @close(i32 %16)
%51 = tail call i32 @close(i32 %32)
br label %52
52: ; preds = %49, %34, %18, %10
%53 = phi i32* [ @EXIT_FAILURE, %10 ], [ @EXIT_FAILURE, %18 ], [ @EXIT_FAILURE, %34 ], [ @EXIT_SUCCESS, %49 ]
%54 = load i32, i32* %53
ret i32 %54
}