#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 */

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, char *, double);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int matAssemble2D(double *m, int d1, int d2, char *string) {
  int i, j;
  strcpy(string, "{{");
  for (i = 0; i < d1; i++) {
    for (j = 0; j < d2; j++) {
      sprintf(string, "%s%g,", string, m[i * d2 + j]);
    }
    string[strlen(string) - 1] = '\0';
    strcat(string, "},{");
  }
  string[strlen(string) - 2] = '\0';
  strcat(string, "}");
  return 0;
}


x86 assembly (clang, -O0):
<matAssemble2D>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe9f(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1203 <matAssemble2D+0xc3>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11c4 <matAssemble2D+0x84>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
imul   -0x10(%rbp),%ecx
add    -0x20(%rbp),%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xe52(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1180 <matAssemble2D+0x40>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
lea    0xe19(%rip),%rsi
call   1050 <strcat@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    116d <matAssemble2D+0x2d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x2,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
lea    0xdde(%rip),%rsi
call   1050 <strcat@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<matAssemble2D>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rbx
mov    %edx,0x4(%rsp)
mov    %esi,%r15d
mov    %rdi,%rbp
lea    0xe9e(%rip),%rsi
mov    %rcx,%rdi
call   1030 <strcpy@plt>
test   %r15d,%r15d
jle    1200 <matAssemble2D+0xc0>
movslq 0x4(%rsp),%rax
mov    %r15d,%ecx
mov    %rcx,0x8(%rsp)
mov    %eax,%r14d
shl    $0x3,%rax
mov    %rax,0x10(%rsp)
xor    %r12d,%r12d
lea    0xe6d(%rip),%r15
jmp    11ce <matAssemble2D+0x8e>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbx,%rdi
lea    0xe50(%rip),%rsi
call   1050 <strcat@plt>
add    $0x1,%r12
add    0x10(%rsp),%rbp
cmp    0x8(%rsp),%r12
je     1200 <matAssemble2D+0xc0>
cmpl   $0x0,0x4(%rsp)
jle    11a0 <matAssemble2D+0x60>
xor    %r13d,%r13d
nopl   0x0(%rax,%rax,1)
movsd  0x0(%rbp,%r13,8),%xmm0
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1060 <sprintf@plt>
add    $0x1,%r13
cmp    %r13,%r14
jne    11e0 <matAssemble2D+0xa0>
jmp    11a0 <matAssemble2D+0x60>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x2(%rax,%rbx,1)
lea    0xdf7(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcat@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"{{\00"
@.str.1 = [6 x i8] c"%s%g,\00"
@.str.2 = [4 x i8] c"},{\00"
@.str.3 = [2 x i8] c"}\00"
define i32 @matAssemble2D(double* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca double*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store double* %0, double** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%11 = load i8*, i8** %8
%12 = call i32 @strcpy(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %9
br label %13
13: ; preds = %47, %4
%14 = load i32, i32* %9
%15 = load i32, i32* %6
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %50
17: ; preds = %13
store i32 0, i32* %10
br label %18
18: ; preds = %35, %17
%19 = load i32, i32* %10
%20 = load i32, i32* %7
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %38
22: ; preds = %18
%23 = load i8*, i8** %8
%24 = load i8*, i8** %8
%25 = load double*, double** %5
%26 = load i32, i32* %9
%27 = load i32, i32* %7
%28 = mul nsw i32 %26, %27
%29 = load i32, i32* %10
%30 = add nsw i32 %28, %29
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds double, double* %25, i64 %31
%33 = load double, double* %32
%34 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %24, double %33)
br label %35
35: ; preds = %22
%36 = load i32, i32* %10
%37 = add nsw i32 %36, 1
store i32 %37, i32* %10
br label %18
38: ; preds = %18
%39 = load i8*, i8** %8
%40 = load i8*, i8** %8
%41 = call i32 @strlen(i8* %40)
%42 = sub nsw i32 %41, 1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %39, i64 %43
store i8 0, i8* %44
%45 = load i8*, i8** %8
%46 = call i32 @strcat(i8* %45, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %47
47: ; preds = %38
%48 = load i32, i32* %9
%49 = add nsw i32 %48, 1
store i32 %49, i32* %9
br label %13
50: ; preds = %13
%51 = load i8*, i8** %8
%52 = load i8*, i8** %8
%53 = call i32 @strlen(i8* %52)
%54 = sub nsw i32 %53, 2
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %51, i64 %55
store i8 0, i8* %56
%57 = load i8*, i8** %8
%58 = call i32 @strcat(i8* %57, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"{{\00"
@.str.1 = [6 x i8] c"%s%g,\00"
@.str.2 = [4 x i8] c"},{\00"
@.str.3 = [2 x i8] c"}\00"
define i32 @matAssemble2D(double* nocapture %0, i32 %1, i32 %2, i8* %3) {
%5 = tail call i32 @strcpy(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp sgt i32 %1, 0
br i1 %6, label %7, label %32
7: ; preds = %4
%8 = icmp sgt i32 %2, 0
%9 = sext i32 %2 to i64
%10 = zext i32 %1 to i64
%11 = zext i32 %2 to i64
br label %12
12: ; preds = %7, %24
%13 = phi i64 [ 0, %7 ], [ %30, %24 ]
br i1 %8, label %14, label %24
14: ; preds = %12
%15 = mul nsw i64 %13, %9
br label %16
16: ; preds = %14, %16
%17 = phi i64 [ 0, %14 ], [ %22, %16 ]
%18 = add nsw i64 %17, %15
%19 = getelementptr inbounds double, double* %0, i64 %18
%20 = load double, double* %19
%21 = tail call i32 @sprintf(i8* dereferenceable(1) %3, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %3, double %20)
%22 = add nuw nsw i64 %17, 1
%23 = icmp eq i64 %22, %11
br i1 %23, label %24, label %16
24: ; preds = %16, %12
%25 = tail call i32 @strlen(i8* %3)
%26 = add nsw i32 %25, -1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %3, i64 %27
store i8 0, i8* %28
%29 = tail call i32 @strcat(i8* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%30 = add nuw nsw i64 %13, 1
%31 = icmp eq i64 %30, %10
br i1 %31, label %32, label %12
32: ; preds = %24, %4
%33 = tail call i32 @strlen(i8* %3)
%34 = add nsw i32 %33, -2
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %3, i64 %35
store i8 0, i8* %36
%37 = tail call i32 @strcat(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}