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

/* Variables and functions */
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
FILE *pgm_open_and_initialize(char filename[], uint8_t postfix) {
  char full_name[262];
  sprintf(full_name, "%s-%d.pgm", filename, postfix);
  FILE *image = fopen(full_name, "w+");
  fputs("P5\n", image);
  fprintf(image, "%d %d\n", 16 * 8, 14 * 8);
  fputs("255\n", image);
  return image;
}


x86 assembly (clang, -O0):
<pgm_open_and_initialize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
lea    0xe99(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x120(%rbp),%rdi
lea    0xe90(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x128(%rbp)
mov    -0x128(%rbp),%rsi
lea    0xe79(%rip),%rdi
call   1030 <fputs@plt>
mov    -0x128(%rbp),%rdi
lea    0xe6a(%rip),%rsi
mov    $0x80,%edx
mov    $0x70,%ecx
call   1040 <fprintf@plt>
mov    -0x128(%rbp),%rsi
lea    0xe54(%rip),%rdi
call   1030 <fputs@plt>
mov    -0x128(%rbp),%rax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pgm_open_and_initialize>:
push   %rbx
sub    $0x110,%rsp
mov    %esi,%ecx
mov    %rdi,%rdx
lea    0xeac(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <sprintf@plt>
lea    0xea4(%rip),%rsi
mov    %rbx,%rdi
call   1040 <fopen@plt>
mov    %rax,%rbx
lea    0xe95(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
lea    0xe80(%rip),%rsi
mov    %rbx,%rdi
mov    $0x80,%edx
mov    $0x70,%ecx
call   1030 <fprintf@plt>
lea    0xe6e(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
mov    %rbx,%rax
add    $0x110,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [10 x i8] c"%s-%d.pgm\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [4 x i8] c"P5\0A\00"
@.str.3 = [7 x i8] c"%d %d\0A\00"
@.str.4 = [5 x i8] c"255\0A\00"
define i32* @pgm_open_and_initialize(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [262 x i8]
%6 = alloca i32*
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = getelementptr inbounds [262 x i8], [262 x i8]* %5, i64 0, i64 0
%8 = load i8*, i8** %3
%9 = load i32, i32* %4
%10 = call i32 @sprintf(i8* %7, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %8, i32 %9)
%11 = getelementptr inbounds [262 x i8], [262 x i8]* %5, i64 0, i64 0
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = call i32 @fputs(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %13)
%15 = load i32*, i32** %6
%16 = call i32 @fprintf(i32* %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 128, i32 112)
%17 = load i32*, i32** %6
%18 = call i32 @fputs(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32* %17)
%19 = load i32*, i32** %6
ret i32* %19
}

LLVM IR (-O2):
@.str = [10 x i8] c"%s-%d.pgm\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [4 x i8] c"P5\0A\00"
@.str.3 = [7 x i8] c"%d %d\0A\00"
@.str.4 = [5 x i8] c"255\0A\00"
define noalias i32* @pgm_open_and_initialize(i8* %0, i32 %1) {
%3 = alloca [262 x i8]
%4 = getelementptr inbounds [262 x i8], [262 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
%6 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64 3, i64 1, i32* %6)
%8 = tail call i32 @fprintf(i32* %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 128, i32 112)
%9 = tail call i64 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i64 4, i64 1, i32* %6)
ret i32* %6
}