#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;
struct keymap {
  int dummy;
};

/* Variables and functions */
int keymap_param(struct keymap *, char *, int);
int /*<<< orphan*/ manchester_advance_pulse(int *, int *, int);
int /*<<< orphan*/ manchester_advance_space(int *, int *, int);
void encode_manchester(struct keymap *map, uint32_t scancode, int *buf,
                       int *length) {
  int len = 0, bits, i;
  bits = keymap_param(map, "bits", 14);
  for (i = bits - 1; i >= 0; i--) {
    if (scancode & (1 << i)) {
      manchester_advance_pulse(buf, &len, keymap_param(map, "one_pulse", 888));
      manchester_advance_space(buf, &len, keymap_param(map, "one_space", 888));
    } else {
      manchester_advance_space(buf, &len, keymap_param(map, "zero_space", 888));
      manchester_advance_pulse(buf, &len, keymap_param(map, "zero_pulse", 888));
    }
  }
  *length = (len % 2) ? len : len + 1;
}


x86 assembly (clang, -O0):
<encode_manchester>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea7(%rip),%rsi
mov    $0xe,%edx
call   1050 <keymap_param@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     125b <encode_manchester+0x12b>
mov    -0xc(%rbp),%eax
mov    -0x2c(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     11f0 <encode_manchester+0xc0>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe5f(%rip),%rsi
mov    $0x378,%edx
call   1050 <keymap_param@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%edx
lea    -0x24(%rbp),%rsi
call   1040 <manchester_advance_pulse@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe3d(%rip),%rsi
mov    $0x378,%edx
call   1050 <keymap_param@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%edx
lea    -0x24(%rbp),%rsi
call   1030 <manchester_advance_space@plt>
jmp    1248 <encode_manchester+0x118>
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe16(%rip),%rsi
mov    $0x378,%edx
call   1050 <keymap_param@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%edx
lea    -0x24(%rbp),%rsi
call   1030 <manchester_advance_space@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xdf5(%rip),%rsi
mov    $0x378,%edx
call   1050 <keymap_param@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%edx
lea    -0x24(%rbp),%rsi
call   1040 <manchester_advance_pulse@plt>
jmp    124d <encode_manchester+0x11d>
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
jmp    116f <encode_manchester+0x3f>
mov    -0x24(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     127a <encode_manchester+0x14a>
mov    -0x24(%rbp),%eax
mov    %eax,-0x54(%rbp)
jmp    1283 <encode_manchester+0x153>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_manchester>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %rdx,%r12
mov    %esi,%r15d
mov    %rdi,%rbp
movl   $0x0,0xc(%rsp)
lea    0xea7(%rip),%rsi
mov    $0xe,%edx
call   1050 <keymap_param@plt>
test   %eax,%eax
jle    122e <encode_manchester+0xfe>
mov    %eax,%ebx
mov    %r14,0x10(%rsp)
add    $0x1,%ebx
lea    0xc(%rsp),%r14
lea    0xe8e(%rip),%r13
jmp    11d7 <encode_manchester+0xa7>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xe82(%rip),%rsi
mov    $0x378,%edx
call   1050 <keymap_param@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <manchester_advance_space@plt>
mov    %rbp,%rdi
lea    0xe6c(%rip),%rsi
mov    $0x378,%edx
call   1050 <keymap_param@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <manchester_advance_pulse@plt>
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
jbe    1223 <encode_manchester+0xf3>
lea    -0x2(%rbx),%eax
movzbl %al,%eax
mov    %rbp,%rdi
bt     %eax,%r15d
jae    1190 <encode_manchester+0x60>
lea    0xe18(%rip),%rsi
mov    $0x378,%edx
call   1050 <keymap_param@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <manchester_advance_pulse@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    $0x378,%edx
call   1050 <keymap_param@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <manchester_advance_space@plt>
jmp    11cf <encode_manchester+0x9f>
mov    0xc(%rsp),%eax
mov    0x10(%rsp),%r14
jmp    1230 <encode_manchester+0x100>
xor    %eax,%eax
mov    %eax,%ecx
not    %ecx
and    $0x1,%ecx
add    %eax,%ecx
mov    %ecx,(%r14)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.keymap = type { i32 }
@.str = [5 x i8] c"bits\00"
@.str.1 = [10 x i8] c"one_pulse\00"
@.str.2 = [10 x i8] c"one_space\00"
@.str.3 = [11 x i8] c"zero_space\00"
@.str.4 = [11 x i8] c"zero_pulse\00"
define void @encode_manchester(%struct.keymap* %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca %struct.keymap*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store %struct.keymap* %0, %struct.keymap** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32* %3, i32** %8
store i32 0, i32* %9
%12 = load %struct.keymap*, %struct.keymap** %5
%13 = call i32 @keymap_param(%struct.keymap* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 14)
store i32 %13, i32* %10
%14 = load i32, i32* %10
%15 = sub nsw i32 %14, 1
store i32 %15, i32* %11
br label %16
16: ; preds = %44, %4
%17 = load i32, i32* %11
%18 = icmp sge i32 %17, 0
br i1 %18, label %19, label %47
19: ; preds = %16
%20 = load i32, i32* %6
%21 = load i32, i32* %11
%22 = shl i32 1, %21
%23 = and i32 %20, %22
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %34
25: ; preds = %19
%26 = load i32*, i32** %7
%27 = load %struct.keymap*, %struct.keymap** %5
%28 = call i32 @keymap_param(%struct.keymap* %27, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 888)
%29 = call i32 @manchester_advance_pulse(i32* %26, i32* %9, i32 %28)
%30 = load i32*, i32** %7
%31 = load %struct.keymap*, %struct.keymap** %5
%32 = call i32 @keymap_param(%struct.keymap* %31, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 888)
%33 = call i32 @manchester_advance_space(i32* %30, i32* %9, i32 %32)
br label %43
34: ; preds = %19
%35 = load i32*, i32** %7
%36 = load %struct.keymap*, %struct.keymap** %5
%37 = call i32 @keymap_param(%struct.keymap* %36, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 888)
%38 = call i32 @manchester_advance_space(i32* %35, i32* %9, i32 %37)
%39 = load i32*, i32** %7
%40 = load %struct.keymap*, %struct.keymap** %5
%41 = call i32 @keymap_param(%struct.keymap* %40, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 888)
%42 = call i32 @manchester_advance_pulse(i32* %39, i32* %9, i32 %41)
br label %43
43: ; preds = %34, %25
br label %44
44: ; preds = %43
%45 = load i32, i32* %11
%46 = add nsw i32 %45, -1
store i32 %46, i32* %11
br label %16
47: ; preds = %16
%48 = load i32, i32* %9
%49 = srem i32 %48, 2
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = load i32, i32* %9
br label %56
53: ; preds = %47
%54 = load i32, i32* %9
%55 = add nsw i32 %54, 1
br label %56
56: ; preds = %53, %51
%57 = phi i32 [ %52, %51 ], [ %55, %53 ]
%58 = load i32*, i32** %8
store i32 %57, i32* %58
ret void
}

LLVM IR (-O2):
%struct.keymap = type { i32 }
@.str = [5 x i8] c"bits\00"
@.str.1 = [10 x i8] c"one_pulse\00"
@.str.2 = [10 x i8] c"one_space\00"
@.str.3 = [11 x i8] c"zero_space\00"
@.str.4 = [11 x i8] c"zero_pulse\00"
define void @encode_manchester(%struct.keymap* %0, i32 %1, i32* %2, i32* nocapture writeonly %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%7 = tail call i32 @keymap_param(%struct.keymap* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 14)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %29
9: ; preds = %4, %25
%10 = phi i32 [ %11, %25 ], [ %7, %4 ]
%11 = add nsw i32 %10, -1
%12 = shl nuw i32 1, %11
%13 = and i32 %12, %1
%14 = icmp eq i32 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %9
%16 = call i32 @keymap_param(%struct.keymap* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 888)
%17 = call i32 @manchester_advance_pulse(i32* %2, i32* %5, i32 %16)
%18 = call i32 @keymap_param(%struct.keymap* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 888)
%19 = call i32 @manchester_advance_space(i32* %2, i32* %5, i32 %18)
br label %25
20: ; preds = %9
%21 = call i32 @keymap_param(%struct.keymap* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 888)
%22 = call i32 @manchester_advance_space(i32* %2, i32* %5, i32 %21)
%23 = call i32 @keymap_param(%struct.keymap* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 888)
%24 = call i32 @manchester_advance_pulse(i32* %2, i32* %5, i32 %23)
br label %25
25: ; preds = %15, %20
%26 = icmp ugt i32 %10, 1
br i1 %26, label %9, label %27
27: ; preds = %25
%28 = load i32, i32* %5
br label %29
29: ; preds = %27, %4
%30 = phi i32 [ %28, %27 ], [ 0, %4 ]
%31 = and i32 %30, 1
%32 = xor i32 %31, 1
%33 = add nsw i32 %32, %30
store i32 %33, i32* %3
ret void
}