#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 enum rc_proto { ____Placeholder_rc_proto } rc_proto;

/* Variables and functions */
int xbox_dvd_encode(enum rc_proto proto, unsigned scancode, unsigned *buf) {
  int len = 0;
  buf[len++] = 4000;
  buf[len++] = 3900;
  scancode &= 0xfff;
  scancode |= (~scancode << 12) & 0xfff000;
  for (int i = 23; i >= 0; i--) {
    buf[len++] = 550;
    if (scancode & (1 << i))
      buf[len++] = 1900;
    else
      buf[len++] = 900;
  }
  buf[len++] = 550;
  return len;
}


x86 assembly (clang, -O0):
<xbox_dvd_encode>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x14(%rbp)
movslq %ecx,%rcx
movl   $0xfa0,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x14(%rbp)
movslq %ecx,%rcx
movl   $0xf3c,(%rax,%rcx,4)
mov    -0x8(%rbp),%eax
and    $0xfff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
xor    $0xffffffff,%eax
shl    $0xc,%eax
and    $0xfff000,%eax
or     -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
movl   $0x17,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     11f4 <xbox_dvd_encode+0xf4>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x14(%rbp)
movslq %ecx,%rcx
movl   $0x226,(%rax,%rcx,4)
mov    -0x8(%rbp),%eax
mov    -0x18(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     11c8 <xbox_dvd_encode+0xc8>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x14(%rbp)
movslq %ecx,%rcx
movl   $0x76c,(%rax,%rcx,4)
jmp    11e1 <xbox_dvd_encode+0xe1>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x14(%rbp)
movslq %ecx,%rcx
movl   $0x384,(%rax,%rcx,4)
jmp    11e6 <xbox_dvd_encode+0xe6>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    116d <xbox_dvd_encode+0x6d>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x14(%rbp)
movslq %ecx,%rcx
movl   $0x226,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<xbox_dvd_encode>:
movabs $0xf3c00000fa0,%rax
mov    %rax,(%rdx)
mov    %esi,%eax
and    $0xfff,%eax
not    %esi
shl    $0xc,%esi
and    $0xfff000,%esi
or     %eax,%esi
lea    0x14(%rdx),%rax
mov    $0x17,%ecx
mov    $0x384,%r8d
movl   $0x226,-0xc(%rax)
bt     %ecx,%esi
mov    $0x76c,%edi
cmovae %r8d,%edi
mov    %edi,-0x8(%rax)
add    $0xffffffff,%ecx
bt     %ecx,%esi
mov    $0x76c,%edi
cmovae %r8d,%edi
movl   $0x226,-0x4(%rax)
mov    %edi,(%rax)
add    $0x10,%rax
sub    $0x1,%ecx
jae    1130 <xbox_dvd_encode+0x30>
movl   $0x226,0xc8(%rdx)
mov    $0x33,%eax
ret

LLVM IR (-O0):
define i32 @xbox_dvd_encode(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32* %2, i32** %6
store i32 0, i32* %7
%9 = load i32*, i32** %6
%10 = load i32, i32* %7
%11 = add nsw i32 %10, 1
store i32 %11, i32* %7
%12 = sext i32 %10 to i64
%13 = getelementptr inbounds i32, i32* %9, i64 %12
store i32 4000, i32* %13
%14 = load i32*, i32** %6
%15 = load i32, i32* %7
%16 = add nsw i32 %15, 1
store i32 %16, i32* %7
%17 = sext i32 %15 to i64
%18 = getelementptr inbounds i32, i32* %14, i64 %17
store i32 3900, i32* %18
%19 = load i32, i32* %5
%20 = and i32 %19, 4095
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = xor i32 %21, -1
%23 = shl i32 %22, 12
%24 = and i32 %23, 16773120
%25 = load i32, i32* %5
%26 = or i32 %25, %24
store i32 %26, i32* %5
store i32 23, i32* %8
br label %27
27: ; preds = %54, %3
%28 = load i32, i32* %8
%29 = icmp sge i32 %28, 0
br i1 %29, label %30, label %57
30: ; preds = %27
%31 = load i32*, i32** %6
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds i32, i32* %31, i64 %34
store i32 550, i32* %35
%36 = load i32, i32* %5
%37 = load i32, i32* %8
%38 = shl i32 1, %37
%39 = and i32 %36, %38
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %30
%42 = load i32*, i32** %6
%43 = load i32, i32* %7
%44 = add nsw i32 %43, 1
store i32 %44, i32* %7
%45 = sext i32 %43 to i64
%46 = getelementptr inbounds i32, i32* %42, i64 %45
store i32 1900, i32* %46
br label %53
47: ; preds = %30
%48 = load i32*, i32** %6
%49 = load i32, i32* %7
%50 = add nsw i32 %49, 1
store i32 %50, i32* %7
%51 = sext i32 %49 to i64
%52 = getelementptr inbounds i32, i32* %48, i64 %51
store i32 900, i32* %52
br label %53
53: ; preds = %47, %41
br label %54
54: ; preds = %53
%55 = load i32, i32* %8
%56 = add nsw i32 %55, -1
store i32 %56, i32* %8
br label %27
57: ; preds = %27
%58 = load i32*, i32** %6
%59 = load i32, i32* %7
%60 = add nsw i32 %59, 1
store i32 %60, i32* %7
%61 = sext i32 %59 to i64
%62 = getelementptr inbounds i32, i32* %58, i64 %61
store i32 550, i32* %62
%63 = load i32, i32* %7
ret i32 %63
}

LLVM IR (-O2):
define i32 @xbox_dvd_encode(i32 %0, i32 %1, i32* nocapture writeonly %2) {
store i32 4000, i32* %2
%4 = getelementptr inbounds i32, i32* %2, i64 1
store i32 3900, i32* %4
%5 = and i32 %1, 4095
%6 = xor i32 %5, -1
%7 = shl nsw i32 %6, 12
%8 = and i32 %7, 16773120
%9 = or i32 %8, %5
br label %12
10: ; preds = %12
%11 = getelementptr inbounds i32, i32* %2, i64 50
store i32 550, i32* %11
ret i32 51
12: ; preds = %12, %3
%13 = phi i64 [ 2, %3 ], [ %31, %12 ]
%14 = phi i32 [ 23, %3 ], [ %32, %12 ]
%15 = or i64 %13, 1
%16 = getelementptr inbounds i32, i32* %2, i64 %13
store i32 550, i32* %16
%17 = shl nuw i32 1, %14
%18 = and i32 %17, %9
%19 = icmp eq i32 %18, 0
%20 = select i1 %19, i32 900, i32 1900
%21 = getelementptr inbounds i32, i32* %2, i64 %15
store i32 %20, i32* %21
%22 = add nuw nsw i64 %13, 2
%23 = add nsw i32 %14, -1
%24 = or i64 %22, 1
%25 = getelementptr inbounds i32, i32* %2, i64 %22
store i32 550, i32* %25
%26 = shl nuw i32 1, %23
%27 = and i32 %26, %9
%28 = icmp eq i32 %27, 0
%29 = select i1 %28, i32 900, i32 1900
%30 = getelementptr inbounds i32, i32* %2, i64 %24
store i32 %29, i32* %30
%31 = add nuw nsw i64 %13, 4
%32 = add nsw i32 %14, -2
%33 = icmp eq i32 %23, 0
br i1 %33, label %10, label %12
}