#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 uint64_t;
typedef int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int get_xscom_base();
int /*<<< orphan*/ stderr;
int xscom_read(int, int, int *);
int xscom_write(int, int, int);
int sram_read(uint32_t chip_id, int chan, uint32_t addr, uint64_t *val) {
  uint64_t sdat, base = get_xscom_base();
  uint32_t coff = chan * 0x20;
  int rc;
  rc = xscom_read(chip_id, base + 0x11 + coff, &sdat);
  if (rc) {
    do {
      fprintf(stderr, "xscom OCBCSR0 read error %d\n", rc);
    } while (0);
    return -1;
  }
  sdat = ~((0x8000000000000000UL >> (4)) | (0x8000000000000000UL >> (5)));
  rc = xscom_write(chip_id, base + 0x12 + coff, sdat);
  if (rc) {
    do {
      fprintf(stderr, "xscom OCBCSR0_AND write error %d\n", rc);
    } while (0);
    return -1;
  }
  sdat = ((uint64_t)addr) << 32;
  rc = xscom_write(chip_id, base + 0x10 + coff, sdat);
  if (rc) {
    do {
      fprintf(stderr, "xscom OCBAR0 write error %d\n", rc);
    } while (0);
    return -1;
  }
  rc = xscom_read(chip_id, base + 0x15 + coff, val);
  if (rc) {
    do {
      fprintf(stderr, "xscom OCBDR0 read error %d\n", rc);
    } while (0);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sram_read>:
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    $0x0,%al
call   1050 <get_xscom_base@plt>
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x5,%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%esi
add    $0x11,%esi
add    -0x24(%rbp),%esi
lea    -0x1c(%rbp),%rdx
call   1060 <xscom_read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     11b3 <sram_read+0x73>
jmp    118f <sram_read+0x4f>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
lea    0xe5e(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <sram_read+0x16d>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%esi
add    $0x12,%esi
add    -0x24(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1030 <xscom_write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1204 <sram_read+0xc4>
jmp    11e0 <sram_read+0xa0>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
lea    0xe2a(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <sram_read+0x16d>
mov    -0x10(%rbp),%eax
mov    $0x20,%ecx
shl    %cl,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%esi
add    $0x10,%esi
add    -0x24(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1030 <xscom_write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     125b <sram_read+0x11b>
jmp    1237 <sram_read+0xf7>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
lea    0xdf5(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <sram_read+0x16d>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%esi
add    $0x15,%esi
add    -0x24(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1060 <xscom_read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     12a6 <sram_read+0x166>
jmp    1282 <sram_read+0x142>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
lea    0xdc7(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <sram_read+0x16d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sram_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %esi,%ebp
mov    %edi,%r12d
xor    %eax,%eax
call   1050 <get_xscom_base@plt>
mov    %eax,%ebx
shl    $0x5,%ebp
lea    (%rbx,%rbp,1),%esi
add    $0x11,%esi
lea    0xc(%rsp),%rdx
mov    %r12d,%edi
call   1060 <xscom_read@plt>
test   %eax,%eax
je     1199 <sram_read+0x59>
mov    0x2e62(%rip),%rcx
mov    (%rcx),%edi
lea    0xe79(%rip),%rsi
mov    %eax,%edx
call   1040 <fprintf@plt>
mov    $0xffffffff,%r15d
jmp    121b <sram_read+0xdb>
add    %ebp,%ebx
movl   $0xffffffff,0xc(%rsp)
ff
lea    0x12(%rbx),%esi
mov    $0xffffffff,%r15d
mov    %r12d,%edi
mov    $0xffffffff,%edx
call   1030 <xscom_write@plt>
test   %eax,%eax
je     11cf <sram_read+0x8f>
mov    0x2e1c(%rip),%rcx
mov    (%rcx),%edi
lea    0xe50(%rip),%rsi
jmp    1214 <sram_read+0xd4>
lea    0x10(%rbx),%esi
mov    %r12d,%edi
call   1030 <xscom_write@plt>
test   %eax,%eax
je     11f0 <sram_read+0xb0>
mov    0x2dfb(%rip),%rcx
mov    (%rcx),%edi
lea    0xe51(%rip),%rsi
jmp    1214 <sram_read+0xd4>
add    $0x15,%ebx
mov    %r12d,%edi
mov    %ebx,%esi
mov    %r14,%rdx
call   1060 <xscom_read@plt>
test   %eax,%eax
je     122b <sram_read+0xeb>
mov    0x2dd5(%rip),%rcx
mov    (%rcx),%edi
lea    0xe48(%rip),%rsi
mov    %eax,%edx
call   1040 <fprintf@plt>
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r15d,%r15d
jmp    121b <sram_read+0xdb>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"xscom OCBCSR0 read error %d\0A\00"
@.str.1 = [34 x i8] c"xscom OCBCSR0_AND write error %d\0A\00"
@.str.2 = [29 x i8] c"xscom OCBAR0 write error %d\0A\00"
@.str.3 = [28 x i8] c"xscom OCBDR0 read error %d\0A\00"
define i32 @sram_read(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 i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32* %3, i32** %9
%14 = call i32 (...) @get_xscom_base()
store i32 %14, i32* %11
%15 = load i32, i32* %7
%16 = mul nsw i32 %15, 32
store i32 %16, i32* %12
%17 = load i32, i32* %6
%18 = load i32, i32* %11
%19 = add nsw i32 %18, 17
%20 = load i32, i32* %12
%21 = add nsw i32 %19, %20
%22 = call i32 @xscom_read(i32 %17, i32 %21, i32* %10)
store i32 %22, i32* %13
%23 = load i32, i32* %13
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %4
br label %26
26: ; preds = %25
%27 = load i32, i32* @stderr
%28 = load i32, i32* %13
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %26
store i32 -1, i32* %5
br label %82
31: ; preds = %4
store i32 -1, i32* %10
%32 = load i32, i32* %6
%33 = load i32, i32* %11
%34 = add nsw i32 %33, 18
%35 = load i32, i32* %12
%36 = add nsw i32 %34, %35
%37 = load i32, i32* %10
%38 = call i32 @xscom_write(i32 %32, i32 %36, i32 %37)
store i32 %38, i32* %13
%39 = load i32, i32* %13
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %31
br label %42
42: ; preds = %41
%43 = load i32, i32* @stderr
%44 = load i32, i32* %13
%45 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %42
store i32 -1, i32* %5
br label %82
47: ; preds = %31
%48 = load i32, i32* %8
%49 = shl i32 %48, 32
store i32 %49, i32* %10
%50 = load i32, i32* %6
%51 = load i32, i32* %11
%52 = add nsw i32 %51, 16
%53 = load i32, i32* %12
%54 = add nsw i32 %52, %53
%55 = load i32, i32* %10
%56 = call i32 @xscom_write(i32 %50, i32 %54, i32 %55)
store i32 %56, i32* %13
%57 = load i32, i32* %13
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %65
59: ; preds = %47
br label %60
60: ; preds = %59
%61 = load i32, i32* @stderr
%62 = load i32, i32* %13
%63 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %62)
br label %64
64: ; preds = %60
store i32 -1, i32* %5
br label %82
65: ; preds = %47
%66 = load i32, i32* %6
%67 = load i32, i32* %11
%68 = add nsw i32 %67, 21
%69 = load i32, i32* %12
%70 = add nsw i32 %68, %69
%71 = load i32*, i32** %9
%72 = call i32 @xscom_read(i32 %66, i32 %70, i32* %71)
store i32 %72, i32* %13
%73 = load i32, i32* %13
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %81
75: ; preds = %65
br label %76
76: ; preds = %75
%77 = load i32, i32* @stderr
%78 = load i32, i32* %13
%79 = call i32 @fprintf(i32 %77, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %78)
br label %80
80: ; preds = %76
store i32 -1, i32* %5
br label %82
81: ; preds = %65
store i32 0, i32* %5
br label %82
82: ; preds = %81, %80, %64, %46, %30
%83 = load i32, i32* %5
ret i32 %83
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"xscom OCBCSR0 read error %d\0A\00"
@.str.1 = [34 x i8] c"xscom OCBCSR0_AND write error %d\0A\00"
@.str.2 = [29 x i8] c"xscom OCBAR0 write error %d\0A\00"
@.str.3 = [28 x i8] c"xscom OCBDR0 read error %d\0A\00"
define i32 @sram_read(i32 %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = tail call i32 (...) @get_xscom_base()
%8 = shl nsw i32 %1, 5
%9 = add i32 %7, %8
%10 = add i32 %9, 17
%11 = call i32 @xscom_read(i32 %0, i32 %10, i32* %5)
%12 = icmp eq i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %4
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %37
16: ; preds = %4
store i32 -1, i32* %5
%17 = add i32 %9, 18
%18 = call i32 @xscom_write(i32 %0, i32 %17, i32 -1)
%19 = icmp eq i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %37
23: ; preds = %16
%24 = add i32 %9, 16
%25 = call i32 @xscom_write(i32 %0, i32 %24, i32 poison)
%26 = icmp eq i32 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %37
30: ; preds = %23
%31 = add i32 %9, 21
%32 = call i32 @xscom_read(i32 %0, i32 %31, i32* %3)
%33 = icmp eq i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %32)
br label %37
37: ; preds = %30, %34, %27, %20, %13
%38 = phi i32 [ -1, %13 ], [ -1, %20 ], [ -1, %27 ], [ -1, %34 ], [ 0, %30 ]
ret i32 %38
}