#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*/ mem_write(char *, int *, unsigned char *, int);
void write_o_instr(char *bin, int *pos, unsigned char opcode, int reg) {
  unsigned char rd = (0x7 & reg);
  unsigned char opcoderd = opcode + rd;
  unsigned char REX = 4 << 4;
  REX |= (0 << 3);
  REX |= (0 << 2);
  REX |= (0 << 1);
  if (reg & 0x8) {
    REX |= (1 << 0);
    mem_write(bin, pos, &REX, 1);
  }
  mem_write(bin, pos, &opcoderd, 1);
}


x86 assembly (clang, -O0):
<write_o_instr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x18(%rbp),%eax
and    $0x7,%eax
mov    %al,-0x19(%rbp)
movzbl -0x11(%rbp),%eax
movzbl -0x19(%rbp),%ecx
add    %ecx,%eax
mov    %al,-0x1a(%rbp)
movb   $0x40,-0x1b(%rbp)
movzbl -0x1b(%rbp),%eax
or     $0x0,%eax
mov    %al,-0x1b(%rbp)
movzbl -0x1b(%rbp),%eax
or     $0x0,%eax
mov    %al,-0x1b(%rbp)
movzbl -0x1b(%rbp),%eax
or     $0x0,%eax
mov    %al,-0x1b(%rbp)
mov    -0x18(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
je     118f <write_o_instr+0x7f>
movzbl -0x1b(%rbp),%eax
or     $0x1,%eax
mov    %al,-0x1b(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x1b(%rbp),%rdx
mov    $0x1,%ecx
call   1030 <mem_write@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x1a(%rbp),%rdx
mov    $0x1,%ecx
call   1030 <mem_write@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_o_instr>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %ecx,%eax
and    $0x7,%al
add    %dl,%al
mov    %al,0x7(%rsp)
movb   $0x40,0x6(%rsp)
test   $0x8,%cl
je     1148 <write_o_instr+0x38>
movb   $0x41,0x6(%rsp)
lea    0x6(%rsp),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x1,%ecx
call   1030 <mem_write@plt>
lea    0x7(%rsp),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x1,%ecx
call   1030 <mem_write@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define void @write_o_instr(i8* %0, i32* %1, i8 zeroext %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8
%8 = alloca i32
%9 = alloca i8
%10 = alloca i8
%11 = alloca i8
store i8* %0, i8** %5
store i32* %1, i32** %6
store i8 %2, i8* %7
store i32 %3, i32* %8
%12 = load i32, i32* %8
%13 = and i32 7, %12
%14 = trunc i32 %13 to i8
store i8 %14, i8* %9
%15 = load i8, i8* %7
%16 = zext i8 %15 to i32
%17 = load i8, i8* %9
%18 = zext i8 %17 to i32
%19 = add nsw i32 %16, %18
%20 = trunc i32 %19 to i8
store i8 %20, i8* %10
store i8 64, i8* %11
%21 = load i8, i8* %11
%22 = zext i8 %21 to i32
%23 = or i32 %22, 0
%24 = trunc i32 %23 to i8
store i8 %24, i8* %11
%25 = load i8, i8* %11
%26 = zext i8 %25 to i32
%27 = or i32 %26, 0
%28 = trunc i32 %27 to i8
store i8 %28, i8* %11
%29 = load i8, i8* %11
%30 = zext i8 %29 to i32
%31 = or i32 %30, 0
%32 = trunc i32 %31 to i8
store i8 %32, i8* %11
%33 = load i32, i32* %8
%34 = and i32 %33, 8
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %44
36: ; preds = %4
%37 = load i8, i8* %11
%38 = zext i8 %37 to i32
%39 = or i32 %38, 1
%40 = trunc i32 %39 to i8
store i8 %40, i8* %11
%41 = load i8*, i8** %5
%42 = load i32*, i32** %6
%43 = call i32 @mem_write(i8* %41, i32* %42, i8* %11, i32 1)
br label %44
44: ; preds = %36, %4
%45 = load i8*, i8** %5
%46 = load i32*, i32** %6
%47 = call i32 @mem_write(i8* %45, i32* %46, i8* %10, i32 1)
ret void
}

LLVM IR (-O2):
define void @write_o_instr(i8* %0, i32* %1, i8 zeroext %2, i32 %3) {
%5 = alloca i8
%6 = alloca i8
%7 = trunc i32 %3 to i8
%8 = and i8 %7, 7
%9 = add i8 %8, %2
store i8 %9, i8* %5
store i8 64, i8* %6
%10 = and i32 %3, 8
%11 = icmp eq i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %4
store i8 65, i8* %6
%13 = call i32 @mem_write(i8* %0, i32* %1, i8* %6, i32 1)
br label %14
14: ; preds = %12, %4
%15 = call i32 @mem_write(i8* %0, i32* %1, i8* %5, i32 1)
ret void
}