#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(unsigned char, int /*<<< orphan*/ *);
scalar_t__ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *, char *);
int main(int argc, char **argv) {
  if (argc < 3)
    return 128;
  printf("Processing %s => %s (RGB => grayscale)\n", argv[1], argv[2]);
  FILE *input = fopen(argv[1], "rb");
  FILE *output = fopen(argv[2], "wb");
  unsigned char color[4];
  unsigned char gray;
  while (fread(color, 1, 4, input) != 0) {
    gray = (color[0] + color[1] + color[2]) / 3;
    fputc(gray, output);
  }
  fclose(input);
  fclose(output);
  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   $0x3,-0x8(%rbp)
jge    117c <main+0x2c>
movl   $0x80,-0x4(%rbp)
jmp    1231 <main+0xe1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xe6d(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe81(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe6c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
lea    -0x24(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fread@plt>
cmp    $0x0,%rax
je     1218 <main+0xc8>
movzbl -0x24(%rbp),%eax
movzbl -0x23(%rbp),%ecx
add    %ecx,%eax
movzbl -0x22(%rbp),%ecx
add    %ecx,%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
mov    -0x20(%rbp),%rsi
movzbl %al,%edi
call   1060 <fputc@plt>
jmp    11c8 <main+0x78>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
mov    $0x80,%eax
cmp    $0x3,%edi
jl     122c <main+0xdc>
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    0x8(%rsi),%rsi
mov    0x10(%rbx),%rdx
lea    0xe87(%rip),%rdi
call   1050 <printf@plt>
mov    0x8(%rbx),%rdi
lea    0xe9f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r14
mov    0x10(%rbx),%rdi
lea    0xe8f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     1211 <main+0xc1>
lea    0xc(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nop
movzbl 0xc(%rsp),%eax
movzbl 0xd(%rsp),%ecx
add    %eax,%ecx
movzbl 0xe(%rsp),%eax
add    %ecx,%eax
imul   $0xaaab,%eax,%eax
shr    $0x11,%eax
movzbl %al,%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jne    11d0 <main+0x80>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [40 x i8] c"Processing %s => %s (RGB => grayscale)\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [3 x i8] c"wb\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [4 x i8]
%9 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 128, i32* %3
br label %56
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 2
%19 = load i8*, i8** %18
%20 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %19)
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %24, i32** %6
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %28, i32** %7
br label %29
29: ; preds = %34, %13
%30 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%31 = load i32*, i32** %6
%32 = call i64 @fread(i8* %30, i32 1, i32 4, i32* %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %51
34: ; preds = %29
%35 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 1
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = add nsw i32 %37, %40
%42 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 2
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = add nsw i32 %41, %44
%46 = sdiv i32 %45, 3
%47 = trunc i32 %46 to i8
store i8 %47, i8* %9
%48 = load i8, i8* %9
%49 = load i32*, i32** %7
%50 = call i32 @fputc(i8 zeroext %48, i32* %49)
br label %29
51: ; preds = %29
%52 = load i32*, i32** %6
%53 = call i32 @fclose(i32* %52)
%54 = load i32*, i32** %7
%55 = call i32 @fclose(i32* %54)
store i32 0, i32* %3
br label %56
56: ; preds = %51, %12
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@.str = [40 x i8] c"Processing %s => %s (RGB => grayscale)\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [3 x i8] c"wb\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4 x i8]
%4 = icmp slt i32 %0, 3
br i1 %4, label %38, label %5
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = getelementptr inbounds i8*, i8** %1, i64 2
%9 = load i8*, i8** %8
%10 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %7, i8* %9)
%11 = load i8*, i8** %6
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%13 = load i8*, i8** %8
%14 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%15 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%16 = call i64 @fread(i8* %15, i32 1, i32 4, i32* %12)
%17 = icmp eq i64 %16, 0
br i1 %17, label %35, label %18
18: ; preds = %5
%19 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
%20 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
br label %21
21: ; preds = %18, %21
%22 = load i8, i8* %15
%23 = zext i8 %22 to i16
%24 = load i8, i8* %19
%25 = zext i8 %24 to i16
%26 = add nuw nsw i16 %25, %23
%27 = load i8, i8* %20
%28 = zext i8 %27 to i16
%29 = add nuw nsw i16 %26, %28
%30 = udiv i16 %29, 3
%31 = trunc i16 %30 to i8
%32 = tail call i32 @fputc(i8 zeroext %31, i32* %14)
%33 = call i64 @fread(i8* %15, i32 1, i32 4, i32* %12)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %21
35: ; preds = %21, %5
%36 = tail call i32 @fclose(i32* %12)
%37 = tail call i32 @fclose(i32* %14)
br label %38
38: ; preds = %2, %35
%39 = phi i32 [ 0, %35 ], [ 128, %2 ]
ret i32 %39
}