#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__ check_file_type(char *, char *, unsigned char *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
void do_rename(const char *filename) {
  FILE *f;
  int a, len;
  unsigned char crc;
  unsigned char buf[130];
  char name[100], type[100];
  f = fopen(filename, "rb");
  if (f == NULL)
    return;
  len = fread(buf, 1, 130, f);
  if (len != 130) {
    printf("# i/o error reading first block of file '%s'\n", filename);
    return;
  }
  crc = 0;
  for (a = 1; a < 129; a++)
    crc += buf[a];
  if (crc != buf[129]) {
    printf("# crc error: in file %02xh, calculated %02xh\n", buf[129], crc);
    return;
  }
  if (check_file_type(name, type, buf))
    printf("mv '%s' '%s' # type = %s\n", filename, name, type);
  else
    printf("# can't determine type of file '%s'\n", filename);
}


x86 assembly (clang, -O0):
<do_rename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x180,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea6(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1173 <do_rename+0x33>
jmp    1275 <do_rename+0x135>
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x82,%edx
call   1030 <fread@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x82,-0x18(%rbp)
je     11b4 <do_rename+0x74>
mov    -0x8(%rbp),%rsi
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1275 <do_rename+0x135>
movb   $0x0,-0x19(%rbp)
movl   $0x1,-0x14(%rbp)
cmpl   $0x81,-0x14(%rbp)
jge    11ef <do_rename+0xaf>
movslq -0x14(%rbp),%rax
movzbl -0xa0(%rbp,%rax,1),%ecx
ff
movzbl -0x19(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x19(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11bf <do_rename+0x7f>
movzbl -0x19(%rbp),%eax
movzbl -0x1f(%rbp),%ecx
cmp    %ecx,%eax
je     121a <do_rename+0xda>
movzbl -0x1f(%rbp),%esi
movzbl -0x19(%rbp),%edx
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1275 <do_rename+0x135>
lea    -0x110(%rbp),%rdi
lea    -0x180(%rbp),%rsi
lea    -0xa0(%rbp),%rdx
call   1050 <check_file_type@plt>
cmp    $0x0,%rax
je     1263 <do_rename+0x123>
mov    -0x8(%rbp),%rsi
lea    -0x110(%rbp),%rdx
lea    -0x180(%rbp),%rcx
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1275 <do_rename+0x135>
mov    -0x8(%rbp),%rsi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x180,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_rename>:
push   %rbx
sub    $0x170,%rsp
mov    %rdi,%rbx
lea    0xeae(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1237 <do_rename+0xf7>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x82,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
cmp    $0x82,%eax
jne    11f7 <do_rename+0xb7>
xor    %eax,%eax
xor    %edx,%edx
movzbl 0x1(%rsp,%rax,1),%ecx
add    %edx,%ecx
movzbl 0x2(%rsp,%rax,1),%edx
add    %ecx,%edx
movzbl 0x3(%rsp,%rax,1),%ecx
add    %edx,%ecx
movzbl 0x4(%rsp,%rax,1),%edx
add    %ecx,%edx
movzbl %dl,%edx
add    $0x4,%rax
cmp    $0x80,%rax
jne    1180 <do_rename+0x40>
movzbl 0x81(%rsp),%esi
cmp    %esi,%edx
jne    1210 <do_rename+0xd0>
lea    0x100(%rsp),%rdi
lea    0x90(%rsp),%rsi
mov    %rsp,%rdx
call   1050 <check_file_type@plt>
test   %rax,%rax
je     1226 <do_rename+0xe6>
lea    0xe84(%rip),%rdi
lea    0x100(%rsp),%rdx
lea    0x90(%rsp),%rcx
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1237 <do_rename+0xf7>
lea    0xe05(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
add    $0x170,%rsp
pop    %rbx
jmp    1040 <printf@plt>
lea    0xe1a(%rip),%rdi
xor    %eax,%eax
add    $0x170,%rsp
pop    %rbx
jmp    1040 <printf@plt>
lea    0xe4c(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x170,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [46 x i8] c"# i/o error reading first block of file '%s'\0A\00"
@.str.2 = [46 x i8] c"# crc error: in file %02xh, calculated %02xh\0A\00"
@.str.3 = [26 x i8] c"mv '%s' '%s' # type = %s\0A\00"
@.str.4 = [37 x i8] c"# can't determine type of file '%s'\0A\00"
define void @do_rename(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = alloca [130 x i8]
%8 = alloca [100 x i8]
%9 = alloca [100 x i8]
store i8* %0, i8** %2
%10 = load i8*, i8** %2
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %3
%12 = load i32*, i32** %3
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %1
br label %69
15: ; preds = %1
%16 = getelementptr inbounds [130 x i8], [130 x i8]* %7, i64 0, i64 0
%17 = load i32*, i32** %3
%18 = call i32 @fread(i8* %16, i32 1, i32 130, i32* %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 130
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = load i8*, i8** %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %69
24: ; preds = %15
store i8 0, i8* %6
store i32 1, i32* %4
br label %25
25: ; preds = %38, %24
%26 = load i32, i32* %4
%27 = icmp slt i32 %26, 129
br i1 %27, label %28, label %41
28: ; preds = %25
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [130 x i8], [130 x i8]* %7, i64 0, i64 %30
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = load i8, i8* %6
%35 = zext i8 %34 to i32
%36 = add nsw i32 %35, %33
%37 = trunc i32 %36 to i8
store i8 %37, i8* %6
br label %38
38: ; preds = %28
%39 = load i32, i32* %4
%40 = add nsw i32 %39, 1
store i32 %40, i32* %4
br label %25
41: ; preds = %25
%42 = load i8, i8* %6
%43 = zext i8 %42 to i32
%44 = getelementptr inbounds [130 x i8], [130 x i8]* %7, i64 0, i64 129
%45 = load i8, i8* %44
%46 = zext i8 %45 to i32
%47 = icmp ne i32 %43, %46
br i1 %47, label %48, label %55
48: ; preds = %41
%49 = getelementptr inbounds [130 x i8], [130 x i8]* %7, i64 0, i64 129
%50 = load i8, i8* %49
%51 = zext i8 %50 to i32
%52 = load i8, i8* %6
%53 = zext i8 %52 to i32
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %51, i32 %53)
br label %69
55: ; preds = %41
%56 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%57 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%58 = getelementptr inbounds [130 x i8], [130 x i8]* %7, i64 0, i64 0
%59 = call i64 @check_file_type(i8* %56, i8* %57, i8* %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %55
%62 = load i8*, i8** %2
%63 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%64 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %62, i8* %63, i8* %64)
br label %69
66: ; preds = %55
%67 = load i8*, i8** %2
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %67)
br label %69
69: ; preds = %14, %21, %48, %66, %61
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [46 x i8] c"# i/o error reading first block of file '%s'\0A\00"
@.str.2 = [46 x i8] c"# crc error: in file %02xh, calculated %02xh\0A\00"
@.str.3 = [26 x i8] c"mv '%s' '%s' # type = %s\0A\00"
@.str.4 = [37 x i8] c"# can't determine type of file '%s'\0A\00"
define void @do_rename(i8* %0) {
%2 = alloca [130 x i8]
%3 = alloca [100 x i8]
%4 = alloca [100 x i8]
%5 = getelementptr inbounds [130 x i8], [130 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %54, label %10
10: ; preds = %1
%11 = call i32 @fread(i8* %5, i32 1, i32 130, i32* %8)
%12 = icmp eq i32 %11, 130
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %54
15: ; preds = %10, %15
%16 = phi i64 [ %37, %15 ], [ 1, %10 ]
%17 = phi i32 [ %38, %15 ], [ 0, %10 ]
%18 = getelementptr inbounds [130 x i8], [130 x i8]* %2, i64 0, i64 %16
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = add nuw nsw i32 %17, %20
%22 = add nuw nsw i64 %16, 1
%23 = getelementptr inbounds [130 x i8], [130 x i8]* %2, i64 0, i64 %22
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = add nuw nsw i32 %21, %25
%27 = add nuw nsw i64 %16, 2
%28 = getelementptr inbounds [130 x i8], [130 x i8]* %2, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = add nuw nsw i32 %26, %30
%32 = add nuw nsw i64 %16, 3
%33 = getelementptr inbounds [130 x i8], [130 x i8]* %2, i64 0, i64 %32
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = add nuw nsw i32 %31, %35
%37 = add nuw nsw i64 %16, 4
%38 = and i32 %36, 255
%39 = icmp eq i64 %37, 129
br i1 %39, label %40, label %15
40: ; preds = %15
%41 = getelementptr inbounds [130 x i8], [130 x i8]* %2, i64 0, i64 129
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
%44 = icmp eq i32 %38, %43
br i1 %44, label %47, label %45
45: ; preds = %40
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %43, i32 %38)
br label %54
47: ; preds = %40
%48 = call i64 @check_file_type(i8* %6, i8* %7, i8* %5)
%49 = icmp eq i64 %48, 0
br i1 %49, label %52, label %50
50: ; preds = %47
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %6, i8* %7)
br label %54
52: ; preds = %47
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %54
54: ; preds = %50, %52, %1, %45, %13
ret void
}