#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 uint32_t;

/* Variables and functions */
uint32_t transpose_n(int n, uint32_t *matrix) {
  uint32_t out = 0;
  int j;
  for (j = 0; j < 32; ++j) {
    if (matrix[j] & (1 << n))
      out |= (1 << j);
  }
  return out;
}


x86 assembly (clang, -O0):
<transpose_n>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x20,-0x18(%rbp)
jge    1168 <transpose_n+0x68>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x4(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1155 <transpose_n+0x55>
mov    -0x18(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    115a <transpose_n+0x5a>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1119 <transpose_n+0x19>
mov    -0x14(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<transpose_n>:
mov    %edi,%ecx
mov    $0x1,%eax
shl    %cl,%eax
movd   %eax,%xmm0
pshufd $0x0,%xmm0,%xmm0
movdqu (%rsi),%xmm4
movdqu 0x10(%rsi),%xmm3
movdqu 0x20(%rsi),%xmm5
movdqu 0x30(%rsi),%xmm2
pand   %xmm0,%xmm4
pxor   %xmm1,%xmm1
pcmpeqd %xmm1,%xmm4
pandn  0xec7(%rip),%xmm4
pand   %xmm0,%xmm3
pcmpeqd %xmm1,%xmm3
pandn  0xec7(%rip),%xmm3
pand   %xmm0,%xmm5
pcmpeqd %xmm1,%xmm5
pandn  0xec7(%rip),%xmm5
pand   %xmm0,%xmm2
pcmpeqd %xmm1,%xmm2
pandn  0xec7(%rip),%xmm2
por    %xmm4,%xmm3
por    %xmm5,%xmm2
movdqu 0x40(%rsi),%xmm4
pand   %xmm0,%xmm4
pcmpeqd %xmm1,%xmm4
pandn  0xeba(%rip),%xmm4
movdqu 0x50(%rsi),%xmm5
pand   %xmm0,%xmm5
pcmpeqd %xmm1,%xmm5
pandn  0xeb5(%rip),%xmm5
por    %xmm3,%xmm2
por    %xmm4,%xmm5
movdqu 0x60(%rsi),%xmm3
pand   %xmm0,%xmm3
pcmpeqd %xmm1,%xmm3
pandn  0xea8(%rip),%xmm3
por    %xmm5,%xmm3
por    %xmm2,%xmm3
movdqu 0x70(%rsi),%xmm2
pand   %xmm0,%xmm2
pcmpeqd %xmm1,%xmm2
pandn  0xe9b(%rip),%xmm2
por    %xmm3,%xmm2
pshufd $0xee,%xmm2,%xmm0
por    %xmm2,%xmm0
pshufd $0x55,%xmm0,%xmm1
por    %xmm0,%xmm1
movd   %xmm1,%eax
ret

LLVM IR (-O0):
define i32 @transpose_n(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
store i32 0, i32* %5
store i32 0, i32* %6
br label %7
7: ; preds = %26, %2
%8 = load i32, i32* %6
%9 = icmp slt i32 %8, 32
br i1 %9, label %10, label %29
10: ; preds = %7
%11 = load i32*, i32** %4
%12 = load i32, i32* %6
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i32, i32* %11, i64 %13
%15 = load i32, i32* %14
%16 = load i32, i32* %3
%17 = shl i32 1, %16
%18 = and i32 %15, %17
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %10
%21 = load i32, i32* %6
%22 = shl i32 1, %21
%23 = load i32, i32* %5
%24 = or i32 %23, %22
store i32 %24, i32* %5
br label %25
25: ; preds = %20, %10
br label %26
26: ; preds = %25
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
store i32 %28, i32* %6
br label %7
29: ; preds = %7
%30 = load i32, i32* %5
ret i32 %30
}

LLVM IR (-O2):
define i32 @transpose_n(i32 %0, i32* nocapture %1) {
%3 = shl nuw i32 1, %0
%4 = insertelement <4 x i32> poison, i32 %3, i64 0
%5 = shufflevector <4 x i32> %4, <4 x i32> poison, <4 x i32> zeroinitializer
%6 = bitcast i32* %1 to <4 x i32>*
%7 = load <4 x i32>, <4 x i32>* %6
%8 = and <4 x i32> %7, %5
%9 = icmp eq <4 x i32> %8, zeroinitializer
%10 = select <4 x i1> %9, <4 x i32> zeroinitializer, <4 x i32> <i32 1, i32 2, i32 4, i32 8>
%11 = getelementptr inbounds i32, i32* %1, i64 4
%12 = bitcast i32* %11 to <4 x i32>*
%13 = load <4 x i32>, <4 x i32>* %12
%14 = and <4 x i32> %13, %5
%15 = icmp eq <4 x i32> %14, zeroinitializer
%16 = select <4 x i1> %15, <4 x i32> zeroinitializer, <4 x i32> <i32 16, i32 32, i32 64, i32 128>
%17 = or <4 x i32> %16, %10
%18 = getelementptr inbounds i32, i32* %1, i64 8
%19 = bitcast i32* %18 to <4 x i32>*
%20 = load <4 x i32>, <4 x i32>* %19
%21 = and <4 x i32> %20, %5
%22 = icmp eq <4 x i32> %21, zeroinitializer
%23 = select <4 x i1> %22, <4 x i32> zeroinitializer, <4 x i32> <i32 256, i32 512, i32 1024, i32 2048>
%24 = or <4 x i32> %23, %17
%25 = getelementptr inbounds i32, i32* %1, i64 12
%26 = bitcast i32* %25 to <4 x i32>*
%27 = load <4 x i32>, <4 x i32>* %26
%28 = and <4 x i32> %27, %5
%29 = icmp eq <4 x i32> %28, zeroinitializer
%30 = select <4 x i1> %29, <4 x i32> zeroinitializer, <4 x i32> <i32 4096, i32 8192, i32 16384, i32 32768>
%31 = or <4 x i32> %30, %24
%32 = getelementptr inbounds i32, i32* %1, i64 16
%33 = bitcast i32* %32 to <4 x i32>*
%34 = load <4 x i32>, <4 x i32>* %33
%35 = and <4 x i32> %34, %5
%36 = icmp eq <4 x i32> %35, zeroinitializer
%37 = select <4 x i1> %36, <4 x i32> zeroinitializer, <4 x i32> <i32 65536, i32 131072, i32 262144, i32 524288>
%38 = or <4 x i32> %37, %31
%39 = getelementptr inbounds i32, i32* %1, i64 20
%40 = bitcast i32* %39 to <4 x i32>*
%41 = load <4 x i32>, <4 x i32>* %40
%42 = and <4 x i32> %41, %5
%43 = icmp eq <4 x i32> %42, zeroinitializer
%44 = select <4 x i1> %43, <4 x i32> zeroinitializer, <4 x i32> <i32 1048576, i32 2097152, i32 4194304, i32 8388608>
%45 = or <4 x i32> %44, %38
%46 = getelementptr inbounds i32, i32* %1, i64 24
%47 = bitcast i32* %46 to <4 x i32>*
%48 = load <4 x i32>, <4 x i32>* %47
%49 = and <4 x i32> %48, %5
%50 = icmp eq <4 x i32> %49, zeroinitializer
%51 = select <4 x i1> %50, <4 x i32> zeroinitializer, <4 x i32> <i32 16777216, i32 33554432, i32 67108864, i32 134217728>
%52 = or <4 x i32> %51, %45
%53 = getelementptr inbounds i32, i32* %1, i64 28
%54 = bitcast i32* %53 to <4 x i32>*
%55 = load <4 x i32>, <4 x i32>* %54
%56 = and <4 x i32> %55, %5
%57 = icmp eq <4 x i32> %56, zeroinitializer
%58 = select <4 x i1> %57, <4 x i32> zeroinitializer, <4 x i32> <i32 268435456, i32 536870912, i32 1073741824, i32 -2147483648>
%59 = or <4 x i32> %58, %52
%60 = call i32 @llvm.vector.reduce.or.v4i32(<4 x i32> %59)
ret i32 %60
}