#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*/ free(int *);
int *malloc(int);
int /*<<< orphan*/ memcpy(int *, int *, int);
int /*<<< orphan*/ memset(int *, int /*<<< orphan*/, int);
int *add_tracks(int *old_pattern, int pattern_length, int old_num_tracks,
                int new_num_tracks) {
  int *new_pattern = malloc(new_num_tracks * pattern_length * sizeof(int));
  for (int p = 0; p < pattern_length; p++) {
    memcpy(new_pattern + p * new_num_tracks, old_pattern + p * old_num_tracks,
           sizeof(int) * old_num_tracks);
    memset(new_pattern + p * new_num_tracks + old_num_tracks, 0,
           sizeof(int) * (new_num_tracks - old_num_tracks));
  }
  free(old_pattern);
  return new_pattern;
}


x86 assembly (clang, -O0):
<add_tracks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
imul   -0xc(%rbp),%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11f9 <add_tracks+0xb9>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%eax
imul   -0x14(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x24(%rbp),%eax
imul   -0x10(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%eax
imul   -0x14(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%rdi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x14(%rbp),%eax
sub    -0x10(%rbp),%eax
cltq
shl    $0x2,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1174 <add_tracks+0x34>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_tracks>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %esi,%ebx
mov    %rdi,0x10(%rsp)
mov    %esi,%edi
mov    %ecx,%r15d
imul   %ecx,%edi
shl    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,0x8(%rsp)
test   %ebx,%ebx
jle    11c9 <add_tracks+0x89>
movslq %r14d,%r14
movslq %r15d,%r13
sub    %r14d,%r15d
lea    0x0(,%r14,4),%eax
mov    %eax,0x4(%rsp)
shl    $0x2,%r15d
mov    %ebx,%r12d
shl    $0x2,%r14
shl    $0x2,%r13
mov    0x8(%rsp),%rbx
mov    0x10(%rsp),%rbp
nop
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    0x4(%rsp),%edx
call   1050 <memcpy@plt>
lea    (%rbx,%r14,1),%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1040 <memset@plt>
add    %r14,%rbp
add    %r13,%rbx
add    $0xffffffffffffffff,%r12
jne    11a0 <add_tracks+0x60>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32* @add_tracks(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = load i32, i32* %8
%12 = load i32, i32* %6
%13 = mul nsw i32 %11, %12
%14 = sext i32 %13 to i64
%15 = mul i64 %14, 4
%16 = trunc i64 %15 to i32
%17 = call i32* @malloc(i32 %16)
store i32* %17, i32** %9
store i32 0, i32* %10
br label %18
18: ; preds = %56, %4
%19 = load i32, i32* %10
%20 = load i32, i32* %6
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %59
22: ; preds = %18
%23 = load i32*, i32** %9
%24 = load i32, i32* %10
%25 = load i32, i32* %8
%26 = mul nsw i32 %24, %25
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %23, i64 %27
%29 = load i32*, i32** %5
%30 = load i32, i32* %10
%31 = load i32, i32* %7
%32 = mul nsw i32 %30, %31
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %29, i64 %33
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = mul i64 4, %36
%38 = trunc i64 %37 to i32
%39 = call i32 @memcpy(i32* %28, i32* %34, i32 %38)
%40 = load i32*, i32** %9
%41 = load i32, i32* %10
%42 = load i32, i32* %8
%43 = mul nsw i32 %41, %42
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i32, i32* %40, i64 %44
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
%49 = load i32, i32* %8
%50 = load i32, i32* %7
%51 = sub nsw i32 %49, %50
%52 = sext i32 %51 to i64
%53 = mul i64 4, %52
%54 = trunc i64 %53 to i32
%55 = call i32 @memset(i32* %48, i32 0, i32 %54)
br label %56
56: ; preds = %22
%57 = load i32, i32* %10
%58 = add nsw i32 %57, 1
store i32 %58, i32* %10
br label %18
59: ; preds = %18
%60 = load i32*, i32** %5
%61 = call i32 @free(i32* %60)
%62 = load i32*, i32** %9
ret i32* %62
}

LLVM IR (-O2):
define i32* @add_tracks(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = shl i32 %1, 2
%6 = mul i32 %5, %3
%7 = tail call i32* @malloc(i32 %6)
%8 = icmp sgt i32 %1, 0
br i1 %8, label %9, label %16
9: ; preds = %4
%10 = sext i32 %2 to i64
%11 = shl i32 %2, 2
%12 = sub nsw i32 %3, %2
%13 = shl i32 %12, 2
%14 = sext i32 %3 to i64
%15 = zext i32 %1 to i64
br label %18
16: ; preds = %18, %4
%17 = tail call i32 @free(i32* %0)
ret i32* %7
18: ; preds = %9, %18
%19 = phi i64 [ 0, %9 ], [ %27, %18 ]
%20 = mul nsw i64 %19, %14
%21 = getelementptr inbounds i32, i32* %7, i64 %20
%22 = mul nsw i64 %19, %10
%23 = getelementptr inbounds i32, i32* %0, i64 %22
%24 = tail call i32 @memcpy(i32* %21, i32* %23, i32 %11)
%25 = getelementptr inbounds i32, i32* %21, i64 %10
%26 = tail call i32 @memset(i32* %25, i32 0, i32 %13)
%27 = add nuw nsw i64 %19, 1
%28 = icmp eq i64 %27, %15
br i1 %28, label %16, label %18
}