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

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int, ...);
int read_bus(int, unsigned char *, int);
int write_bus(int, unsigned char *, int);
int read_register(int busfd, __uint16_t reg, unsigned char *buf, int bufsize) {
  unsigned char reg_buf[2];
  int ret;
  reg_buf[0] = (reg >> 0) & 0xFF;
  reg_buf[1] = (reg >> 8) & 0xFF;
  ret = write_bus(busfd, reg_buf, 2);
  if (ret < 0) {
    printf("Failed to write [0x%02x 0x%02x] (reg: %04x).\n", reg_buf[0],
           reg_buf[1], reg);
    return ret;
  }
  printf("wrote %d bytes\n", ret);
  return read_bus(busfd, buf, bufsize);
}


x86 assembly (clang, -O0):
<read_register>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
sar    $0x0,%eax
and    $0xff,%eax
mov    %al,-0x1e(%rbp)
mov    -0xc(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x1d(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1e(%rbp),%rsi
mov    $0x2,%edx
call   1040 <write_bus@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11a3 <read_register+0x73>
movzbl -0x1e(%rbp),%esi
movzbl -0x1d(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c6 <read_register+0x96>
mov    -0x24(%rbp),%esi
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <read_bus@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_register>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %esi,%r12d
mov    %edi,%ebx
mov    %r12w,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    $0x2,%edx
call   1040 <write_bus@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1183 <read_register+0x53>
lea    0xec5(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1050 <read_bus@plt>
mov    %eax,%ebp
jmp    119e <read_register+0x6e>
movzbl 0xe(%rsp),%esi
movzbl 0xf(%rsp),%edx
lea    0xe6c(%rip),%rdi
mov    %r12d,%ecx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [46 x i8] c"Failed to write [0x%02x 0x%02x] (reg: %04x).\0A\00"
@.str.1 = [16 x i8] c"wrote %d bytes\0A\00"
define i32 @read_register(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca [2 x i8]
%11 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = load i32, i32* %7
%13 = ashr i32 %12, 0
%14 = and i32 %13, 255
%15 = trunc i32 %14 to i8
%16 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
store i8 %15, i8* %16
%17 = load i32, i32* %7
%18 = ashr i32 %17, 8
%19 = and i32 %18, 255
%20 = trunc i32 %19 to i8
%21 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 1
store i8 %20, i8* %21
%22 = load i32, i32* %6
%23 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%24 = call i32 @write_bus(i32 %22, i8* %23, i32 2)
store i32 %24, i32* %11
%25 = load i32, i32* %11
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %4
%28 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 1
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = load i32, i32* %7
%35 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %30, i32 %33, i32 %34)
%36 = load i32, i32* %11
store i32 %36, i32* %5
br label %44
37: ; preds = %4
%38 = load i32, i32* %11
%39 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %6
%41 = load i8*, i8** %8
%42 = load i32, i32* %9
%43 = call i32 @read_bus(i32 %40, i8* %41, i32 %42)
store i32 %43, i32* %5
br label %44
44: ; preds = %37, %27
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
@.str = [46 x i8] c"Failed to write [0x%02x 0x%02x] (reg: %04x).\0A\00"
@.str.1 = [16 x i8] c"wrote %d bytes\0A\00"
define i32 @read_register(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca [2 x i8]
%6 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%7 = trunc i32 %1 to i8
store i8 %7, i8* %6
%8 = lshr i32 %1, 8
%9 = trunc i32 %8 to i8
%10 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
store i8 %9, i8* %10
%11 = call i32 @write_bus(i32 %0, i8* %6, i32 2)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %4
%14 = load i8, i8* %6
%15 = zext i8 %14 to i32
%16 = load i8, i8* %10
%17 = zext i8 %16 to i32
%18 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %17, i32 %1)
br label %22
19: ; preds = %4
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%21 = call i32 @read_bus(i32 %0, i8* %2, i32 %3)
br label %22
22: ; preds = %19, %13
%23 = phi i32 [ %11, %13 ], [ %21, %19 ]
ret i32 %23
}