#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*/ printf(char *, ...);
void print_char_optim(unsigned char *out, int N, int M) {
  int j, ip, i4;
  unsigned char res, res1;
  for (j = 0; j < M; j++) {
    for (ip = 0; ip < N / 4; ip++) {
      res = out[ip * M + j];
      for (i4 = 0; i4 < 4; i4++) {
        res1 = res % 4;
        printf("%d ", res1);
        res >>= 2;
      }
    }
    printf("\n");
  }
}


x86 assembly (clang, -O0):
<print_char_optim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11ec <print_char_optim+0xdc>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jge    11d0 <print_char_optim+0xc0>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
imul   -0x10(%rbp),%ecx
add    -0x14(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x1d(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x4,-0x1c(%rbp)
jge    11bd <print_char_optim+0xad>
movzbl -0x1d(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %dl,%al
mov    %al,-0x1e(%rbp)
movzbl -0x1e(%rbp),%esi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movzbl -0x1d(%rbp),%eax
sar    $0x2,%eax
mov    %al,-0x1d(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1178 <print_char_optim+0x68>
jmp    11c2 <print_char_optim+0xb2>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    113c <print_char_optim+0x2c>
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1129 <print_char_optim+0x19>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_char_optim>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,(%rsp)
mov    %rdi,0x8(%rsp)
test   %edx,%edx
jle    11f4 <print_char_optim+0xd4>
mov    (%rsp),%rax
lea    0x3(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x2,%ecx
mov    %rcx,0x10(%rsp)
mov    %edx,%r13d
lea    0xea3(%rip),%rbx
xor    %r12d,%r12d
jmp    1189 <print_char_optim+0x69>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x1,%r12
addq   $0x1,0x8(%rsp)
cmp    %r13,%r12
je     11f4 <print_char_optim+0xd4>
cmpl   $0x4,(%rsp)
jl     1170 <print_char_optim+0x50>
mov    0x10(%rsp),%r14
mov    0x8(%rsp),%r15
nopl   0x0(%rax)
movzbl (%r15),%ebp
mov    %ebp,%esi
and    $0x3,%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%esi
shr    $0x2,%esi
and    $0x3,%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%esi
shr    $0x4,%esi
and    $0x3,%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
shr    $0x6,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    %r13,%r15
add    $0xffffffffffffffff,%r14
jne    11a0 <print_char_optim+0x80>
jmp    1170 <print_char_optim+0x50>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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