#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 ddrt_read(int);
int ssn_ddrt_init(int mode, int code_stream, unsigned int reversed_bit,
                  unsigned int dqindex, unsigned int bytenum) {
  int times = 0;
  unsigned int regval;
  unsigned int ddrt_dq_err_ovfl;
  unsigned int ddrt_dq_err_cnt;
  unsigned int dq_num, dq_tmp;
  dq_num = (bytenum << 3) + dqindex;
  if (dqindex > 3)
    dq_tmp = ((dqindex - 4) << 3);
  else
    dq_tmp = (dqindex << 3);
  (*(volatile unsigned int *)(0x206d0000 + 0x30) = (reversed_bit));
  (*(volatile unsigned int *)(0x206d0000 + 0x0) = (mode | code_stream | 0x1));
  (*(volatile unsigned int *)(0x206d0000 + 0x4) = (0));
  do {
    regval = ddrt_read(0x206d0000 + 0x4);
    times++;
  } while ((!(regval & 0x01)) && (times < (400)));
  if (!(regval & 0x02)) {
    ddrt_dq_err_ovfl = ddrt_read(0x206d0000 + 0x80) & (1 << dq_num);
    if (ddrt_dq_err_ovfl)
      return -1;
    ddrt_dq_err_cnt = ddrt_read(
        0x206d0000 + (0x60 + (((bytenum << 1) + (dqindex >> 2)) << 2)));
    ddrt_dq_err_cnt = ddrt_dq_err_cnt & (0xff << (dq_tmp));
    if (ddrt_dq_err_cnt)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<ssn_ddrt_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
shl    $0x3,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x3,-0x14(%rbp)
jbe    1156 <ssn_ddrt_init+0x46>
mov    -0x14(%rbp),%eax
sub    $0x4,%eax
shl    $0x3,%eax
mov    %eax,-0x30(%rbp)
jmp    115f <ssn_ddrt_init+0x4f>
mov    -0x14(%rbp),%eax
shl    $0x3,%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%ecx
mov    $0x206d0030,%eax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%ecx
or     -0xc(%rbp),%ecx
or     $0x1,%ecx
mov    $0x206d0000,%eax
mov    %ecx,(%rax)
mov    $0x206d0004,%eax
movl   $0x0,(%rax)
mov    $0x206d0004,%edi
call   1030 <ddrt_read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%ecx
and    $0x1,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x31(%rbp)
jne    11bb <ssn_ddrt_init+0xab>
cmpl   $0x190,-0x1c(%rbp)
setl   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1184 <ssn_ddrt_init+0x74>
mov    -0x20(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
jne    1257 <ssn_ddrt_init+0x147>
mov    $0x206d0080,%edi
call   1030 <ddrt_read@plt>
mov    -0x2c(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1206 <ssn_ddrt_init+0xf6>
movl   $0xffffffff,-0x4(%rbp)
jmp    125e <ssn_ddrt_init+0x14e>
mov    -0x18(%rbp),%edi
shl    $0x1,%edi
mov    -0x14(%rbp),%eax
shr    $0x2,%eax
add    %eax,%edi
shl    $0x2,%edi
add    $0x60,%edi
add    $0x206d0000,%edi
call   1030 <ddrt_read@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x30(%rbp),%ecx
mov    $0xff,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1252 <ssn_ddrt_init+0x142>
movl   $0xffffffff,-0x4(%rbp)
jmp    125e <ssn_ddrt_init+0x14e>
jmp    1257 <ssn_ddrt_init+0x147>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ssn_ddrt_init>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%ebp
lea    0x0(%rbp,%r8,8),%r14d
lea    0x0(,%rbp,8),%r12d
lea    -0x20(,%rbp,8),%r15d
ff
mov    %edx,0x206d0030
or     %esi,%edi
or     $0x1,%edi
mov    %edi,0x206d0000
movl   $0x0,0x206d0004
xor    %ebx,%ebx
nop
mov    $0x206d0004,%edi
call   1030 <ddrt_read@plt>
test   $0x1,%al
jne    116b <ssn_ddrt_init+0x5b>
lea    0x1(%rbx),%ecx
cmp    $0x18f,%ebx
mov    %ecx,%ebx
jb     1150 <ssn_ddrt_init+0x40>
cmp    $0x4,%ebp
cmovae %r15d,%r12d
test   $0x2,%al
jne    11a7 <ssn_ddrt_init+0x97>
mov    $0x206d0080,%edi
call   1030 <ddrt_read@plt>
mov    $0xffffffff,%ebp
bt     %r14d,%eax
jb     11a9 <ssn_ddrt_init+0x99>
add    $0x206d0060,%r14d
and    $0xfffffffc,%r14d
mov    %r14d,%edi
call   1030 <ddrt_read@plt>
mov    %r12d,%ecx
shr    %cl,%eax
test   %al,%al
jne    11a9 <ssn_ddrt_init+0x99>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ssn_ddrt_init(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 0, i32* %12
%18 = load i32, i32* %11
%19 = shl i32 %18, 3
%20 = load i32, i32* %10
%21 = add i32 %19, %20
store i32 %21, i32* %16
%22 = load i32, i32* %10
%23 = icmp ugt i32 %22, 3
br i1 %23, label %24, label %28
24: ; preds = %5
%25 = load i32, i32* %10
%26 = sub i32 %25, 4
%27 = shl i32 %26, 3
store i32 %27, i32* %17
br label %31
28: ; preds = %5
%29 = load i32, i32* %10
%30 = shl i32 %29, 3
store i32 %30, i32* %17
br label %31
31: ; preds = %28, %24
%32 = load i32, i32* %9
store volatile i32 %32, i32* inttoptr (i64 544014384 to i32*)
%33 = load i32, i32* %7
%34 = load i32, i32* %8
%35 = or i32 %33, %34
%36 = or i32 %35, 1
store volatile i32 %36, i32* inttoptr (i64 544014336 to i32*)
store volatile i32 0, i32* inttoptr (i64 544014340 to i32*)
br label %37
37: ; preds = %48, %31
%38 = call i32 @ddrt_read(i32 544014340)
store i32 %38, i32* %13
%39 = load i32, i32* %12
%40 = add nsw i32 %39, 1
store i32 %40, i32* %12
br label %41
41: ; preds = %37
%42 = load i32, i32* %13
%43 = and i32 %42, 1
%44 = icmp ne i32 %43, 0
br i1 %44, label %48, label %45
45: ; preds = %41
%46 = load i32, i32* %12
%47 = icmp slt i32 %46, 400
br label %48
48: ; preds = %45, %41
%49 = phi i1 [ false, %41 ], [ %47, %45 ]
br i1 %49, label %37, label %50
50: ; preds = %48
%51 = load i32, i32* %13
%52 = and i32 %51, 2
%53 = icmp ne i32 %52, 0
br i1 %53, label %80, label %54
54: ; preds = %50
%55 = call i32 @ddrt_read(i32 544014464)
%56 = load i32, i32* %16
%57 = shl i32 1, %56
%58 = and i32 %55, %57
store i32 %58, i32* %14
%59 = load i32, i32* %14
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %54
store i32 -1, i32* %6
br label %81
62: ; preds = %54
%63 = load i32, i32* %11
%64 = shl i32 %63, 1
%65 = load i32, i32* %10
%66 = lshr i32 %65, 2
%67 = add i32 %64, %66
%68 = shl i32 %67, 2
%69 = add i32 96, %68
%70 = add i32 544014336, %69
%71 = call i32 @ddrt_read(i32 %70)
store i32 %71, i32* %15
%72 = load i32, i32* %15
%73 = load i32, i32* %17
%74 = shl i32 255, %73
%75 = and i32 %72, %74
store i32 %75, i32* %15
%76 = load i32, i32* %15
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %79
78: ; preds = %62
store i32 -1, i32* %6
br label %81
79: ; preds = %62
br label %80
80: ; preds = %79, %50
store i32 0, i32* %6
br label %81
81: ; preds = %80, %78, %61
%82 = load i32, i32* %6
ret i32 %82
}

LLVM IR (-O2):
define i32 @ssn_ddrt_init(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = shl i32 %4, 3
%7 = add i32 %6, %3
%8 = shl i32 %3, 3
%9 = add i32 %8, -32
store volatile i32 %2, i32* inttoptr (i64 544014384 to i32*)
%10 = or i32 %0, %1
%11 = or i32 %10, 1
store volatile i32 %11, i32* inttoptr (i64 544014336 to i32*)
store volatile i32 0, i32* inttoptr (i64 544014340 to i32*)
br label %12
12: ; preds = %12, %5
%13 = phi i32 [ 0, %5 ], [ %15, %12 ]
%14 = tail call i32 @ddrt_read(i32 544014340)
%15 = add nuw nsw i32 %13, 1
%16 = and i32 %14, 1
%17 = icmp eq i32 %16, 0
%18 = icmp ult i32 %13, 399
%19 = select i1 %17, i1 %18, i1 false
br i1 %19, label %12, label %20
20: ; preds = %12
%21 = icmp ugt i32 %3, 3
%22 = select i1 %21, i32 %9, i32 %8
%23 = and i32 %14, 2
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %37
25: ; preds = %20
%26 = tail call i32 @ddrt_read(i32 544014464)
%27 = shl nuw i32 1, %7
%28 = and i32 %26, %27
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %38
30: ; preds = %25
%31 = add i32 %7, 544014432
%32 = and i32 %31, -4
%33 = tail call i32 @ddrt_read(i32 %32)
%34 = shl i32 255, %22
%35 = and i32 %33, %34
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %30, %20
br label %38
38: ; preds = %30, %25, %37
%39 = phi i32 [ 0, %37 ], [ -1, %25 ], [ -1, %30 ]
ret i32 %39
}