#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*/ fprintf(int /*<<< orphan*/ *, char *, int, int);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int, ...);
char *write_pgm(char *ptr, int counter) {
  int size;
  FILE *out_file;
  char pbm_header[50];
  char filename[50];
  int *width, *height;
  width = (int *)(ptr + 4);
  height = (int *)(ptr + 8);
  size = (*width) * (*height);
  ptr += 12;
  sprintf(pbm_header, "P5\n%d %d\n255\n", *width, *height);
  sprintf(filename, "test_%010d.pgm", counter);
  out_file = fopen(filename, "wb");
  fprintf(out_file, "P5\n%d %d\n255\n", *width, *height);
  fclose(out_file);
  out_file = fopen(filename, "a");
  fwrite(ptr, 1, size, out_file);
  fclose(out_file);
  return ptr + size;
}


x86 assembly (clang, -O0):
<write_pgm>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x98(%rbp)
mov    -0x8(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0xa0(%rbp)
mov    -0x98(%rbp),%rax
mov    (%rax),%eax
mov    -0xa0(%rbp),%rcx
imul   (%rcx),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
add    $0xc,%rax
mov    %rax,-0x8(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x98(%rbp),%rax
mov    (%rax),%edx
mov    -0xa0(%rbp),%rax
mov    (%rax),%ecx
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
lea    -0x90(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe37(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
lea    -0x90(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x98(%rbp),%rax
mov    (%rax),%edx
mov    -0xa0(%rbp),%rax
mov    (%rax),%ecx
lea    0xdee(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x90(%rbp),%rdi
lea    0xdf2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
add    %rcx,%rax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pgm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %esi,%r12d
mov    %rdi,%rbx
mov    0x4(%rdi),%edx
mov    0x8(%rdi),%ecx
mov    %ecx,%r14d
imul   %edx,%r14d
lea    0xc(%rdi),%rax
mov    %rax,0x8(%rsp)
lea    0xe7c(%rip),%r15
lea    0x50(%rsp),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <sprintf@plt>
lea    0xe74(%rip),%rsi
lea    0x10(%rsp),%r13
mov    %r13,%rdi
mov    %r12d,%edx
xor    %eax,%eax
call   1060 <sprintf@plt>
lea    0xe6a(%rip),%rsi
mov    %r13,%rdi
call   1050 <fopen@plt>
mov    %rax,%rbp
mov    0x4(%rbx),%edx
mov    0x8(%rbx),%ecx
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <fprintf@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
lea    0xe42(%rip),%rsi
mov    %r13,%rdi
call   1050 <fopen@plt>
mov    %rax,%rbp
mov    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
movslq %r14d,%rax
add    %rbx,%rax
add    $0xc,%rax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"P5\0A%d %d\0A255\0A\00"
@.str.1 = [15 x i8] c"test_%010d.pgm\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [2 x i8] c"a\00"
define i8* @write_pgm(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca [50 x i8]
%8 = alloca [50 x i8]
%9 = alloca i32*
%10 = alloca i32*
store i8* %0, i8** %3
store i32 %1, i32* %4
%11 = load i8*, i8** %3
%12 = getelementptr inbounds i8, i8* %11, i64 4
%13 = bitcast i8* %12 to i32*
store i32* %13, i32** %9
%14 = load i8*, i8** %3
%15 = getelementptr inbounds i8, i8* %14, i64 8
%16 = bitcast i8* %15 to i32*
store i32* %16, i32** %10
%17 = load i32*, i32** %9
%18 = load i32, i32* %17
%19 = load i32*, i32** %10
%20 = load i32, i32* %19
%21 = mul nsw i32 %18, %20
store i32 %21, i32* %5
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i64 12
store i8* %23, i8** %3
%24 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%25 = load i32*, i32** %9
%26 = load i32, i32* %25
%27 = load i32*, i32** %10
%28 = load i32, i32* %27
%29 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %24, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %26, i32 %28)
%30 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
%31 = load i32, i32* %4
%32 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %30, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %34, i32** %6
%35 = load i32*, i32** %6
%36 = load i32*, i32** %9
%37 = load i32, i32* %36
%38 = load i32*, i32** %10
%39 = load i32, i32* %38
%40 = call i32 @fprintf(i32* %35, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %37, i32 %39)
%41 = load i32*, i32** %6
%42 = call i32 @fclose(i32* %41)
%43 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
%44 = call i32* @fopen(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %44, i32** %6
%45 = load i8*, i8** %3
%46 = load i32, i32* %5
%47 = load i32*, i32** %6
%48 = call i32 @fwrite(i8* %45, i32 1, i32 %46, i32* %47)
%49 = load i32*, i32** %6
%50 = call i32 @fclose(i32* %49)
%51 = load i8*, i8** %3
%52 = load i32, i32* %5
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
ret i8* %54
}

LLVM IR (-O2):
@.str = [14 x i8] c"P5\0A%d %d\0A255\0A\00"
@.str.1 = [15 x i8] c"test_%010d.pgm\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [2 x i8] c"a\00"
define i8* @write_pgm(i8* %0, i32 %1) {
%3 = alloca [50 x i8]
%4 = alloca [50 x i8]
%5 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds i8, i8* %0, i64 4
%8 = bitcast i8* %7 to i32*
%9 = getelementptr inbounds i8, i8* %0, i64 8
%10 = bitcast i8* %9 to i32*
%11 = load i32, i32* %8
%12 = load i32, i32* %10
%13 = mul nsw i32 %12, %11
%14 = getelementptr inbounds i8, i8* %0, i64 12
%15 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %12)
%16 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%17 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* %8
%19 = load i32, i32* %10
%20 = tail call i32 @fprintf(i32* %17, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %18, i32 %19)
%21 = tail call i32 @fclose(i32* %17)
%22 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%23 = tail call i32 @fwrite(i8* %14, i32 1, i32 %13, i32* %22)
%24 = tail call i32 @fclose(i32* %22)
%25 = sext i32 %13 to i64
%26 = getelementptr inbounds i8, i8* %14, i64 %25
ret i8* %26
}