#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 */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "Usage: %s FILE\n\n", argv[0]);
    exit(1);
  }
  FILE *f = fopen(argv[1], "r+");
  if (!f) {
    perror("fopen failed");
    exit(1);
  }
  fseek(f, 0, SEEK_END);
  int f_size = ftell(f);
  fseek(f, 0, SEEK_SET);
  unsigned char sum = 0;
  int i;
  for (i = 0; i < f_size - 1; i++) {
    sum += fgetc(f);
  }
  fputc((0x100 - sum) & 0xff, f);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d6 <main+0x46>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe2c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    120f <main+0x7f>
lea    0xe14(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d96(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
movb   $0x0,-0x1d(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1283 <main+0xf3>
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %rax,%rcx
movzbl -0x1d(%rbp),%eax
add    %rcx,%rax
mov    %al,-0x1d(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    124e <main+0xbe>
movzbl -0x1d(%rbp),%eax
mov    $0x100,%edi
sub    %eax,%edi
and    $0xff,%edi
mov    -0x18(%rbp),%rsi
call   1050 <fputc@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jne    122b <main+0x9b>
mov    0x8(%rsi),%rdi
lea    0xe69(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     124d <main+0xbd>
mov    %rax,%r14
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r14,%rdi
call   1070 <ftell@plt>
mov    %eax,%ebp
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x2,%ebp
jl     120f <main+0x7f>
add    $0xffffffff,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
call   1040 <fgetc@plt>
add    %al,%bl
add    $0xffffffff,%ebp
jne    1200 <main+0x70>
neg    %bl
movzbl %bl,%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xdc2(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xdc0(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [17 x i8] c"Usage: %s FILE\0A\0A\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [13 x i8] c"fopen failed\00"
@SEEK_END = global i32 0
@SEEK_SET = 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 i8
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %6
%24 = load i32*, i32** %6
%25 = icmp ne i32* %24, null
br i1 %25, label %29, label %26
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %19
%30 = load i32*, i32** %6
%31 = load i32, i32* @SEEK_END
%32 = call i32 @fseek(i32* %30, i32 0, i32 %31)
%33 = load i32*, i32** %6
%34 = call i32 @ftell(i32* %33)
store i32 %34, i32* %7
%35 = load i32*, i32** %6
%36 = load i32, i32* @SEEK_SET
%37 = call i32 @fseek(i32* %35, i32 0, i32 %36)
store i8 0, i8* %8
store i32 0, i32* %9
br label %38
38: ; preds = %50, %29
%39 = load i32, i32* %9
%40 = load i32, i32* %7
%41 = sub nsw i32 %40, 1
%42 = icmp slt i32 %39, %41
br i1 %42, label %43, label %53
43: ; preds = %38
%44 = load i32*, i32** %6
%45 = call i64 @fgetc(i32* %44)
%46 = load i8, i8* %8
%47 = zext i8 %46 to i64
%48 = add nsw i64 %47, %45
%49 = trunc i64 %48 to i8
store i8 %49, i8* %8
br label %50
50: ; preds = %43
%51 = load i32, i32* %9
%52 = add nsw i32 %51, 1
store i32 %52, i32* %9
br label %38
53: ; preds = %38
%54 = load i8, i8* %8
%55 = zext i8 %54 to i32
%56 = sub nsw i32 256, %55
%57 = and i32 %56, 255
%58 = load i32*, i32** %6
%59 = call i32 @fputc(i32 %57, i32* %58)
%60 = load i32*, i32** %6
%61 = call i32 @fclose(i32* %60)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [17 x i8] c"Usage: %s FILE\0A\0A\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [13 x i8] c"fopen failed\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %6)
%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 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %9
%18 = load i32, i32* @SEEK_END
%19 = tail call i32 @fseek(i32* %12, i32 0, i32 %18)
%20 = tail call i32 @ftell(i32* %12)
%21 = load i32, i32* @SEEK_SET
%22 = tail call i32 @fseek(i32* %12, i32 0, i32 %21)
%23 = add i32 %20, -1
%24 = icmp sgt i32 %20, 1
br i1 %24, label %25, label %33
25: ; preds = %17, %25
%26 = phi i32 [ %31, %25 ], [ 0, %17 ]
%27 = phi i8 [ %30, %25 ], [ 0, %17 ]
%28 = tail call i64 @fgetc(i32* %12)
%29 = trunc i64 %28 to i8
%30 = add i8 %27, %29
%31 = add nuw nsw i32 %26, 1
%32 = icmp eq i32 %31, %23
br i1 %32, label %33, label %25
33: ; preds = %25, %17
%34 = phi i8 [ 0, %17 ], [ %30, %25 ]
%35 = sub i8 0, %34
%36 = zext i8 %35 to i32
%37 = tail call i32 @fputc(i32 %36, i32* %12)
%38 = tail call i32 @fclose(i32* %12)
ret i32 0
}