#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 rand();
void blewadd(unsigned char **p, unsigned num, int reg) {
  unsigned char *buf = *p;
  switch (rand() % 3) {
  case 0:
    buf[0] = 0x81;
    buf[1] = 0xc0 + reg;
    *((unsigned *)(&buf[2])) = num;
    break;
  case 1:
    buf[0] = 0x81;
    buf[1] = 0xe8 + reg;
    *((unsigned *)(&buf[2])) = 0 - num;
    break;
  case 2:
    buf[0] = 0x8d;
    buf[1] = 0x80 + reg + reg * 8;
    *((unsigned *)(&buf[2])) = num;
    break;
  }
  (*p) += 6;
}


x86 assembly (clang, -O0):
<blewadd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
call   1030 <rand@plt>
movslq %eax,%rcx
imul   $0x55555556,%rcx,%rdx
mov    %rdx,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rdx
add    %ecx,%edx
mov    %edx,%ecx
lea    (%rcx,%rcx,2),%ecx
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
je     1182 <blewadd+0x72>
jmp    1160 <blewadd+0x50>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
je     11a9 <blewadd+0x99>
jmp    1171 <blewadd+0x61>
mov    -0x1c(%rbp),%eax
sub    $0x2,%eax
je     11d2 <blewadd+0xc2>
jmp    11fc <blewadd+0xec>
mov    -0x18(%rbp),%rax
movb   $0x81,(%rax)
mov    -0x10(%rbp),%eax
add    $0xc0,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x2(%rax)
jmp    11fc <blewadd+0xec>
mov    -0x18(%rbp),%rax
movb   $0x81,(%rax)
mov    -0x10(%rbp),%eax
add    $0xe8,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,0x1(%rax)
xor    %ecx,%ecx
sub    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x2(%rax)
jmp    11fc <blewadd+0xec>
mov    -0x18(%rbp),%rax
movb   $0x8d,(%rax)
mov    -0x10(%rbp),%eax
add    $0x80,%eax
mov    -0x10(%rbp),%ecx
shl    $0x3,%ecx
add    %ecx,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x2(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
add    $0x6,%rcx
mov    %rcx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<blewadd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r14d
mov    %rdi,%r15
mov    (%rdi),%rbx
xor    %eax,%eax
call   1030 <rand@plt>
cltq
imul   $0x55555556,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,2),%ecx
sub    %ecx,%eax
cmp    $0x2,%eax
je     115f <blewadd+0x4f>
cmp    $0x1,%eax
je     116d <blewadd+0x5d>
test   %eax,%eax
jne    117f <blewadd+0x6f>
movb   $0x81,(%rbx)
add    $0xc0,%bpl
mov    %bpl,0x1(%rbx)
jmp    117b <blewadd+0x6b>
movb   $0x8d,(%rbx)
lea    0x0(%rbp,%rbp,8),%eax
add    $0x80,%al
mov    %al,0x1(%rbx)
jmp    117b <blewadd+0x6b>
movb   $0x81,(%rbx)
add    $0xe8,%bpl
mov    %bpl,0x1(%rbx)
neg    %r14d
mov    %r14d,0x2(%rbx)
addq   $0x6,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @blewadd(i8** %0, i32 %1, i32 %2) {
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8** %0, i8*** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = load i8**, i8*** %4
%9 = load i8*, i8** %8
store i8* %9, i8** %7
%10 = call i32 (...) @rand()
%11 = srem i32 %10, 3
switch i32 %11, label %52 [
i32 0, label %12
i32 1, label %24
i32 2, label %37
]
12: ; preds = %3
%13 = load i8*, i8** %7
%14 = getelementptr inbounds i8, i8* %13, i64 0
store i8 -127, i8* %14
%15 = load i32, i32* %6
%16 = add nsw i32 192, %15
%17 = trunc i32 %16 to i8
%18 = load i8*, i8** %7
%19 = getelementptr inbounds i8, i8* %18, i64 1
store i8 %17, i8* %19
%20 = load i32, i32* %5
%21 = load i8*, i8** %7
%22 = getelementptr inbounds i8, i8* %21, i64 2
%23 = bitcast i8* %22 to i32*
store i32 %20, i32* %23
br label %52
24: ; preds = %3
%25 = load i8*, i8** %7
%26 = getelementptr inbounds i8, i8* %25, i64 0
store i8 -127, i8* %26
%27 = load i32, i32* %6
%28 = add nsw i32 232, %27
%29 = trunc i32 %28 to i8
%30 = load i8*, i8** %7
%31 = getelementptr inbounds i8, i8* %30, i64 1
store i8 %29, i8* %31
%32 = load i32, i32* %5
%33 = sub i32 0, %32
%34 = load i8*, i8** %7
%35 = getelementptr inbounds i8, i8* %34, i64 2
%36 = bitcast i8* %35 to i32*
store i32 %33, i32* %36
br label %52
37: ; preds = %3
%38 = load i8*, i8** %7
%39 = getelementptr inbounds i8, i8* %38, i64 0
store i8 -115, i8* %39
%40 = load i32, i32* %6
%41 = add nsw i32 128, %40
%42 = load i32, i32* %6
%43 = mul nsw i32 %42, 8
%44 = add nsw i32 %41, %43
%45 = trunc i32 %44 to i8
%46 = load i8*, i8** %7
%47 = getelementptr inbounds i8, i8* %46, i64 1
store i8 %45, i8* %47
%48 = load i32, i32* %5
%49 = load i8*, i8** %7
%50 = getelementptr inbounds i8, i8* %49, i64 2
%51 = bitcast i8* %50 to i32*
store i32 %48, i32* %51
br label %52
52: ; preds = %3, %37, %24, %12
%53 = load i8**, i8*** %4
%54 = load i8*, i8** %53
%55 = getelementptr inbounds i8, i8* %54, i64 6
store i8* %55, i8** %53
ret void
}

LLVM IR (-O2):
define void @blewadd(i8** nocapture %0, i32 %1, i32 %2) {
%4 = load i8*, i8** %0
%5 = tail call i32 (...) @rand()
%6 = srem i32 %5, 3
switch i32 %6, label %26 [
i32 0, label %7
i32 1, label %11
i32 2, label %16
]
7: ; preds = %3
store i8 -127, i8* %4
%8 = trunc i32 %2 to i8
%9 = add i8 %8, -64
%10 = getelementptr inbounds i8, i8* %4, i64 1
store i8 %9, i8* %10
br label %22
11: ; preds = %3
store i8 -127, i8* %4
%12 = trunc i32 %2 to i8
%13 = add i8 %12, -24
%14 = getelementptr inbounds i8, i8* %4, i64 1
store i8 %13, i8* %14
%15 = sub i32 0, %1
br label %22
16: ; preds = %3
store i8 -115, i8* %4
%17 = add nsw i32 %2, 128
%18 = shl nsw i32 %2, 3
%19 = add nsw i32 %17, %18
%20 = trunc i32 %19 to i8
%21 = getelementptr inbounds i8, i8* %4, i64 1
store i8 %20, i8* %21
br label %22
22: ; preds = %7, %11, %16
%23 = phi i32 [ %1, %16 ], [ %15, %11 ], [ %1, %7 ]
%24 = getelementptr inbounds i8, i8* %4, i64 2
%25 = bitcast i8* %24 to i32*
store i32 %23, i32* %25
br label %26
26: ; preds = %22, %3
%27 = load i8*, i8** %0
%28 = getelementptr inbounds i8, i8* %27, i64 6
store i8* %28, i8** %0
ret void
}