#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 /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
void writePgmPictureASCII(char *fileNameBase, char *nameFile, char *mode,
                          int **imageMatrix, char *magicNumber, int idim,
                          int jdim, int dynamic) {
  int row = 0;
  int column = 0;
  char buffer[16] = {0};
  FILE *file;
  file = fopen(nameFile, mode);
  if (file != NULL) {
    fprintf(file, "%s@n", magicNumber);
    fprintf(file, "# This a binary image from %s@n", fileNameBase);
    fprintf(
        file,
        "# It is automatic created by Delmas Pierre Binarisation Program@n");
    fprintf(file, "%d %d@n", idim, jdim);
    fprintf(file, "%d@n", dynamic);
    for (row = 0; row < idim; row++) {
      for (column = 0; column < jdim; column++) {
        sprintf(buffer, "%d@n", imageMatrix[row][column]);
        fputs(buffer, file);
      }
    }
    fclose(file);
  }
}


x86 assembly (clang, -O0):
<writePgmPictureASCII>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%eax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     12ab <writePgmPictureASCII+0x14b>
mov    -0x58(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xe4c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x58(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe1b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x58(%rbp),%rdi
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x58(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    0x10(%rbp),%ecx
lea    0xe53(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x58(%rbp),%rdi
mov    0x18(%rbp),%edx
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    12a2 <writePgmPictureASCII+0x142>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    0x10(%rbp),%eax
jge    128f <writePgmPictureASCII+0x12f>
lea    -0x50(%rbp),%rdi
mov    -0x20(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xdf6(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rsi
call   1040 <fputs@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1245 <writePgmPictureASCII+0xe5>
jmp    1294 <writePgmPictureASCII+0x134>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1232 <writePgmPictureASCII+0xd2>
mov    -0x58(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writePgmPictureASCII>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r9d,%r15d
mov    %r8,%r12
mov    %rcx,%rbx
mov    %rdi,%r14
mov    0x78(%rsp),%eax
mov    %eax,(%rsp)
mov    0x70(%rsp),%r13d
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
mov    %rsi,%rdi
mov    %rdx,%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     12b3 <writePgmPictureASCII+0x153>
mov    %rax,%rbp
mov    %rbx,0x18(%rsp)
lea    0xe6a(%rip),%rsi
mov    %rax,%rdi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe3b(%rip),%rsi
mov    %rbp,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe47(%rip),%rdi
mov    $0x41,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1080 <fwrite@plt>
lea    0xe70(%rip),%rsi
mov    %rbp,%rdi
mov    %r15d,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe5c(%rip),%rsi
mov    %rbp,0x10(%rsp)
mov    %rbp,%rdi
mov    (%rsp),%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %r15d,%r15d
jle    129b <writePgmPictureASCII+0x13b>
mov    %r15d,%eax
mov    %rax,(%rsp)
mov    %r13d,%ebx
xor    %r15d,%r15d
lea    0xe31(%rip),%rbp
lea    0x20(%rsp),%r12
mov    %r13d,0xc(%rsp)
mov    0x18(%rsp),%r14
jmp    125f <writePgmPictureASCII+0xff>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%r15
cmp    (%rsp),%r15
mov    0xc(%rsp),%r13d
je     129b <writePgmPictureASCII+0x13b>
test   %r13d,%r13d
jle    1250 <writePgmPictureASCII+0xf0>
xor    %r13d,%r13d
nopw   0x0(%rax,%rax,1)
mov    (%r14,%r15,8),%rax
mov    (%rax,%r13,4),%edx
mov    %r12,%rdi
mov    %rbp,%rsi
call   1070 <sprintf@plt>
mov    %r12,%rdi
mov    0x10(%rsp),%rsi
call   1040 <fputs@plt>
add    $0x1,%r13
cmp    %r13,%rbx
jne    1270 <writePgmPictureASCII+0x110>
jmp    1250 <writePgmPictureASCII+0xf0>
mov    0x10(%rsp),%rdi
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s@n\00"
@.str.1 = [32 x i8] c"# This a binary image from %s@n\00"
@.str.2 = [66 x i8] c"# It is automatic created by Delmas Pierre Binarisation Program@n\00"
@.str.3 = [8 x i8] c"%d %d@n\00"
@.str.4 = [5 x i8] c"%d@n\00"
define void @writePgmPictureASCII(i8* %0, i8* %1, i8* %2, i32** %3, i8* %4, i32 %5, i32 %6, i32 %7) {
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32**
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca [16 x i8]
%20 = alloca i32*
store i8* %0, i8** %9
store i8* %1, i8** %10
store i8* %2, i8** %11
store i32** %3, i32*** %12
store i8* %4, i8** %13
store i32 %5, i32* %14
store i32 %6, i32* %15
store i32 %7, i32* %16
store i32 0, i32* %17
store i32 0, i32* %18
%21 = bitcast [16 x i8]* %19 to i8*
%22 = load i8*, i8** %10
%23 = load i8*, i8** %11
%24 = call i32* @fopen(i8* %22, i8* %23)
store i32* %24, i32** %20
%25 = load i32*, i32** %20
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %77
27: ; preds = %8
%28 = load i32*, i32** %20
%29 = load i8*, i8** %13
%30 = call i32 (i32*, i8*, ...) @fprintf(i32* %28, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %29)
%31 = load i32*, i32** %20
%32 = load i8*, i8** %9
%33 = call i32 (i32*, i8*, ...) @fprintf(i32* %31, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %32)
%34 = load i32*, i32** %20
%35 = call i32 (i32*, i8*, ...) @fprintf(i32* %34, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32*, i32** %20
%37 = load i32, i32* %14
%38 = load i32, i32* %15
%39 = call i32 (i32*, i8*, ...) @fprintf(i32* %36, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %37, i32 %38)
%40 = load i32*, i32** %20
%41 = load i32, i32* %16
%42 = call i32 (i32*, i8*, ...) @fprintf(i32* %40, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %41)
store i32 0, i32* %17
br label %43
43: ; preds = %71, %27
%44 = load i32, i32* %17
%45 = load i32, i32* %14
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %74
47: ; preds = %43
store i32 0, i32* %18
br label %48
48: ; preds = %67, %47
%49 = load i32, i32* %18
%50 = load i32, i32* %15
%51 = icmp slt i32 %49, %50
br i1 %51, label %52, label %70
52: ; preds = %48
%53 = getelementptr inbounds [16 x i8], [16 x i8]* %19, i64 0, i64 0
%54 = load i32**, i32*** %12
%55 = load i32, i32* %17
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32*, i32** %54, i64 %56
%58 = load i32*, i32** %57
%59 = load i32, i32* %18
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i32, i32* %58, i64 %60
%62 = load i32, i32* %61
%63 = call i32 @sprintf(i8* %53, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %62)
%64 = getelementptr inbounds [16 x i8], [16 x i8]* %19, i64 0, i64 0
%65 = load i32*, i32** %20
%66 = call i32 @fputs(i8* %64, i32* %65)
br label %67
67: ; preds = %52
%68 = load i32, i32* %18
%69 = add nsw i32 %68, 1
store i32 %69, i32* %18
br label %48
70: ; preds = %48
br label %71
71: ; preds = %70
%72 = load i32, i32* %17
%73 = add nsw i32 %72, 1
store i32 %73, i32* %17
br label %43
74: ; preds = %43
%75 = load i32*, i32** %20
%76 = call i32 @fclose(i32* %75)
br label %77
77: ; preds = %74, %8
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s@n\00"
@.str.1 = [32 x i8] c"# This a binary image from %s@n\00"
@.str.2 = [66 x i8] c"# It is automatic created by Delmas Pierre Binarisation Program@n\00"
@.str.3 = [8 x i8] c"%d %d@n\00"
@.str.4 = [5 x i8] c"%d@n\00"
define void @writePgmPictureASCII(i8* %0, i8* nocapture %1, i8* nocapture %2, i32** nocapture %3, i8* %4, i32 %5, i32 %6, i32 %7) {
%9 = alloca [16 x i8]
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%11 = tail call i32* @fopen(i8* %1, i8* %2)
%12 = icmp eq i32* %11, null
br i1 %12, label %42, label %13
13: ; preds = %8
%14 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %4)
%15 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%16 = tail call i64 @fwrite(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.2, i64 0, i64 0), i64 65, i64 1, i32* %11)
%17 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %5, i32 %6)
%18 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %7)
%19 = icmp sgt i32 %5, 0
br i1 %19, label %20, label %40
20: ; preds = %13
%21 = icmp sgt i32 %6, 0
%22 = zext i32 %5 to i64
%23 = zext i32 %6 to i64
br label %24
24: ; preds = %20, %37
%25 = phi i64 [ 0, %20 ], [ %38, %37 ]
br i1 %21, label %26, label %37
26: ; preds = %24
%27 = getelementptr inbounds i32*, i32** %3, i64 %25
br label %28
28: ; preds = %26, %28
%29 = phi i64 [ 0, %26 ], [ %35, %28 ]
%30 = load i32*, i32** %27
%31 = getelementptr inbounds i32, i32* %30, i64 %29
%32 = load i32, i32* %31
%33 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %32)
%34 = call i32 @fputs(i8* %10, i32* %11)
%35 = add nuw nsw i64 %29, 1
%36 = icmp eq i64 %35, %23
br i1 %36, label %37, label %28
37: ; preds = %28, %24
%38 = add nuw nsw i64 %25, 1
%39 = icmp eq i64 %38, %22
br i1 %39, label %40, label %24
40: ; preds = %37, %13
%41 = tail call i32 @fclose(i32* %11)
br label %42
42: ; preds = %40, %8
ret void
}