#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_write(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_write(chip_id, 0x0006B015 + coff, val);
  if (rc) {
    do {
      fprintf(stderr, "xscom OCB_PIB_OCBAR0_0x0006B010 write error %d\n", rc);
    } while (0);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sram_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x5,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%esi
add    $0x6b011,%esi
lea    -0x1c(%rbp),%rdx
call   1050 <xscom_read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1198 <sram_write+0x68>
jmp    1174 <sram_write+0x44>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xe79(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <sram_write+0x161>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%esi
add    $0x6b012,%esi
mov    -0x1c(%rbp),%edx
call   1030 <xscom_write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11e9 <sram_write+0xb9>
jmp    11c5 <sram_write+0x95>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xe58(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <sram_write+0x161>
mov    -0x10(%rbp),%eax
mov    $0x20,%ecx
shl    %cl,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%esi
add    $0x6b010,%esi
mov    -0x1c(%rbp),%edx
call   1030 <xscom_write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1240 <sram_write+0x110>
jmp    121c <sram_write+0xec>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xe34(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <sram_write+0x161>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%esi
add    $0x6b015,%esi
mov    -0x14(%rbp),%edx
call   1030 <xscom_write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     128a <sram_write+0x15a>
jmp    1266 <sram_write+0x136>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xdea(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <sram_write+0x161>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sram_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %esi,%ebp
mov    %edi,%ebx
shl    $0x5,%ebp
lea    0x6b011(%rbp),%esi
lea    0x4(%rsp),%rdx
call   1050 <xscom_read@plt>
test   %eax,%eax
je     1174 <sram_write+0x44>
mov    0x2e84(%rip),%rcx
mov    (%rcx),%edi
lea    0xe9b(%rip),%rsi
mov    %eax,%edx
call   1040 <fprintf@plt>
mov    $0xffffffff,%r15d
jmp    11e8 <sram_write+0xb8>
movl   $0xffffffff,0x4(%rsp)
ff
lea    0x6b012(%rbp),%esi
mov    $0xffffffff,%r15d
mov    %ebx,%edi
mov    $0xffffffff,%edx
call   1030 <xscom_write@plt>
test   %eax,%eax
je     11aa <sram_write+0x7a>
mov    0x2e41(%rip),%rcx
mov    (%rcx),%edi
lea    0xe88(%rip),%rsi
jmp    11e1 <sram_write+0xb1>
lea    0x6b010(%rbp),%esi
mov    %ebx,%edi
call   1030 <xscom_write@plt>
test   %eax,%eax
jne    11d1 <sram_write+0xa1>
add    $0x6b015,%ebp
mov    %ebx,%edi
mov    %ebp,%esi
mov    %r14d,%edx
call   1030 <xscom_write@plt>
test   %eax,%eax
je     11f6 <sram_write+0xc6>
mov    0x2e08(%rip),%rcx
mov    (%rcx),%edi
lea    0xe82(%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    11e8 <sram_write+0xb8>

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"
define i32 @sram_write(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_write(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 ([48 x i8], [48 x i8]* @.str.2, 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"
define i32 @sram_write(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_write(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 ([48 x i8], [48 x i8]* @.str.2, 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
}