#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*/ FILE;

/* Variables and functions */
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
size_t strcspn(char *, char *);
int strlen(char *);
int main(void) {
  FILE *fp;
  if ((fp = fopen("rosalind_revc.txt", "r")) == NULL) {
    printf("File open failed!");
    return 1;
  }
  char buf[1001];
  fgets(buf, 1001, fp);
  buf[strcspn(buf, "\n")] = '\0';
  int c;
  for (int i = 0, j = strlen(buf) - 1; i < j; i++, j--) {
    c = buf[i];
    buf[i] = buf[j];
    buf[j] = c;
  }
  printf("%s", buf);
  if (fclose(fp) != 0) {
    printf("File closed failed!");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rdi
lea    0xe92(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11ad <main+0x4d>
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c1 <main+0x161>
lea    -0x400(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x3e9,%esi
call   1070 <fgets@plt>
lea    -0x400(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1060 <strcspn@plt>
movb   $0x0,-0x400(%rbp,%rax,1)
movl   $0x0,-0x408(%rbp)
lea    -0x400(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x40c(%rbp)
mov    -0x408(%rbp),%eax
cmp    -0x40c(%rbp),%eax
jge    1278 <main+0x118>
movslq -0x408(%rbp),%rax
movsbl -0x400(%rbp,%rax,1),%eax
ff
mov    %eax,-0x404(%rbp)
movslq -0x40c(%rbp),%rax
mov    -0x400(%rbp,%rax,1),%cl
movslq -0x408(%rbp),%rax
mov    %cl,-0x400(%rbp,%rax,1)
mov    -0x404(%rbp),%eax
mov    %al,%cl
movslq -0x40c(%rbp),%rax
mov    %cl,-0x400(%rbp,%rax,1)
mov    -0x408(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x408(%rbp)
mov    -0x40c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x40c(%rbp)
jmp    11fc <main+0x9c>
lea    -0x400(%rbp),%rsi
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     12ba <main+0x15a>
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c1 <main+0x161>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x3f0,%rsp
lea    0xe8e(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1219 <main+0xb9>
mov    %rax,%r14
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x3e9,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
lea    0xe82(%rip),%rsi
mov    %rbp,%rdi
call   1060 <strcspn@plt>
movb   $0x0,(%rsp,%rax,1)
mov    %rbp,%rdi
call   1040 <strlen@plt>
cmp    $0x2,%eax
jl     11f0 <main+0x90>
mov    %eax,%eax
add    $0xfffffffffffffffe,%rax
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
nop
movzbl (%rsp,%rcx,1),%edx
movzbl 0x1(%rsp,%rax,1),%ebx
mov    %bl,(%rsp,%rcx,1)
mov    %dl,0x1(%rsp,%rax,1)
add    $0x1,%rcx
lea    -0x1(%rax),%rdx
cmp    %rax,%rcx
mov    %rdx,%rax
jl     11d0 <main+0x70>
lea    0xe31(%rip),%rdi
xor    %ebp,%ebp
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
je     122c <main+0xcc>
lea    0xe14(%rip),%rdi
jmp    1220 <main+0xc0>
lea    0xdf4(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x3f0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"rosalind_revc.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"File open failed!\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [20 x i8] c"File closed failed!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [1001 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %2
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %57
11: ; preds = %0
%12 = getelementptr inbounds [1001 x i8], [1001 x i8]* %3, i64 0, i64 0
%13 = load i32*, i32** %2
%14 = call i32 @fgets(i8* %12, i32 1001, i32* %13)
%15 = getelementptr inbounds [1001 x i8], [1001 x i8]* %3, i64 0, i64 0
%16 = call i64 @strcspn(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%17 = getelementptr inbounds [1001 x i8], [1001 x i8]* %3, i64 0, i64 %16
store i8 0, i8* %17
store i32 0, i32* %5
%18 = getelementptr inbounds [1001 x i8], [1001 x i8]* %3, i64 0, i64 0
%19 = call i32 @strlen(i8* %18)
%20 = sub nsw i32 %19, 1
store i32 %20, i32* %6
br label %21
21: ; preds = %43, %11
%22 = load i32, i32* %5
%23 = load i32, i32* %6
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %48
25: ; preds = %21
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [1001 x i8], [1001 x i8]* %3, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
store i32 %30, i32* %4
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [1001 x i8], [1001 x i8]* %3, i64 0, i64 %32
%34 = load i8, i8* %33
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [1001 x i8], [1001 x i8]* %3, i64 0, i64 %36
store i8 %34, i8* %37
%38 = load i32, i32* %4
%39 = trunc i32 %38 to i8
%40 = load i32, i32* %6
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [1001 x i8], [1001 x i8]* %3, i64 0, i64 %41
store i8 %39, i8* %42
br label %43
43: ; preds = %25
%44 = load i32, i32* %5
%45 = add nsw i32 %44, 1
store i32 %45, i32* %5
%46 = load i32, i32* %6
%47 = add nsw i32 %46, -1
store i32 %47, i32* %6
br label %21
48: ; preds = %21
%49 = getelementptr inbounds [1001 x i8], [1001 x i8]* %3, i64 0, i64 0
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %49)
%51 = load i32*, i32** %2
%52 = call i64 @fclose(i32* %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %48
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %57
56: ; preds = %48
store i32 0, i32* %1
br label %57
57: ; preds = %56, %54, %9
%58 = load i32, i32* %1
ret i32 %58
}

LLVM IR (-O2):
@.str = [18 x i8] c"rosalind_revc.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"File open failed!\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [20 x i8] c"File closed failed!\00"
define i32 @main() {
%1 = alloca [1001 x i8]
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %34
6: ; preds = %0
%7 = getelementptr inbounds [1001 x i8], [1001 x i8]* %1, i64 0, i64 0
%8 = call i32 @fgets(i8* %7, i32 1001, i32* %2)
%9 = call i64 @strcspn(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%10 = getelementptr inbounds [1001 x i8], [1001 x i8]* %1, i64 0, i64 %9
store i8 0, i8* %10
%11 = call i32 @strlen(i8* %7)
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %16
13: ; preds = %6
%14 = zext i32 %11 to i64
%15 = add nsw i64 %14, -1
br label %20
16: ; preds = %20, %6
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %7)
%18 = call i64 @fclose(i32* %2)
%19 = icmp eq i64 %18, 0
br i1 %19, label %32, label %30
20: ; preds = %13, %20
%21 = phi i64 [ 0, %13 ], [ %27, %20 ]
%22 = phi i64 [ %15, %13 ], [ %28, %20 ]
%23 = getelementptr inbounds [1001 x i8], [1001 x i8]* %1, i64 0, i64 %21
%24 = load i8, i8* %23
%25 = getelementptr inbounds [1001 x i8], [1001 x i8]* %1, i64 0, i64 %22
%26 = load i8, i8* %25
store i8 %26, i8* %23
store i8 %24, i8* %25
%27 = add nuw nsw i64 %21, 1
%28 = add nsw i64 %22, -1
%29 = icmp slt i64 %27, %28
br i1 %29, label %20, label %16
30: ; preds = %16
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %32
32: ; preds = %16, %30
%33 = phi i32 [ 1, %30 ], [ 0, %16 ]
br label %34
34: ; preds = %32, %4
%35 = phi i32 [ 1, %4 ], [ %33, %32 ]
ret i32 %35
}