#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 /*<<< 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) {
  uint32_t coff = chan * 0x20;
  uint64_t sdat;
  int rc;
  rc = xscom_read(chip_id, 0x0006B011 + coff, &sdat);
  if (rc) {
    do {
      fprintf(stderr, "xscom OCB_PIB_OCBCSR0_0x0006B011 read error %d\n", rc);
    } while (0);
    return -1;
  }
  sdat = ~((0x8000000000000000UL >> (4)) | (0x8000000000000000UL >> (5)));
  rc = xscom_write(chip_id, 0x0006B012 + coff, sdat);
  if (rc) {
    do {
      fprintf(stderr, "xscom OCB_PIB_OCBCSR0_ANDx0006B012 write error %d\n",
              rc);
    } while (0);
    return -1;
  }
  sdat = ((uint64_t)addr) << 32;
  rc = xscom_write(chip_id, 0x0006B010 + coff, sdat);
  if (rc) {
    do {
      fprintf(stderr, "xscom OCB_PIB_OCBAR0_0x0006B010 write error %d\n", rc);
    } while (0);
    return -1;
  }
  rc = xscom_read(chip_id, 0x0006B015 + coff, val);
  if (rc) {
    do {
      fprintf(stderr, "xscom OCB_PIB_OCBAR0_0x0006B010 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    -0xc(%rbp),%eax
shl    $0x5,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%esi
add    $0x6b011,%esi
lea    -0x20(%rbp),%rdx
call   1050 <xscom_read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1199 <sram_read+0x69>
jmp    1175 <sram_read+0x45>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xe78(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <sram_read+0x163>
movl   $0xffffffff,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%esi
add    $0x6b012,%esi
mov    -0x20(%rbp),%edx
call   1030 <xscom_write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11ea <sram_read+0xba>
jmp    11c6 <sram_read+0x96>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xe57(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <sram_read+0x163>
mov    -0x10(%rbp),%eax
mov    $0x20,%ecx
shl    %cl,%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%esi
add    $0x6b010,%esi
mov    -0x20(%rbp),%edx
call   1030 <xscom_write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1241 <sram_read+0x111>
jmp    121d <sram_read+0xed>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xe33(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <sram_read+0x163>
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%esi
add    $0x6b015,%esi
mov    -0x18(%rbp),%rdx
call   1050 <xscom_read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     128c <sram_read+0x15c>
jmp    1268 <sram_read+0x138>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xe18(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <sram_read+0x163>
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   %rbx
push   %rax
mov    %rcx,%r14
mov    %esi,%ebx
mov    %edi,%ebp
shl    $0x5,%ebx
lea    0x6b011(%rbx),%esi
lea    0x4(%rsp),%rdx
call   1050 <xscom_read@plt>
test   %eax,%eax
je     1177 <sram_read+0x47>
mov    0x2e84(%rip),%rcx
mov    (%rcx),%edi
lea    0xe9b(%rip),%rsi
mov    %eax,%edx
call   1040 <fprintf@plt>
mov    $0xffffffff,%r15d
jmp    11fd <sram_read+0xcd>
movl   $0xffffffff,0x4(%rsp)
ff
lea    0x6b012(%rbx),%esi
mov    $0xffffffff,%r15d
mov    %ebp,%edi
mov    $0xffffffff,%edx
call   1030 <xscom_write@plt>
test   %eax,%eax
je     11ad <sram_read+0x7d>
mov    0x2e3e(%rip),%rcx
mov    (%rcx),%edi
lea    0xe85(%rip),%rsi
jmp    11f6 <sram_read+0xc6>
lea    0x6b010(%rbx),%esi
mov    %ebp,%edi
call   1030 <xscom_write@plt>
test   %eax,%eax
je     11d0 <sram_read+0xa0>
mov    0x2e1b(%rip),%rcx
mov    (%rcx),%edi
lea    0xe95(%rip),%rsi
jmp    11f6 <sram_read+0xc6>
add    $0x6b015,%ebx
mov    %ebp,%edi
mov    %ebx,%esi
mov    %r14,%rdx
call   1050 <xscom_read@plt>
test   %eax,%eax
je     120b <sram_read+0xdb>
mov    0x2df3(%rip),%rcx
mov    (%rcx),%edi
lea    0xe9d(%rip),%rsi
mov    %eax,%edx
call   1040 <fprintf@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r15d,%r15d
jmp    11fd <sram_read+0xcd>

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

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