#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 */
struct stat {
  scalar_t__ st_size;
};
typedef scalar_t__ 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*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fstat(int, struct stat *);
char *malloc(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pread(int, char *, int, scalar_t__);
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;
  }
  struct stat fst;
  fstat(fd_in, &fst);
  offset = fst.st_size - 1;
  while (offset >= 0) {
    pread(fd_in, &c, 1, offset);
    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    $0x40,%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   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    121a <main+0x6a>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdff(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136b <main+0x1bb>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    125b <main+0xab>
lea    0xdce(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136b <main+0x1bb>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
add    $0x4,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdaa(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d09(%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   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    12f5 <main+0x145>
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xd61(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2cd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136b <main+0x1bb>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10d0 <fstat@plt>
mov    -0x38(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jl     134f <main+0x19f>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rcx
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   10b0 <pread@plt>
mov    -0x18(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    130d <main+0x15d>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    0x2c52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%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    12c7 <main+0x117>
mov    0x8(%rbp),%rbx
lea    0xe2d(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %eax,%eax
je     12c7 <main+0x117>
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     130b <main+0x15b>
mov    %eax,%r15d
mov    0x8(%rbp),%rdi
call   1040 <strlen@plt>
lea    0x4(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    0x8(%rbp),%rdx
lea    0xe18(%rip),%rsi
mov    %rax,%rdi
call   10c0 <sprintf@plt>
mov    0x2d78(%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   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12f6 <main+0x146>
mov    %eax,%r12d
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
call   10d0 <fstat@plt>
mov    0x8(%rsp),%rbp
test   %rbp,%rbp
jle    12ae <main+0xfe>
add    $0x1,%rbp
lea    0x7(%rsp),%rbx
nop
lea    -0x2(%rbp),%rcx
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   10b0 <pread@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
add    $0xffffffffffffffff,%rbp
cmp    $0x1,%rbp
ja     1280 <main+0xd0>
mov    %r15d,%edi
call   1050 <close@plt>
mov    %r12d,%edi
call   1050 <close@plt>
mov    0x2cf3(%rip),%rax
jmp    12e7 <main+0x137>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xd28(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2cd9(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
lea    0xd3a(%rip),%rsi
mov    %r14,%rdx
jmp    12db <main+0x12b>
lea    0xd01(%rip),%rdi
call   10a0 <perror@plt>
jmp    12e0 <main+0x130>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.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"
@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 i64
%9 = alloca i8*
%10 = alloca i8
%11 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %20, label %14
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @strcmp(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%19 = icmp ne i32 %18, 0
br i1 %19, label %27, label %20
20: ; preds = %14, %2
%21 = load i32, i32* @stderr
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @EXIT_FAILURE
store i32 %26, i32* %3
br label %88
27: ; preds = %14
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = load i32, i32* @O_RDONLY
%32 = call i32 (i8*, i32, ...) @open(i8* %30, i32 %31)
store i32 %32, i32* %6
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
store i32 %36, i32* %3
br label %88
37: ; preds = %27
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = call i32 @strlen(i8* %40)
%42 = add nsw i32 %41, 4
%43 = sext i32 %42 to i64
%44 = mul i64 1, %43
%45 = trunc i64 %44 to i32
%46 = call i8* @malloc(i32 %45)
store i8* %46, i8** %9
%47 = load i8*, i8** %9
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = call i32 @sprintf(i8* %47, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %50)
%52 = load i8*, i8** %9
%53 = load i32, i32* @O_WRONLY
%54 = load i32, i32* @O_CREAT
%55 = or i32 %53, %54
%56 = load i32, i32* @O_TRUNC
%57 = or i32 %55, %56
%58 = call i32 (i8*, i32, ...) @open(i8* %52, i32 %57, i32 420)
store i32 %58, i32* %7
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %65
60: ; preds = %37
%61 = load i32, i32* @stderr
%62 = load i8*, i8** %9
%63 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %62)
%64 = load i32, i32* @EXIT_FAILURE
store i32 %64, i32* %3
br label %88
65: ; preds = %37
%66 = load i32, i32* %6
%67 = call i32 @fstat(i32 %66, %struct.stat* %11)
%68 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%69 = load i64, i64* %68
%70 = sub nsw i64 %69, 1
store i64 %70, i64* %8
br label %71
71: ; preds = %74, %65
%72 = load i64, i64* %8
%73 = icmp sge i64 %72, 0
br i1 %73, label %74, label %82
74: ; preds = %71
%75 = load i32, i32* %6
%76 = load i64, i64* %8
%77 = call i32 @pread(i32 %75, i8* %10, i32 1, i64 %76)
%78 = load i32, i32* %7
%79 = call i32 @write(i32 %78, i8* %10, i32 1)
%80 = load i64, i64* %8
%81 = add nsw i64 %80, -1
store i64 %81, i64* %8
br label %71
82: ; preds = %71
%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, %60, %34, %20
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.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"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = alloca %struct.stat
%5 = icmp eq i32 %0, 2
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @strcmp(i8* dereferenceable(1) %8, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %6, %2
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %1
%14 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
br label %57
16: ; preds = %6
%17 = load i32, i32* @O_RDONLY
%18 = tail call i32 (i8*, i32, ...) @open(i8* %8, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = tail call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
br label %57
23: ; preds = %16
%24 = load i8*, i8** %7
%25 = tail call i32 @strlen(i8* %24)
%26 = add nsw i32 %25, 4
%27 = tail call i8* @malloc(i32 %26)
%28 = load i8*, i8** %7
%29 = tail call i32 @sprintf(i8* dereferenceable(1) %27, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @O_WRONLY
%31 = load i32, i32* @O_CREAT
%32 = or i32 %31, %30
%33 = load i32, i32* @O_TRUNC
%34 = or i32 %32, %33
%35 = tail call i32 (i8*, i32, ...) @open(i8* %27, i32 %34, i32 420)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %41
37: ; preds = %23
%38 = load i32, i32* @stderr
%39 = tail call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %27)
%40 = load i32, i32* @EXIT_FAILURE
br label %57
41: ; preds = %23
%42 = bitcast %struct.stat* %4 to i8*
%43 = call i32 @fstat(i32 %18, %struct.stat* %4)
%44 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%45 = load i64, i64* %44
%46 = icmp sgt i64 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %41, %47
%48 = phi i64 [ %49, %47 ], [ %45, %41 ]
%49 = add nsw i64 %48, -1
%50 = call i32 @pread(i32 %18, i8* %3, i32 1, i64 %49)
%51 = call i32 @write(i32 %35, i8* %3, i32 1)
%52 = icmp ugt i64 %48, 1
br i1 %52, label %47, label %53
53: ; preds = %47, %41
%54 = tail call i32 @close(i32 %18)
%55 = tail call i32 @close(i32 %35)
%56 = load i32, i32* @EXIT_SUCCESS
br label %57
57: ; preds = %53, %37, %20, %11
%58 = phi i32 [ %15, %11 ], [ %22, %20 ], [ %40, %37 ], [ %56, %53 ]
ret i32 %58
}