#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 /*<<< orphan*/ printf(char *, int);
int read(int, int *, int);
int /*<<< orphan*/ stderr;
int write(int, int *, int);
int main(int argc, char *argv[]) {
  if (argc != 3) {
    fprintf(stderr, "usage: intrev <inputfile> <outputfile>\n");
    exit(1);
  }
  char *infile;
  char *outfile;
  infile = argv[1];
  outfile = argv[2];
  int fd = open(infile, O_RDONLY);
  int ofd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
  assert(fd > -1);
  struct stat *info;
  int check = fstat(fd, info);
  assert(check == 0);
  int size = info->st_size;
  int *p = malloc(size);
  int count;
  int buf;
  int i = 0;
  int j = 0;
  count = read(fd, &buf, 4);
  while (count == 4) {
    p[i] = buf;
    i++;
    printf("%d\n", buf);
    count = read(fd, &buf, 4);
  }
  for (j = i - 1; j >= 0; j--) {
    int 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     11df <main+0x3f>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <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    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
mov    0x2dd6(%rip),%rax
or     (%rax),%esi
mov    0x2dc5(%rip),%rax
or     (%rax),%esi
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rsi
call   10c0 <fstat@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%edi
call   1090 <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   1060 <read@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x4,-0x44(%rbp)
jne    12f1 <main+0x151>
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    -0x48(%rbp),%esi
lea    0xd55(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    %eax,-0x44(%rbp)
jmp    12a8 <main+0x108>
mov    -0x4c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x50(%rbp)
cmpl   $0x0,-0x50(%rbp)
jl     1346 <main+0x1a6>
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   1070 <assert@plt>
mov    -0x50(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x50(%rbp)
jmp    12fa <main+0x15a>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    12f4 <main+0x154>
mov    0x8(%rsi),%rdi
mov    0x10(%rsi),%rbx
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   10a0 <open@plt>
mov    %eax,%r15d
mov    0x2dda(%rip),%rax
mov    0x2e1b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e08(%rip),%rax
or     (%rax),%esi
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   10a0 <open@plt>
mov    %eax,%r14d
mov    %r15d,%edi
not    %edi
shr    $0x1f,%edi
call   1070 <assert@plt>
mov    %r15d,%edi
call   10c0 <fstat@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1070 <assert@plt>
call   1090 <malloc@plt>
mov    %rax,%rbp
lea    0x4(%rsp),%rsi
mov    %r15d,%edi
mov    $0x4,%edx
call   1060 <read@plt>
cmp    $0x4,%eax
jne    12d3 <main+0x133>
xor    %ebx,%ebx
lea    0xdda(%rip),%r12
lea    0x4(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x4(%rsp),%esi
mov    %esi,0x0(%rbp,%rbx,4)
add    $0x1,%rbx
mov    %r12,%rdi
call   1040 <printf@plt>
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
cmp    $0x4,%eax
je     1260 <main+0xc0>
test   %ebx,%ebx
jle    12d3 <main+0x133>
mov    %ebx,%ebx
add    $0x1,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
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   1070 <assert@plt>
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     12a0 <main+0x100>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %r15d,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cc5(%rip),%rax
mov    (%rax),%edi
lea    0xcfc(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <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
@.str.1 = [4 x i8] c"%d\0A\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 %struct.stat*
%11 = alloca i32
%12 = alloca i32
%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* %8
%48 = load %struct.stat*, %struct.stat** %10
%49 = call i32 @fstat(i32 %47, %struct.stat* %48)
store i32 %49, i32* %11
%50 = load i32, i32* %11
%51 = icmp eq i32 %50, 0
%52 = zext i1 %51 to i32
%53 = call i32 @assert(i32 %52)
%54 = load %struct.stat*, %struct.stat** %10
%55 = getelementptr inbounds %struct.stat, %struct.stat* %54, i32 0, i32 0
%56 = load i32, i32* %55
store i32 %56, i32* %12
%57 = load i32, i32* %12
%58 = call i32* @malloc(i32 %57)
store i32* %58, i32** %13
store i32 0, i32* %16
store i32 0, i32* %17
%59 = load i32, i32* %8
%60 = call i32 @read(i32 %59, i32* %15, i32 4)
store i32 %60, i32* %14
br label %61
61: ; preds = %64, %25
%62 = load i32, i32* %14
%63 = icmp eq i32 %62, 4
br i1 %63, label %64, label %76
64: ; preds = %61
%65 = load i32, i32* %15
%66 = load i32*, i32** %13
%67 = load i32, i32* %16
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i32, i32* %66, i64 %68
store i32 %65, i32* %69
%70 = load i32, i32* %16
%71 = add nsw i32 %70, 1
store i32 %71, i32* %16
%72 = load i32, i32* %15
%73 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %72)
%74 = load i32, i32* %8
%75 = call i32 @read(i32 %74, i32* %15, i32 4)
store i32 %75, i32* %14
br label %61
76: ; preds = %61
%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
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp eq i32 %0, 3
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 (i8*, i32, ...) @open(i8* %11, i32 %14)
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_CREAT
%18 = or i32 %17, %16
%19 = load i32, i32* @O_TRUNC
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IRWXU
%22 = tail call i32 (i8*, i32, ...) @open(i8* %13, i32 %20, i32 %21)
%23 = xor i32 %15, -1
%24 = lshr i32 %23, 31
%25 = tail call i32 @assert(i32 %24)
%26 = tail call i32 @fstat(i32 %15, %struct.stat* undef)
%27 = icmp eq i32 %26, 0
%28 = zext i1 %27 to i32
%29 = tail call i32 @assert(i32 %28)
%30 = tail call i32* @malloc(i32 undef)
%31 = bitcast i32* %3 to i8*
%32 = call i32 @read(i32 %15, i32* %3, i32 4)
%33 = icmp eq i32 %32, 4
br i1 %33, label %39, label %57
34: ; preds = %39
%35 = trunc i64 %43 to i32
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %57
37: ; preds = %34
%38 = and i64 %43, 4294967295
br label %47
39: ; preds = %9, %39
%40 = phi i64 [ %43, %39 ], [ 0, %9 ]
%41 = load i32, i32* %3
%42 = getelementptr inbounds i32, i32* %30, i64 %40
store i32 %41, i32* %42
%43 = add nuw i64 %40, 1
%44 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %41)
%45 = call i32 @read(i32 %15, i32* %3, i32 4)
%46 = icmp eq i32 %45, 4
br i1 %46, label %39, label %34
47: ; preds = %37, %47
%48 = phi i64 [ %38, %37 ], [ %49, %47 ]
%49 = add nsw i64 %48, -1
%50 = and i64 %49, 4294967295
%51 = getelementptr inbounds i32, i32* %30, i64 %50
%52 = tail call i32 @write(i32 %22, i32* %51, i32 4)
%53 = icmp eq i32 %52, 4
%54 = zext i1 %53 to i32
%55 = tail call i32 @assert(i32 %54)
%56 = icmp ugt i64 %48, 1
br i1 %56, label %47, label %57
57: ; preds = %47, %9, %34
%58 = tail call i32 @close(i32 %22)
%59 = tail call i32 @close(i32 %15)
ret i32 0
}