#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 {
  int st_size;
};

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fstat(int, struct stat *);
int *malloc(int);
int open(char *, int, ...);
int read(int, int *, int);
int /*<<< orphan*/ stderr;
int write(int, int *, int);
int main(int argc, char *argv[]) {
  char *infile;
  char *outfile;
  int fd;
  int ofd;
  int check;
  int size;
  struct stat info;
  int *p;
  int count;
  int buf;
  int i;
  int j;
  int rc;
  if (argc != 3) {
    fprintf(stderr, "usage: intrev <inputfile> <outputfile>\n");
    exit(1);
  }
  infile = argv[1];
  outfile = argv[2];
  fd = open(infile, O_RDONLY);
  ofd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
  assert(fd > -1);
  assert(ofd > -1);
  check = fstat(fd, &info);
  assert(check == 0);
  size = info.st_size;
  p = malloc(size);
  i = 0;
  j = 0;
  count = read(fd, &buf, 4);
  while (count == 4) {
    p[i] = buf;
    i++;
    count = read(fd, &buf, 4);
  }
  for (j = i - 1; j >= 0; j--) {
    rc = write(ofd, &(p[j]), sizeof(int));
    assert(rc == sizeof(int));
  }
  close(ofd);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11cf <main+0x3f>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    0x2de6(%rip),%rax
or     (%rax),%esi
mov    0x2dd5(%rip),%rax
or     (%rax),%esi
mov    0x2db4(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpl   $0xffffffff,-0x28(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x24(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10b0 <fstat@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x4c(%rbp)
movl   $0x0,-0x50(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x4,-0x44(%rbp)
jne    12e0 <main+0x150>
mov    -0x48(%rbp),%edx
mov    -0x40(%rbp),%rax
movslq -0x4c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    %eax,-0x44(%rbp)
jmp    12a6 <main+0x116>
mov    -0x4c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x50(%rbp)
cmpl   $0x0,-0x50(%rbp)
jl     1335 <main+0x1a5>
mov    -0x28(%rbp),%edi
mov    -0x40(%rbp),%rsi
movslq -0x50(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,-0x54(%rbp)
movslq -0x54(%rbp),%rax
cmp    $0x4,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x50(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x50(%rbp)
jmp    12e9 <main+0x159>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x3,%edi
jne    12d2 <main+0x142>
mov    0x8(%rsi),%rdi
mov    0x10(%rsi),%rbx
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%r15d
mov    0x2de9(%rip),%rax
mov    0x2e2a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e17(%rip),%rax
or     (%rax),%esi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%r14d
mov    %r15d,%edi
not    %edi
shr    $0x1f,%edi
call   1060 <assert@plt>
mov    %r14d,%edi
not    %edi
shr    $0x1f,%edi
call   1060 <assert@plt>
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
call   10b0 <fstat@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1060 <assert@plt>
mov    0x8(%rsp),%edi
call   1080 <malloc@plt>
mov    %rax,%rbp
lea    0x4(%rsp),%rsi
mov    %r15d,%edi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x4,%eax
jne    12b3 <main+0x123>
xor    %ebx,%ebx
lea    0x4(%rsp),%r12
nop
mov    0x4(%rsp),%eax
mov    %eax,0x0(%rbp,%rbx,4)
add    $0x1,%rbx
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x4,%eax
je     1250 <main+0xc0>
test   %ebx,%ebx
jle    12b3 <main+0x123>
mov    %ebx,%ebx
add    $0x1,%rbx
nopl   0x0(%rax,%rax,1)
lea    -0x2(%rbx),%eax
lea    0x0(,%rax,4),%rsi
add    %rbp,%rsi
mov    %r14d,%edi
mov    $0x4,%edx
call   1030 <write@plt>
xor    %edi,%edi
cmp    $0x4,%eax
sete   %dil
call   1060 <assert@plt>
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     1280 <main+0xf0>
mov    %r14d,%edi
call   1040 <close@plt>
mov    %r15d,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
lea    0xd1e(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [40 x i8] c"usage: intrev <inputfile> <outputfile>\0A\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
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 %struct.stat
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%19 = load i32, i32* %4
%20 = icmp ne i32 %19, 3
br i1 %20, label %21, label %25
21: ; preds = %2
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
store i8* %28, i8** %6
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
store i8* %31, i8** %7
%32 = load i8*, i8** %6
%33 = load i32, i32* @O_RDONLY
%34 = call i32 (i8*, i32, ...) @open(i8* %32, i32 %33)
store i32 %34, i32* %8
%35 = load i8*, i8** %7
%36 = load i32, i32* @O_WRONLY
%37 = load i32, i32* @O_CREAT
%38 = or i32 %36, %37
%39 = load i32, i32* @O_TRUNC
%40 = or i32 %38, %39
%41 = load i32, i32* @S_IRWXU
%42 = call i32 (i8*, i32, ...) @open(i8* %35, i32 %40, i32 %41)
store i32 %42, i32* %9
%43 = load i32, i32* %8
%44 = icmp sgt i32 %43, -1
%45 = zext i1 %44 to i32
%46 = call i32 @assert(i32 %45)
%47 = load i32, i32* %9
%48 = icmp sgt i32 %47, -1
%49 = zext i1 %48 to i32
%50 = call i32 @assert(i32 %49)
%51 = load i32, i32* %8
%52 = call i32 @fstat(i32 %51, %struct.stat* %12)
store i32 %52, i32* %10
%53 = load i32, i32* %10
%54 = icmp eq i32 %53, 0
%55 = zext i1 %54 to i32
%56 = call i32 @assert(i32 %55)
%57 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%58 = load i32, i32* %57
store i32 %58, i32* %11
%59 = load i32, i32* %11
%60 = call i32* @malloc(i32 %59)
store i32* %60, i32** %13
store i32 0, i32* %16
store i32 0, i32* %17
%61 = load i32, i32* %8
%62 = call i32 @read(i32 %61, i32* %15, i32 4)
store i32 %62, i32* %14
br label %63
63: ; preds = %66, %25
%64 = load i32, i32* %14
%65 = icmp eq i32 %64, 4
br i1 %65, label %66, label %76
66: ; preds = %63
%67 = load i32, i32* %15
%68 = load i32*, i32** %13
%69 = load i32, i32* %16
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i32, i32* %68, i64 %70
store i32 %67, i32* %71
%72 = load i32, i32* %16
%73 = add nsw i32 %72, 1
store i32 %73, i32* %16
%74 = load i32, i32* %8
%75 = call i32 @read(i32 %74, i32* %15, i32 4)
store i32 %75, i32* %14
br label %63
76: ; preds = %63
%77 = load i32, i32* %16
%78 = sub nsw i32 %77, 1
store i32 %78, i32* %17
br label %79
79: ; preds = %94, %76
%80 = load i32, i32* %17
%81 = icmp sge i32 %80, 0
br i1 %81, label %82, label %97
82: ; preds = %79
%83 = load i32, i32* %9
%84 = load i32*, i32** %13
%85 = load i32, i32* %17
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i32, i32* %84, i64 %86
%88 = call i32 @write(i32 %83, i32* %87, i32 4)
store i32 %88, i32* %18
%89 = load i32, i32* %18
%90 = sext i32 %89 to i64
%91 = icmp eq i64 %90, 4
%92 = zext i1 %91 to i32
%93 = call i32 @assert(i32 %92)
br label %94
94: ; preds = %82
%95 = load i32, i32* %17
%96 = add nsw i32 %95, -1
store i32 %96, i32* %17
br label %79
97: ; preds = %79
%98 = load i32, i32* %9
%99 = call i32 @close(i32 %98)
%100 = load i32, i32* %8
%101 = call i32 @close(i32 %100)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [40 x i8] c"usage: intrev <inputfile> <outputfile>\0A\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca i32
%5 = bitcast %struct.stat* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = icmp eq i32 %0, 3
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDONLY
%18 = tail call i32 (i8*, i32, ...) @open(i8* %14, i32 %17)
%19 = load i32, i32* @O_WRONLY
%20 = load i32, i32* @O_CREAT
%21 = or i32 %20, %19
%22 = load i32, i32* @O_TRUNC
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRWXU
%25 = tail call i32 (i8*, i32, ...) @open(i8* %16, i32 %23, i32 %24)
%26 = xor i32 %18, -1
%27 = lshr i32 %26, 31
%28 = tail call i32 @assert(i32 %27)
%29 = xor i32 %25, -1
%30 = lshr i32 %29, 31
%31 = tail call i32 @assert(i32 %30)
%32 = call i32 @fstat(i32 %18, %struct.stat* %3)
%33 = icmp eq i32 %32, 0
%34 = zext i1 %33 to i32
%35 = tail call i32 @assert(i32 %34)
%36 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%37 = load i32, i32* %36
%38 = tail call i32* @malloc(i32 %37)
%39 = call i32 @read(i32 %18, i32* %4, i32 4)
%40 = icmp eq i32 %39, 4
br i1 %40, label %46, label %63
41: ; preds = %46
%42 = trunc i64 %50 to i32
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %63
44: ; preds = %41
%45 = and i64 %50, 4294967295
br label %53
46: ; preds = %12, %46
%47 = phi i64 [ %50, %46 ], [ 0, %12 ]
%48 = load i32, i32* %4
%49 = getelementptr inbounds i32, i32* %38, i64 %47
store i32 %48, i32* %49
%50 = add nuw i64 %47, 1
%51 = call i32 @read(i32 %18, i32* %4, i32 4)
%52 = icmp eq i32 %51, 4
br i1 %52, label %46, label %41
53: ; preds = %44, %53
%54 = phi i64 [ %45, %44 ], [ %55, %53 ]
%55 = add nsw i64 %54, -1
%56 = and i64 %55, 4294967295
%57 = getelementptr inbounds i32, i32* %38, i64 %56
%58 = tail call i32 @write(i32 %25, i32* %57, i32 4)
%59 = icmp eq i32 %58, 4
%60 = zext i1 %59 to i32
%61 = tail call i32 @assert(i32 %60)
%62 = icmp ugt i64 %54, 1
br i1 %62, label %53, label %63
63: ; preds = %53, %12, %41
%64 = tail call i32 @close(i32 %25)
%65 = tail call i32 @close(i32 %18)
ret i32 0
}