#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 errno;
int /*<<< orphan*/ printf(char *, unsigned char, int);
int read(int, unsigned char *, int);
int write(int, unsigned char *, int);
int local_cam_get_register(int filehandle, int bus_width, int reg_addr,
                           int *val) {
  unsigned char regval = reg_addr;
  unsigned char buf[10];
  int try_count;
  buf[0] = regval;
  if (write(filehandle, buf, 1) != 1) {
    printf("local_cam_get_register: error sending register address 0x%02x, "
           "errno = %d\r\n",
           regval, errno);
    return -1;
  }
  if (read(filehandle, buf, bus_width) == bus_width) {
    if (bus_width == 1) {
      *val = buf[0];
    } else {
      *val = buf[0] << 8;
      *val |= buf[1];
    }
    return 0;
  }
  printf("local_cam_get_register: error reading value @ 0x%02x, errno = %d\r\n",
         regval, errno);
  return -1;
}


x86 assembly (clang, -O0):
<local_cam_get_register>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x10(%rbp),%eax
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
mov    %al,-0x23(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x23(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     1192 <local_cam_get_register+0x62>
mov    -0x19(%rbp),%al
mov    0x2e6b(%rip),%rcx
mov    (%rcx),%edx
lea    0xe82(%rip),%rdi
movzbl %al,%esi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <local_cam_get_register+0xda>
mov    -0x8(%rbp),%edi
lea    -0x23(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1050 <read@plt>
cmp    -0xc(%rbp),%eax
jne    11e8 <local_cam_get_register+0xb8>
cmpl   $0x1,-0xc(%rbp)
jne    11c3 <local_cam_get_register+0x93>
movzbl -0x23(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    11dc <local_cam_get_register+0xac>
movzbl -0x23(%rbp),%ecx
shl    $0x8,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movzbl -0x22(%rbp),%ecx
mov    -0x18(%rbp),%rax
or     (%rax),%ecx
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    120a <local_cam_get_register+0xda>
mov    -0x19(%rbp),%al
mov    0x2dee(%rip),%rcx
mov    (%rcx),%edx
lea    0xe51(%rip),%rdi
movzbl %al,%esi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<local_cam_get_register>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %edx,%ebp
mov    %esi,%r15d
mov    %edi,%ebx
mov    %bpl,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    1190 <local_cam_get_register+0x60>
lea    0xe(%rsp),%rsi
mov    %ebx,%edi
mov    %r15d,%edx
call   1050 <read@plt>
cmp    %r15d,%eax
jne    11a6 <local_cam_get_register+0x76>
movzbl 0xe(%rsp),%eax
mov    %eax,%ecx
shl    $0x8,%ecx
movzbl 0xf(%rsp),%edx
or     %ecx,%edx
cmp    $0x1,%r15d
cmove  %eax,%edx
mov    %edx,(%r14)
xor    %eax,%eax
jmp    11c4 <local_cam_get_register+0x94>
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
movzbl %bpl,%esi
lea    0xe5c(%rip),%rdi
jmp    11ba <local_cam_get_register+0x8a>
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
movzbl %bpl,%esi
lea    0xe92(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [76 x i8] c"local_cam_get_register: error sending register address 0x%02x, errno = %d\0D\0A\00"
@errno = global i32 0
@.str.1 = [67 x i8] c"local_cam_get_register: error reading value @ 0x%02x, errno = %d\0D\0A\00"
define i32 @local_cam_get_register(i32 %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i8
%11 = alloca [10 x i8]
%12 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32* %3, i32** %9
%13 = load i32, i32* %8
%14 = trunc i32 %13 to i8
store i8 %14, i8* %10
%15 = load i8, i8* %10
%16 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
store i8 %15, i8* %16
%17 = load i32, i32* %6
%18 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%19 = call i32 @write(i32 %17, i8* %18, i32 1)
%20 = icmp ne i32 %19, 1
br i1 %20, label %21, label %25
21: ; preds = %4
%22 = load i8, i8* %10
%23 = load i32, i32* @errno
%24 = call i32 @printf(i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str, i64 0, i64 0), i8 zeroext %22, i32 %23)
store i32 -1, i32* %5
br label %57
25: ; preds = %4
%26 = load i32, i32* %6
%27 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%28 = load i32, i32* %7
%29 = call i32 @read(i32 %26, i8* %27, i32 %28)
%30 = load i32, i32* %7
%31 = icmp eq i32 %29, %30
br i1 %31, label %32, label %53
32: ; preds = %25
%33 = load i32, i32* %7
%34 = icmp eq i32 %33, 1
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = load i32*, i32** %9
store i32 %38, i32* %39
br label %52
40: ; preds = %32
%41 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
%44 = shl i32 %43, 8
%45 = load i32*, i32** %9
store i32 %44, i32* %45
%46 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 1
%47 = load i8, i8* %46
%48 = zext i8 %47 to i32
%49 = load i32*, i32** %9
%50 = load i32, i32* %49
%51 = or i32 %50, %48
store i32 %51, i32* %49
br label %52
52: ; preds = %40, %35
store i32 0, i32* %5
br label %57
53: ; preds = %25
%54 = load i8, i8* %10
%55 = load i32, i32* @errno
%56 = call i32 @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %54, i32 %55)
store i32 -1, i32* %5
br label %57
57: ; preds = %53, %52, %21
%58 = load i32, i32* %5
ret i32 %58
}

LLVM IR (-O2):
@.str = [76 x i8] c"local_cam_get_register: error sending register address 0x%02x, errno = %d\0D\0A\00"
@errno = global i32 0
@.str.1 = [67 x i8] c"local_cam_get_register: error reading value @ 0x%02x, errno = %d\0D\0A\00"
define i32 @local_cam_get_register(i32 %0, i32 %1, i32 %2, i32* nocapture writeonly %3) {
%5 = alloca [10 x i8]
%6 = trunc i32 %2 to i8
%7 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
store i8 %6, i8* %7
%8 = call i32 @write(i32 %0, i8* %7, i32 1)
%9 = icmp eq i32 %8, 1
br i1 %9, label %13, label %10
10: ; preds = %4
%11 = load i32, i32* @errno
%12 = tail call i32 @printf(i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str, i64 0, i64 0), i8 zeroext %6, i32 %11)
br label %29
13: ; preds = %4
%14 = call i32 @read(i32 %0, i8* %7, i32 %1)
%15 = icmp eq i32 %14, %1
br i1 %15, label %16, label %26
16: ; preds = %13
%17 = icmp eq i32 %1, 1
%18 = load i8, i8* %7
%19 = zext i8 %18 to i32
%20 = shl nuw nsw i32 %19, 8
%21 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 1
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = or i32 %20, %23
%25 = select i1 %17, i32 %19, i32 %24
store i32 %25, i32* %3
br label %29
26: ; preds = %13
%27 = load i32, i32* @errno
%28 = tail call i32 @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %6, i32 %27)
br label %29
29: ; preds = %26, %16, %10
%30 = phi i32 [ -1, %10 ], [ 0, %16 ], [ -1, %26 ]
ret i32 %30
}