#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 long uint8_t;
typedef int uint32_t;
typedef int int32_t;

/* Variables and functions */
int32_t I2C_Xfer(uint8_t slaveaddr, uint8_t *theBuffer, uint32_t theLength,
                 uint8_t trailingStop) {
  int32_t retval = 0;
  int done = 0;
  uint8_t stat;
  (*(volatile unsigned long *)(0xE001C000 + 0x00)) = (1 << 5);
  while (!done) {
    while (!((*(volatile unsigned long *)(0xE001C000 + 0x00)) & (1 << 3)))
      ;
    stat = (*(volatile unsigned long *)(0xE001C000 + 0x04));
    switch (stat) {
    case (0x08):
    case (0x10):
      (*(volatile unsigned long *)(0xE001C000 + 0x08)) = slaveaddr;
      (*(volatile unsigned long *)(0xE001C000 + 0x18)) = (1 << 5);
      break;
    case (0x20):
    case (0x30):
    case (0x38):
    case (0x48):
      trailingStop = 1;
      retval = -1;
      done = 1;
      break;
    case (0x18):
    case (0x28):
      if (theLength) {
        (*(volatile unsigned long *)(0xE001C000 + 0x08)) = *theBuffer++;
        theLength--;
      } else {
        done = 1;
      }
      break;
    case (0x40):
      (*(volatile unsigned long *)(0xE001C000 + 0x00)) = (1 << 2);
      break;
    case (0x50):
    case (0x58):
      *theBuffer++ = (*(volatile unsigned long *)(0xE001C000 + 0x08));
      theLength--;
      if (theLength == 1) {
        (*(volatile unsigned long *)(0xE001C000 + 0x18)) = (1 << 2);
      } else if (theLength == 0) {
        done = 1;
      }
      break;
    }
    (*(volatile unsigned long *)(0xE001C000 + 0x18)) = (1 << 3);
  }
  if (trailingStop) {
    (*(volatile unsigned long *)(0xE001C000 + 0x00)) = (1 << 4);
    while ((*(volatile unsigned long *)(0xE001C000 + 0x00)) & (1 << 4))
      ;
  }
  return retval;
}


