#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 findMAXval(char *, int);
size_t fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int snprintf(char *, int, char *, char const *, int, int, int);
int writeGREY(FILE *imgfile, int width, int height, char *image_data,
              int data_length) {
  static const char *type = "P5";
  char buff[256 + 1];
  int strlength = 0;
  int maxval = 127;
  size_t lthstr = 0;
  memset(buff, 0, (256 + 1) * sizeof(char));
  maxval = findMAXval(image_data, data_length);
  strlength = snprintf(buff, 256, "%s %d %d %d\n", type, width, height, maxval);
  printf("%d %s\n", strlength, buff);
  if (buff[0] == 0 || strlength < 8) {
    printf("Invalid header\n");
    return -1;
  }
  lthstr = fwrite(buff, sizeof(char), strlength, imgfile);
  printf("wrote %u bytes\n", lthstr);
  if (lthstr != strlength * sizeof(char)) {
    printf("Failed to write header\n");
    return -1;
  }
  lthstr = fwrite(image_data, sizeof(char), data_length, imgfile);
  if (lthstr != data_length * sizeof(char)) {
    printf("Failed to write image data.\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<writeGREY>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movl   $0x0,-0x134(%rbp)
movl   $0x7f,-0x138(%rbp)
movq   $0x0,-0x140(%rbp)
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x101,%edx
call   1060 <memset@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <findMAXval@plt>
mov    %eax,-0x138(%rbp)
lea    -0x130(%rbp),%rdi
mov    0x2e89(%rip),%rcx
mov    -0x14(%rbp),%r8d
mov    -0x18(%rbp),%r9d
mov    -0x138(%rbp),%eax
mov    $0x100,%esi
lea    0xe2a(%rip),%rdx
mov    %eax,(%rsp)
call   1050 <snprintf@plt>
mov    %eax,-0x134(%rbp)
mov    -0x134(%rbp),%esi
lea    -0x130(%rbp),%rdx
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movsbl -0x130(%rbp),%eax
cmp    $0x0,%eax
je     121f <writeGREY+0xcf>
cmpl   $0x8,-0x134(%rbp)
jge    1239 <writeGREY+0xe9>
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fa <writeGREY+0x1aa>
lea    -0x130(%rbp),%rdi
mov    -0x134(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %rax,-0x140(%rbp)
mov    -0x140(%rbp),%rsi
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x140(%rbp),%rax
movslq -0x134(%rbp),%rcx
shl    $0x0,%rcx
cmp    %rcx,%rax
je     12a5 <writeGREY+0x155>
lea    0xda5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fa <writeGREY+0x1aa>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %rax,-0x140(%rbp)
mov    -0x140(%rbp),%rax
movslq -0x24(%rbp),%rcx
shl    $0x0,%rcx
cmp    %rcx,%rax
je     12f3 <writeGREY+0x1a3>
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fa <writeGREY+0x1aa>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeGREY>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %r8d,%r12d
mov    %rcx,%r15
mov    %edx,%ebp
mov    %esi,%ebx
mov    %rdi,%r14
lea    0x10(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x101,%edx
call   1070 <memset@plt>
mov    %r15,%rdi
mov    %r12d,%esi
call   1040 <findMAXval@plt>
mov    %eax,(%rsp)
lea    0xe5c(%rip),%rdx
lea    0xe52(%rip),%rcx
mov    %r13,%rdi
mov    $0x100,%esi
mov    %ebx,%r8d
mov    %ebp,%r9d
call   1060 <snprintf@plt>
mov    %eax,%ebp
lea    0xe46(%rip),%rdi
mov    %eax,%esi
mov    %r13,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe7d(%rip),%rdi
cmp    $0x8,%ebp
jl     1245 <writeGREY+0xe5>
cmpb   $0x0,0x10(%rsp)
je     1245 <writeGREY+0xe5>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r14,%rcx
call   1080 <fwrite@plt>
mov    %rax,%r13
lea    0xe10(%rip),%rdi
xor    %ebx,%ebx
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%eax
cmp    %rax,%r13
jne    123e <writeGREY+0xde>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %r14,%rcx
call   1080 <fwrite@plt>
movslq %r12d,%rcx
cmp    %rcx,%rax
je     124f <writeGREY+0xef>
lea    0xdeb(%rip),%rdi
jmp    1245 <writeGREY+0xe5>
lea    0xdfe(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@writeGREY.type = internal global i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0)
@.str = [3 x i8] c"P5\00"
@.str.1 = [13 x i8] c"%s %d %d %d\0A\00"
@.str.2 = [7 x i8] c"%d %s\0A\00"
@.str.3 = [16 x i8] c"Invalid header\0A\00"
@.str.4 = [16 x i8] c"wrote %u bytes\0A\00"
@.str.5 = [24 x i8] c"Failed to write header\0A\00"
@.str.6 = [29 x i8] c"Failed to write image data.\0A\00"
define i32 @writeGREY(i32* %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca [257 x i8]
%13 = alloca i32
%14 = alloca i32
%15 = alloca i64
store i32* %0, i32** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
store i32 0, i32* %13
store i32 127, i32* %14
store i64 0, i64* %15
%16 = getelementptr inbounds [257 x i8], [257 x i8]* %12, i64 0, i64 0
%17 = call i32 @memset(i8* %16, i32 0, i32 257)
%18 = load i8*, i8** %10
%19 = load i32, i32* %11
%20 = call i32 @findMAXval(i8* %18, i32 %19)
store i32 %20, i32* %14
%21 = getelementptr inbounds [257 x i8], [257 x i8]* %12, i64 0, i64 0
%22 = load i8*, i8** @writeGREY.type
%23 = load i32, i32* %8
%24 = load i32, i32* %9
%25 = load i32, i32* %14
%26 = call i32 @snprintf(i8* %21, i32 256, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %22, i32 %23, i32 %24, i32 %25)
store i32 %26, i32* %13
%27 = load i32, i32* %13
%28 = getelementptr inbounds [257 x i8], [257 x i8]* %12, i64 0, i64 0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %27, i8* %28)
%30 = getelementptr inbounds [257 x i8], [257 x i8]* %12, i64 0, i64 0
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %5
%35 = load i32, i32* %13
%36 = icmp slt i32 %35, 8
br i1 %36, label %37, label %39
37: ; preds = %34, %5
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %6
br label %66
39: ; preds = %34
%40 = getelementptr inbounds [257 x i8], [257 x i8]* %12, i64 0, i64 0
%41 = load i32, i32* %13
%42 = load i32*, i32** %7
%43 = call i64 @fwrite(i8* %40, i32 1, i32 %41, i32* %42)
store i64 %43, i64* %15
%44 = load i64, i64* %15
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i64 %44)
%46 = load i64, i64* %15
%47 = load i32, i32* %13
%48 = sext i32 %47 to i64
%49 = mul i64 %48, 1
%50 = icmp ne i64 %46, %49
br i1 %50, label %51, label %53
51: ; preds = %39
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %6
br label %66
53: ; preds = %39
%54 = load i8*, i8** %10
%55 = load i32, i32* %11
%56 = load i32*, i32** %7
%57 = call i64 @fwrite(i8* %54, i32 1, i32 %55, i32* %56)
store i64 %57, i64* %15
%58 = load i64, i64* %15
%59 = load i32, i32* %11
%60 = sext i32 %59 to i64
%61 = mul i64 %60, 1
%62 = icmp ne i64 %58, %61
br i1 %62, label %63, label %65
63: ; preds = %53
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %6
br label %66
65: ; preds = %53
store i32 0, i32* %6
br label %66
66: ; preds = %65, %63, %51, %37
%67 = load i32, i32* %6
ret i32 %67
}

LLVM IR (-O2):
@.str = [3 x i8] c"P5\00"
@.str.1 = [13 x i8] c"%s %d %d %d\0A\00"
@.str.2 = [7 x i8] c"%d %s\0A\00"
@.str.4 = [16 x i8] c"wrote %u bytes\0A\00"
@str = [28 x i8] c"Failed to write image data.\00"
@str.7 = [23 x i8] c"Failed to write header\00"
@str.8 = [15 x i8] c"Invalid header\00"
define i32 @writeGREY(i32* nocapture %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca [257 x i8]
%7 = getelementptr inbounds [257 x i8], [257 x i8]* %6, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 0, i32 257)
%9 = call i32 @findMAXval(i8* %3, i32 %4)
%10 = call i32 @snprintf(i8* %7, i32 256, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %2, i32 %9)
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %10, i8* %7)
%12 = load i8, i8* %7
%13 = icmp eq i8 %12, 0
%14 = icmp slt i32 %10, 8
%15 = or i1 %14, %13
br i1 %15, label %25, label %16
16: ; preds = %5
%17 = call i64 @fwrite(i8* %7, i32 1, i32 %10, i32* %0)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i64 %17)
%19 = zext i32 %10 to i64
%20 = icmp eq i64 %17, %19
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = call i64 @fwrite(i8* %3, i32 1, i32 %4, i32* %0)
%23 = sext i32 %4 to i64
%24 = icmp eq i64 %22, %23
br i1 %24, label %28, label %25
25: ; preds = %21, %16, %5
%26 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @str.8, i64 0, i64 0), %5 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @str.7, i64 0, i64 0), %16 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0), %21 ]
%27 = call i32 @puts(i8* dereferenceable(1) %26)
br label %28
28: ; preds = %25, %21
%29 = phi i32 [ 0, %21 ], [ -1, %25 ]
ret i32 %29
}