#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*/ uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EOF;
int /*<<< orphan*/ errx(int, char *, int);
int fprintf(int /*<<< orphan*/ *, char *, int, int, ...);
scalar_t__ fputc(int /*<<< orphan*/ const, int /*<<< orphan*/ *);
int const fwrite(int /*<<< orphan*/ const *, int, int const,
                 int /*<<< orphan*/ *);
int pgm_write(FILE *fp, const uint8_t *buf, int w, int h, const char *comment,
              int stride) {
  const int N = w * h;
  int wr;
  if (comment)
    wr = fprintf(fp, "P5\n%d %d\n# %s\n255\n", w, h, comment);
  else
    wr = fprintf(fp, "P5\n%d %d\n255\n", w, h);
  if (wr < 11)
    errx(-1, "error: writing PGM header (only %d chars)", wr);
  if (stride == 1)
    return fwrite(buf, sizeof(uint8_t), N, fp) == N ? 0 : -1;
  else {
    int n = w * h;
    while (n-- > 0) {
      if (fputc(*buf, fp) == EOF)
        return -1;
      buf += stride;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<pgm_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
mov    -0x1c(%rbp),%eax
imul   -0x20(%rbp),%eax
mov    %eax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1197 <pgm_write+0x57>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x28(%rbp),%r8
lea    0xe78(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    %eax,-0x34(%rbp)
jmp    11b2 <pgm_write+0x72>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe6b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xb,-0x34(%rbp)
jge    11d0 <pgm_write+0x90>
mov    -0x34(%rbp),%edx
mov    $0xffffffff,%edi
lea    0xe56(%rip),%rsi
call   1030 <errx@plt>
cmpl   $0x1,-0x2c(%rbp)
jne    1208 <pgm_write+0xc8>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x4,%esi
call   1060 <fwrite@plt>
mov    %eax,%edx
mov    -0x30(%rbp),%esi
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    %esi,%edx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1277 <pgm_write+0x137>
mov    -0x1c(%rbp),%eax
imul   -0x20(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x38(%rbp)
cmp    $0x0,%eax
jle    126b <pgm_write+0x12b>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
call   1040 <fputc@plt>
mov    0x2dac(%rip),%rcx
cmp    (%rcx),%rax
jne    1251 <pgm_write+0x111>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <pgm_write+0x137>
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1212 <pgm_write+0xd2>
jmp    1270 <pgm_write+0x130>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pgm_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r12d
mov    %ecx,%ebx
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r14
test   %r8,%r8
je     11ee <pgm_write+0xae>
lea    0xe98(%rip),%rsi
mov    %r14,%rdi
mov    %ebp,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
cmp    $0xa,%eax
jg     118e <pgm_write+0x4e>
lea    0xe9f(%rip),%rsi
mov    $0xffffffff,%edi
mov    %eax,%edx
call   1030 <errx@plt>
imul   %ebp,%ebx
cmp    $0x1,%r12d
jne    11b7 <pgm_write+0x77>
mov    %r15,%rdi
mov    $0x4,%esi
mov    %ebx,%edx
mov    %r14,%rcx
call   1060 <fwrite@plt>
xor    %r13d,%r13d
cmp    %ebx,%eax
setne  %r13b
neg    %r13d
jmp    1214 <pgm_write+0xd4>
xor    %r13d,%r13d
test   %ebx,%ebx
jle    1214 <pgm_write+0xd4>
movslq %r12d,%rbp
add    $0x1,%ebx
shl    $0x2,%rbp
mov    0x2e19(%rip),%r12
nop
mov    (%r15),%edi
mov    %r14,%rsi
call   1040 <fputc@plt>
cmp    (%r12),%rax
je     120e <pgm_write+0xce>
add    $0xffffffff,%ebx
add    %rbp,%r15
cmp    $0x1,%ebx
jg     11d0 <pgm_write+0x90>
jmp    1214 <pgm_write+0xd4>
lea    0xe1e(%rip),%rsi
mov    %r14,%rdi
mov    %ebp,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
cmp    $0xa,%eax
jle    117b <pgm_write+0x3b>
jmp    118e <pgm_write+0x4e>
mov    $0xffffffff,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"P5\0A%d %d\0A# %s\0A255\0A\00"
@.str.1 = [14 x i8] c"P5\0A%d %d\0A255\0A\00"
@.str.2 = [42 x i8] c"error: writing PGM header (only %d chars)\00"
@EOF = global i64 0
define i32 @pgm_write(i32* %0, i32* %1, i32 %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32* %0, i32** %8
store i32* %1, i32** %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i8* %4, i8** %12
store i32 %5, i32* %13
%17 = load i32, i32* %10
%18 = load i32, i32* %11
%19 = mul nsw i32 %17, %18
store i32 %19, i32* %14
%20 = load i8*, i8** %12
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %28
22: ; preds = %6
%23 = load i32*, i32** %8
%24 = load i32, i32* %10
%25 = load i32, i32* %11
%26 = load i8*, i8** %12
%27 = call i32 (i32*, i8*, i32, i32, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %25, i8* %26)
store i32 %27, i32* %15
br label %33
28: ; preds = %6
%29 = load i32*, i32** %8
%30 = load i32, i32* %10
%31 = load i32, i32* %11
%32 = call i32 (i32*, i8*, i32, i32, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %30, i32 %31)
store i32 %32, i32* %15
br label %33
33: ; preds = %28, %22
%34 = load i32, i32* %15
%35 = icmp slt i32 %34, 11
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i32, i32* %15
%38 = call i32 @errx(i32 -1, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %36, %33
%40 = load i32, i32* %13
%41 = icmp eq i32 %40, 1
br i1 %41, label %42, label %51
42: ; preds = %39
%43 = load i32*, i32** %9
%44 = load i32, i32* %14
%45 = load i32*, i32** %8
%46 = call i32 @fwrite(i32* %43, i32 4, i32 %44, i32* %45)
%47 = load i32, i32* %14
%48 = icmp eq i32 %46, %47
%49 = zext i1 %48 to i64
%50 = select i1 %48, i32 0, i32 -1
store i32 %50, i32* %7
br label %74
51: ; preds = %39
%52 = load i32, i32* %10
%53 = load i32, i32* %11
%54 = mul nsw i32 %52, %53
store i32 %54, i32* %16
br label %55
55: ; preds = %67, %51
%56 = load i32, i32* %16
%57 = add nsw i32 %56, -1
store i32 %57, i32* %16
%58 = icmp sgt i32 %56, 0
br i1 %58, label %59, label %72
59: ; preds = %55
%60 = load i32*, i32** %9
%61 = load i32, i32* %60
%62 = load i32*, i32** %8
%63 = call i64 @fputc(i32 %61, i32* %62)
%64 = load i64, i64* @EOF
%65 = icmp eq i64 %63, %64
br i1 %65, label %66, label %67
66: ; preds = %59
store i32 -1, i32* %7
br label %74
67: ; preds = %59
%68 = load i32, i32* %13
%69 = load i32*, i32** %9
%70 = sext i32 %68 to i64
%71 = getelementptr inbounds i32, i32* %69, i64 %70
store i32* %71, i32** %9
br label %55
72: ; preds = %55
br label %73
73: ; preds = %72
store i32 0, i32* %7
br label %74
74: ; preds = %73, %66, %42
%75 = load i32, i32* %7
ret i32 %75
}

LLVM IR (-O2):
@.str = [19 x i8] c"P5\0A%d %d\0A# %s\0A255\0A\00"
@.str.1 = [14 x i8] c"P5\0A%d %d\0A255\0A\00"
@.str.2 = [42 x i8] c"error: writing PGM header (only %d chars)\00"
@EOF = global i64 0
define i32 @pgm_write(i32* nocapture %0, i32* nocapture %1, i32 %2, i32 %3, i8* %4, i32 %5) {
%7 = mul nsw i32 %3, %2
%8 = icmp eq i8* %4, null
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 (i32*, i8*, i32, i32, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %2, i32 %3, i8* %4)
br label %13
11: ; preds = %6
%12 = tail call i32 (i32*, i8*, i32, i32, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %2, i32 %3)
br label %13
13: ; preds = %11, %9
%14 = phi i32 [ %10, %9 ], [ %12, %11 ]
%15 = icmp slt i32 %14, 11
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @errx(i32 -1, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %14)
br label %18
18: ; preds = %16, %13
%19 = icmp eq i32 %5, 1
br i1 %19, label %24, label %20
20: ; preds = %18
%21 = icmp sgt i32 %7, 0
br i1 %21, label %22, label %39
22: ; preds = %20
%23 = sext i32 %5 to i64
br label %28
24: ; preds = %18
%25 = tail call i32 @fwrite(i32* %1, i32 4, i32 %7, i32* %0)
%26 = icmp ne i32 %25, %7
%27 = sext i1 %26 to i32
br label %39
28: ; preds = %22, %35
%29 = phi i32 [ %7, %22 ], [ %36, %35 ]
%30 = phi i32* [ %1, %22 ], [ %37, %35 ]
%31 = load i32, i32* %30
%32 = tail call i64 @fputc(i32 %31, i32* %0)
%33 = load i64, i64* @EOF
%34 = icmp eq i64 %32, %33
br i1 %34, label %39, label %35
35: ; preds = %28
%36 = add nsw i32 %29, -1
%37 = getelementptr inbounds i32, i32* %30, i64 %23
%38 = icmp sgt i32 %29, 1
br i1 %38, label %28, label %39
39: ; preds = %35, %28, %20, %24
%40 = phi i32 [ %27, %24 ], [ 0, %20 ], [ 0, %35 ], [ -1, %28 ]
ret i32 %40
}