x86 assembly (clang, -O0):
<I2C_Xfer>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    $0xe001c000,%eax
movq   $0x20,(%rax)
cmpl   $0x0,-0x28(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1143 <I2C_Xfer+0x43>
jmp    1296 <I2C_Xfer+0x196>
jmp    1148 <I2C_Xfer+0x48>
mov    $0xe001c000,%eax
mov    (%rax),%rax
and    $0x8,%rax
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    116a <I2C_Xfer+0x6a>
jmp    116f <I2C_Xfer+0x6f>
jmp    1148 <I2C_Xfer+0x48>
mov    $0xe001c004,%eax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0xfffffffffffffff8,%rax
mov    %rax,-0x38(%rbp)
sub    $0x50,%rax
ja     1285 <I2C_Xfer+0x185>
mov    -0x38(%rbp),%rax
lea    0xe64(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x8(%rbp),%rcx
mov    $0xe001c008,%eax
mov    %rcx,(%rax)
mov    $0xe001c018,%eax
movq   $0x20,(%rax)
jmp    1285 <I2C_Xfer+0x185>
movq   $0x1,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    1285 <I2C_Xfer+0x185>
cmpl   $0x0,-0x14(%rbp)
je     120f <I2C_Xfer+0x10f>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%rcx
mov    $0xe001c008,%eax
mov    %rcx,(%rax)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1216 <I2C_Xfer+0x116>
movl   $0x1,-0x28(%rbp)
jmp    1285 <I2C_Xfer+0x185>
mov    $0xe001c000,%eax
movq   $0x4,(%rax)
jmp    1285 <I2C_Xfer+0x185>
mov    $0xe001c008,%eax
mov    (%rax),%rcx
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x8,%rdx
mov    %rdx,-0x10(%rbp)
mov    %rcx,(%rax)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    126a <I2C_Xfer+0x16a>
mov    $0xe001c018,%eax
movq   $0x4,(%rax)
jmp    1280 <I2C_Xfer+0x180>
cmpl   $0x0,-0x14(%rbp)
jne    127b <I2C_Xfer+0x17b>
movl   $0x1,-0x28(%rbp)
jmp    1280 <I2C_Xfer+0x180>
jmp    1285 <I2C_Xfer+0x185>
mov    $0xe001c018,%eax
movq   $0x8,(%rax)
jmp    112d <I2C_Xfer+0x2d>
cmpq   $0x0,-0x20(%rbp)
je     12cd <I2C_Xfer+0x1cd>
mov    $0xe001c000,%eax
movq   $0x10,(%rax)
mov    $0xe001c000,%eax
mov    (%rax),%rax
and    $0x10,%rax
cmp    $0x0,%rax
je     12c8 <I2C_Xfer+0x1c8>
jmp    12ad <I2C_Xfer+0x1ad>
jmp    12cd <I2C_Xfer+0x1cd>
mov    -0x24(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<I2C_Xfer>:
mov    $0xe001c000,%r8d
movq   $0x20,(%r8)
lea    0xeec(%rip),%r9
jmp    1130 <I2C_Xfer+0x30>
mov    %rdi,0x8(%r8)
movq   $0x20,0x18(%r8)
movq   $0x8,0x18(%r8)
nopw   0x0(%rax,%rax,1)
testq  $0x8,(%r8)
je     1130 <I2C_Xfer+0x30>
mov    0x4(%r8),%rax
add    $0xfffffffffffffff8,%rax
rol    $0x3d,%rax
cmp    $0xa,%rax
ja     11b0 <I2C_Xfer+0xb0>
movslq (%r9,%rax,4),%rax
add    %r9,%rax
jmp    *%rax
test   %edx,%edx
je     11cc <I2C_Xfer+0xcc>
mov    (%rsi),%rax
add    $0x8,%rsi
mov    %rax,0x8(%r8)
add    $0xffffffff,%edx
movq   $0x8,0x18(%r8)
jmp    1130 <I2C_Xfer+0x30>
mov    0x8(%r8),%rax
mov    %rax,(%rsi)
add    $0x8,%rsi
cmp    $0x2,%edx
je     11a3 <I2C_Xfer+0xa3>
cmp    $0x1,%edx
je     11cc <I2C_Xfer+0xcc>
add    $0xffffffff,%edx
movq   $0x8,0x18(%r8)
jmp    1130 <I2C_Xfer+0x30>
movq   $0x4,(%r8)
movq   $0x8,0x18(%r8)
jmp    1130 <I2C_Xfer+0x30>
movq   $0x4,0x18(%r8)
mov    $0x1,%edx
movq   $0x8,0x18(%r8)
jmp    1130 <I2C_Xfer+0x30>
movq   $0x8,0x18(%r8)
mov    $0xffffffff,%eax
jmp    11db <I2C_Xfer+0xdb>
movq   $0x8,0x18(%r8)
xor    %eax,%eax
test   %rcx,%rcx
je     11f9 <I2C_Xfer+0xf9>
movq   $0x10,(%r8)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
testq  $0x10,(%r8)
jne    11f0 <I2C_Xfer+0xf0>
ret

LLVM IR (-O0):
define i32 @I2C_Xfer(i64 %0, i64* %1, i32 %2, i64 %3) {
%5 = alloca i64
%6 = alloca i64*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
store i64 %0, i64* %5
store i64* %1, i64** %6
store i32 %2, i32* %7
store i64 %3, i64* %8
store i32 0, i32* %9
store i32 0, i32* %10
store volatile i64 32, i64* inttoptr (i64 3758211072 to i64*)
br label %12
12: ; preds = %56, %4
%13 = load i32, i32* %10
%14 = icmp ne i32 %13, 0
%15 = xor i1 %14, true
br i1 %15, label %16, label %57
16: ; preds = %12
br label %17
17: ; preds = %22, %16
%18 = load volatile i64, i64* inttoptr (i64 3758211072 to i64*)
%19 = and i64 %18, 8
%20 = icmp ne i64 %19, 0
%21 = xor i1 %20, true
br i1 %21, label %22, label %23
22: ; preds = %17
br label %17
23: ; preds = %17
%24 = load volatile i64, i64* inttoptr (i64 3758211076 to i64*)
store i64 %24, i64* %11
%25 = load i64, i64* %11
switch i64 %25, label %56 [
i64 8, label %26
i64 16, label %26
i64 32, label %28
i64 48, label %28
i64 56, label %28
i64 72, label %28
i64 24, label %29
i64 40, label %29
i64 64, label %40
i64 80, label %41
i64 88, label %41
]
26: ; preds = %23, %23
%27 = load i64, i64* %5
store volatile i64 %27, i64* inttoptr (i64 3758211080 to i64*)
store volatile i64 32, i64* inttoptr (i64 3758211096 to i64*)
br label %56
28: ; preds = %23, %23, %23, %23
store i64 1, i64* %8
store i32 -1, i32* %9
store i32 1, i32* %10
br label %56
29: ; preds = %23, %23
%30 = load i32, i32* %7
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %29
%33 = load i64*, i64** %6
%34 = getelementptr inbounds i64, i64* %33, i32 1
store i64* %34, i64** %6
%35 = load i64, i64* %33
store volatile i64 %35, i64* inttoptr (i64 3758211080 to i64*)
%36 = load i32, i32* %7
%37 = add nsw i32 %36, -1
store i32 %37, i32* %7
br label %39
38: ; preds = %29
store i32 1, i32* %10
br label %39
39: ; preds = %38, %32
br label %56
40: ; preds = %23
store volatile i64 4, i64* inttoptr (i64 3758211072 to i64*)
br label %56
41: ; preds = %23, %23
%42 = load volatile i64, i64* inttoptr (i64 3758211080 to i64*)
%43 = load i64*, i64** %6
%44 = getelementptr inbounds i64, i64* %43, i32 1
store i64* %44, i64** %6
store i64 %42, i64* %43
%45 = load i32, i32* %7
%46 = add nsw i32 %45, -1
store i32 %46, i32* %7
%47 = load i32, i32* %7
%48 = icmp eq i32 %47, 1
br i1 %48, label %49, label %50
49: ; preds = %41
store volatile i64 4, i64* inttoptr (i64 3758211096 to i64*)
br label %55
50: ; preds = %41
%51 = load i32, i32* %7
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %50
store i32 1, i32* %10
br label %54
54: ; preds = %53, %50
br label %55
55: ; preds = %54, %49
br label %56
56: ; preds = %23, %55, %40, %39, %28, %26
store volatile i64 8, i64* inttoptr (i64 3758211096 to i64*)
br label %12
57: ; preds = %12
%58 = load i64, i64* %8
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %67
60: ; preds = %57
store volatile i64 16, i64* inttoptr (i64 3758211072 to i64*)
br label %61
61: ; preds = %65, %60
%62 = load volatile i64, i64* inttoptr (i64 3758211072 to i64*)
%63 = and i64 %62, 16
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %61
br label %61
66: ; preds = %61
br label %67
67: ; preds = %66, %57
%68 = load i32, i32* %9
ret i32 %68
}

LLVM IR (-O2):
define i32 @I2C_Xfer(i64 %0, i64* nocapture %1, i32 %2, i64 %3) {
store volatile i64 32, i64* inttoptr (i64 3758211072 to i64*)
br label %5
5: ; preds = %30, %4
%6 = phi i64* [ %1, %4 ], [ %32, %30 ]
%7 = phi i32 [ %2, %4 ], [ %31, %30 ]
br label %8
8: ; preds = %5, %8
%9 = load volatile i64, i64* inttoptr (i64 3758211072 to i64*)
%10 = and i64 %9, 8
%11 = icmp eq i64 %10, 0
br i1 %11, label %8, label %12
12: ; preds = %8
%13 = load volatile i64, i64* inttoptr (i64 3758211076 to i64*)
%14 = add i64 %13, -8
%15 = tail call i64 @llvm.fshl.i64(i64 %14, i64 %14, i64 61)
switch i64 %15, label %30 [
i64 0, label %16
i64 1, label %16
i64 3, label %17
i64 5, label %17
i64 6, label %17
i64 8, label %17
i64 2, label %18
i64 4, label %18
i64 7, label %24
i64 9, label %25
i64 10, label %25
]
16: ; preds = %12, %12
store volatile i64 %0, i64* inttoptr (i64 3758211080 to i64*)
store volatile i64 32, i64* inttoptr (i64 3758211096 to i64*)
br label %30
17: ; preds = %12, %12, %12, %12
store volatile i64 8, i64* inttoptr (i64 3758211096 to i64*)
br label %35
18: ; preds = %12, %12
%19 = icmp eq i32 %7, 0
br i1 %19, label %33, label %20
20: ; preds = %18
%21 = getelementptr inbounds i64, i64* %6, i64 1
%22 = load i64, i64* %6
store volatile i64 %22, i64* inttoptr (i64 3758211080 to i64*)
%23 = add nsw i32 %7, -1
br label %30
24: ; preds = %12
store volatile i64 4, i64* inttoptr (i64 3758211072 to i64*)
br label %30
25: ; preds = %12, %12
%26 = load volatile i64, i64* inttoptr (i64 3758211080 to i64*)
%27 = getelementptr inbounds i64, i64* %6, i64 1
store i64 %26, i64* %6
%28 = add nsw i32 %7, -1
switch i32 %7, label %30 [
i32 2, label %29
i32 1, label %33
]
29: ; preds = %25
store volatile i64 4, i64* inttoptr (i64 3758211096 to i64*)
br label %30
30: ; preds = %25, %29, %20, %12, %24, %16
%31 = phi i32 [ %7, %12 ], [ 1, %29 ], [ %7, %24 ], [ %23, %20 ], [ %7, %16 ], [ %28, %25 ]
%32 = phi i64* [ %6, %12 ], [ %27, %29 ], [ %6, %24 ], [ %21, %20 ], [ %6, %16 ], [ %27, %25 ]
store volatile i64 8, i64* inttoptr (i64 3758211096 to i64*)
br label %5
33: ; preds = %25, %18
store volatile i64 8, i64* inttoptr (i64 3758211096 to i64*)
%34 = icmp eq i64 %3, 0
br i1 %34, label %41, label %35
35: ; preds = %17, %33
%36 = phi i32 [ -1, %17 ], [ 0, %33 ]
store volatile i64 16, i64* inttoptr (i64 3758211072 to i64*)
br label %37
37: ; preds = %37, %35
%38 = load volatile i64, i64* inttoptr (i64 3758211072 to i64*)
%39 = and i64 %38, 16
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %37
41: ; preds = %37, %33
%42 = phi i32 [ 0, %33 ], [ %36, %37 ]
ret i32 %42
}