#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 getpid();
int /*<<< orphan*/ sprintf(char *, char *, int);
void write_matrix(long long **m, int n, int process_number, FILE *s) {
  int i, j;
  if (s == NULL) {
    char buf[10];
    sprintf(buf, "%d.txt", process_number);
    s = fopen(buf, "w+");
  }
  fprintf(s, "Process-%d %d\n", process_number, getpid());
  for (i = 0; i < n; ++i) {
    for (j = 0; j < n; ++j) {
      if (j != 0)
        fprintf(s, "\t");
      fprintf(s, "%lld", m[i][j]);
    }
    fprintf(s, "\n");
  }
  if (s == NULL)
    fclose(s);
}


x86 assembly (clang, -O0):
<write_matrix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1198 <write_matrix+0x48>
lea    -0x2a(%rbp),%rdi
mov    -0x10(%rbp),%edx
lea    0xe81(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x2a(%rbp),%rdi
lea    0xe78(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%edx
mov    %eax,%ecx
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    125a <write_matrix+0x10a>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    123a <write_matrix+0xea>
cmpl   $0x0,-0x20(%rbp)
je     1206 <write_matrix+0xb6>
mov    -0x18(%rbp),%rdi
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xdf6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11de <write_matrix+0x8e>
mov    -0x18(%rbp),%rdi
lea    0xdd2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11cb <write_matrix+0x7b>
cmpq   $0x0,-0x18(%rbp)
jne    126e <write_matrix+0x11e>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_matrix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rbp
mov    %edx,%r15d
mov    %esi,%r14d
mov    %rdi,%r12
test   %rcx,%rcx
jne    11a8 <write_matrix+0x48>
lea    0xe7a(%rip),%rsi
lea    0xe(%rsp),%rbp
mov    %rbp,%rdi
mov    %r15d,%edx
call   1080 <sprintf@plt>
lea    0xe6a(%rip),%rsi
mov    %rbp,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe54(%rip),%rsi
mov    %rbp,%rdi
mov    %r15d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %r14d,%r14d
jle    125d <write_matrix+0xfd>
mov    %r14d,%r13d
lea    0xe41(%rip),%r15
xor    %ebx,%ebx
mov    %r14d,0x8(%rsp)
jmp    120b <write_matrix+0xab>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0xa,%edi
mov    %rbp,%rsi
call   1050 <fputc@plt>
add    $0x1,%rbx
cmp    %r13,%rbx
mov    0x8(%rsp),%r14d
je     125d <write_matrix+0xfd>
mov    (%r12,%rbx,8),%rax
mov    (%rax),%rdx
mov    %rbp,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
cmp    $0x1,%r14d
je     11f0 <write_matrix+0x90>
mov    $0x1,%r14d
nopl   0x0(%rax,%rax,1)
mov    $0x9,%edi
mov    %rbp,%rsi
call   1050 <fputc@plt>
mov    (%r12,%rbx,8),%rax
mov    (%rax,%r14,8),%rdx
mov    %rbp,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x1,%r14
cmp    %r14,%r13
jne    1230 <write_matrix+0xd0>
jmp    11f0 <write_matrix+0x90>
test   %rbp,%rbp
je     1271 <write_matrix+0x111>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"%d.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [15 x i8] c"Process-%d %d\0A\00"
@.str.3 = [2 x i8] c"\09\00"
@.str.4 = [5 x i8] c"%lld\00"
@.str.5 = [2 x i8] c"\0A\00"
define void @write_matrix(i64** %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i64**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [10 x i8]
store i64** %0, i64*** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%12 = load i32*, i32** %8
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %20
14: ; preds = %4
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%16 = load i32, i32* %7
%17 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %8
br label %20
20: ; preds = %14, %4
%21 = load i32*, i32** %8
%22 = load i32, i32* %7
%23 = call i32 (...) @getpid()
%24 = call i32 (i32*, i8*, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %22, i32 %23)
store i32 0, i32* %9
br label %25
25: ; preds = %58, %20
%26 = load i32, i32* %9
%27 = load i32, i32* %6
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %61
29: ; preds = %25
store i32 0, i32* %10
br label %30
30: ; preds = %52, %29
%31 = load i32, i32* %10
%32 = load i32, i32* %6
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %55
34: ; preds = %30
%35 = load i32, i32* %10
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32*, i32** %8
%39 = call i32 (i32*, i8*, ...) @fprintf(i32* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %37, %34
%41 = load i32*, i32** %8
%42 = load i64**, i64*** %5
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i64*, i64** %42, i64 %44
%46 = load i64*, i64** %45
%47 = load i32, i32* %10
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i64, i64* %46, i64 %48
%50 = load i64, i64* %49
%51 = call i32 (i32*, i8*, ...) @fprintf(i32* %41, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i64 %50)
br label %52
52: ; preds = %40
%53 = load i32, i32* %10
%54 = add nsw i32 %53, 1
store i32 %54, i32* %10
br label %30
55: ; preds = %30
%56 = load i32*, i32** %8
%57 = call i32 (i32*, i8*, ...) @fprintf(i32* %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %58
58: ; preds = %55
%59 = load i32, i32* %9
%60 = add nsw i32 %59, 1
store i32 %60, i32* %9
br label %25
61: ; preds = %25
%62 = load i32*, i32** %8
%63 = icmp eq i32* %62, null
br i1 %63, label %64, label %67
64: ; preds = %61
%65 = load i32*, i32** %8
%66 = call i32 @fclose(i32* %65)
br label %67
67: ; preds = %64, %61
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"%d.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [15 x i8] c"Process-%d %d\0A\00"
@.str.4 = [5 x i8] c"%lld\00"
define void @write_matrix(i64** nocapture %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca [10 x i8]
%6 = icmp eq i32* %3, null
br i1 %6, label %7, label %11
7: ; preds = %4
%8 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%9 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %2)
%10 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %7, %4
%12 = phi i32* [ %10, %7 ], [ %3, %4 ]
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %2, i32 %13)
%15 = icmp sgt i32 %1, 0
br i1 %15, label %16, label %38
16: ; preds = %11
%17 = zext i32 %1 to i64
%18 = icmp eq i32 %1, 1
br label %19
19: ; preds = %34, %16
%20 = phi i64 [ 0, %16 ], [ %36, %34 ]
%21 = getelementptr inbounds i64*, i64** %0, i64 %20
%22 = load i64*, i64** %21
%23 = load i64, i64* %22
%24 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i64 %23)
br i1 %18, label %34, label %25
25: ; preds = %19, %25
%26 = phi i64 [ %32, %25 ], [ 1, %19 ]
%27 = tail call i32 @fputc(i32 9, i32* %12)
%28 = load i64*, i64** %21
%29 = getelementptr inbounds i64, i64* %28, i64 %26
%30 = load i64, i64* %29
%31 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i64 %30)
%32 = add nuw nsw i64 %26, 1
%33 = icmp eq i64 %32, %17
br i1 %33, label %34, label %25
34: ; preds = %25, %19
%35 = tail call i32 @fputc(i32 10, i32* %12)
%36 = add nuw nsw i64 %20, 1
%37 = icmp eq i64 %36, %17
br i1 %37, label %38, label %19
38: ; preds = %34, %11
%39 = icmp eq i32* %12, null
br i1 %39, label %40, label %42
40: ; preds = %38
%41 = tail call i32 @fclose(i32* null)
br label %42
42: ; preds = %40, %38
ret void
}