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

/* Variables and functions */
int be32toh(int);
int handle_delay(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ htobe32(int);
int /*<<< orphan*/ memcpy(char *, void *, int);
int rtas_call(char *, int, int, int /*<<< orphan*/, int /*<<< orphan*/,
              int /*<<< orphan*/, int *, int *, int *);
int /*<<< orphan*/ rtas_free_rmo_buffer(void *, int, int);
int rtas_get_rmo_buffer(int, void **, int *);
int sanity_check();
int rtas_physical_attestation(char *workarea, int seq_num, int *next_seq_num,
                              int *work_area_bytes) {
  uint32_t workarea_pa;
  uint64_t elapsed = 0;
  void *kernbuf;
  int kbuf_sz = 4096;
  int rc, status;
  int resp_bytes = *work_area_bytes;
  rc = sanity_check();
  if (rc)
    return rc;
  if (*work_area_bytes == 0 || *work_area_bytes > kbuf_sz)
    return -1098;
  rc = rtas_get_rmo_buffer(kbuf_sz, &kernbuf, &workarea_pa);
  if (rc)
    return rc;
  memcpy(kernbuf, workarea, *work_area_bytes);
  do {
    rc = rtas_call("ibm,physical-attestation", 3, 3, htobe32(workarea_pa),
                   htobe32(kbuf_sz), htobe32(seq_num), &status, next_seq_num,
                   &resp_bytes);
    if (rc < 0)
      break;
    rc = handle_delay(status, &elapsed);
  } while (rc == 1);
  *next_seq_num = be32toh(*next_seq_num);
  if (be32toh(resp_bytes) > *work_area_bytes) {
    (void)rtas_free_rmo_buffer(kernbuf, workarea_pa, kbuf_sz);
    return -1098;
  }
  *work_area_bytes = be32toh(resp_bytes);
  if (rc == 0)
    memcpy(workarea, kernbuf, *work_area_bytes);
  (void)rtas_free_rmo_buffer(kernbuf, workarea_pa, kbuf_sz);
  return rc ? rc : status;
}


x86 assembly (clang, -O0):
<rtas_physical_attestation>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x1000,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    $0x0,%al
call   1090 <sanity_check@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
je     11cd <rtas_physical_attestation+0x4d>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    134f <rtas_physical_attestation+0x1cf>
mov    -0x28(%rbp),%rax
cmpl   $0x0,(%rax)
je     11e9 <rtas_physical_attestation+0x69>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
cmp    -0x3c(%rbp),%eax
jle    11f5 <rtas_physical_attestation+0x75>
movl   $0xfffffbb6,-0x4(%rbp)
jmp    134f <rtas_physical_attestation+0x1cf>
mov    -0x3c(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1070 <rtas_get_rmo_buffer@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
je     121d <rtas_physical_attestation+0x9d>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    134f <rtas_physical_attestation+0x1cf>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1040 <memcpy@plt>
mov    -0x2c(%rbp),%edi
call   1080 <htobe32@plt>
mov    %eax,-0x50(%rbp)
mov    -0x3c(%rbp),%edi
call   1080 <htobe32@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <htobe32@plt>
mov    -0x50(%rbp),%ecx
mov    -0x4c(%rbp),%r8d
mov    %eax,%r9d
mov    -0x20(%rbp),%r10
lea    0xd9d(%rip),%rdi
mov    $0x3,%edx
lea    -0x44(%rbp),%r11
lea    -0x48(%rbp),%rax
mov    %edx,%esi
mov    %r11,(%rsp)
mov    %r10,0x8(%rsp)
mov    %rax,0x10(%rsp)
call   1030 <rtas_call@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    1297 <rtas_physical_attestation+0x117>
jmp    12b0 <rtas_physical_attestation+0x130>
mov    -0x44(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1050 <handle_delay@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x1,-0x40(%rbp)
je     1230 <rtas_physical_attestation+0xb0>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1060 <be32toh@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x48(%rbp),%edi
call   1060 <be32toh@plt>
mov    -0x28(%rbp),%rcx
cmp    (%rcx),%eax
jle    12f2 <rtas_physical_attestation+0x172>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x3c(%rbp),%edx
call   10a0 <rtas_free_rmo_buffer@plt>
movl   $0xfffffbb6,-0x4(%rbp)
jmp    134f <rtas_physical_attestation+0x1cf>
mov    -0x48(%rbp),%edi
call   1060 <be32toh@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x40(%rbp)
jne    131f <rtas_physical_attestation+0x19f>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1040 <memcpy@plt>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x3c(%rbp),%edx
call   10a0 <rtas_free_rmo_buffer@plt>
cmpl   $0x0,-0x40(%rbp)
je     1343 <rtas_physical_attestation+0x1c3>
mov    -0x40(%rbp),%eax
mov    %eax,-0x54(%rbp)
jmp    1349 <rtas_physical_attestation+0x1c9>
mov    -0x44(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtas_physical_attestation>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r15
mov    %rdx,%r12
mov    %esi,%ebp
mov    %rdi,%r14
movl   $0x0,0x1c(%rsp)
mov    (%rcx),%eax
mov    %eax,0x4(%rsp)
xor    %eax,%eax
call   1090 <sanity_check@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11c5 <rtas_physical_attestation+0x45>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r15),%eax
mov    $0xfffffbb6,%ebx
test   %eax,%eax
je     11b4 <rtas_physical_attestation+0x34>
cmp    $0x1000,%eax
jg     11b4 <rtas_physical_attestation+0x34>
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    $0x1000,%edi
call   1070 <rtas_get_rmo_buffer@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    11b4 <rtas_physical_attestation+0x34>
mov    0x10(%rsp),%rdi
mov    (%r15),%edx
mov    %r14,0x20(%rsp)
mov    %r14,%rsi
call   1040 <memcpy@plt>
lea    0xdf2(%rip),%r13
xchg   %ax,%ax
mov    0x8(%rsp),%edi
call   1080 <htobe32@plt>
mov    %eax,%ebx
mov    $0x1000,%edi
call   1080 <htobe32@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1080 <htobe32@plt>
sub    $0x8,%rsp
mov    %r13,%rdi
mov    $0x3,%esi
mov    $0x3,%edx
mov    %ebx,%ecx
mov    %r14d,%r8d
mov    %eax,%r9d
lea    0xc(%rsp),%rax
push   %rax
push   %r12
lea    0x24(%rsp),%rax
push   %rax
call   1030 <rtas_call@plt>
add    $0x20,%rsp
test   %eax,%eax
js     1276 <rtas_physical_attestation+0xf6>
mov    0xc(%rsp),%edi
lea    0x1c(%rsp),%rsi
call   1050 <handle_delay@plt>
cmp    $0x1,%eax
je     1210 <rtas_physical_attestation+0x90>
mov    %eax,%ebx
mov    (%r12),%edi
call   1060 <be32toh@plt>
mov    %eax,(%r12)
mov    0x4(%rsp),%edi
call   1060 <be32toh@plt>
cmp    (%r15),%eax
jle    12b0 <rtas_physical_attestation+0x130>
mov    0x10(%rsp),%rdi
mov    0x8(%rsp),%esi
mov    $0x1000,%edx
call   10a0 <rtas_free_rmo_buffer@plt>
mov    $0xfffffbb6,%ebx
jmp    11b4 <rtas_physical_attestation+0x34>
mov    0x4(%rsp),%edi
call   1060 <be32toh@plt>
mov    %eax,(%r15)
test   %ebx,%ebx
jne    12d1 <rtas_physical_attestation+0x151>
mov    0x10(%rsp),%rsi
mov    0x20(%rsp),%rdi
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    0x10(%rsp),%rdi
mov    0x8(%rsp),%esi
mov    $0x1000,%edx
call   10a0 <rtas_free_rmo_buffer@plt>
test   %ebx,%ebx
jne    11b4 <rtas_physical_attestation+0x34>
mov    0xc(%rsp),%ebx
jmp    11b4 <rtas_physical_attestation+0x34>

LLVM IR (-O0):
@.str = [25 x i8] c"ibm,physical-attestation\00"
define i32 @rtas_physical_attestation(i8* %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32 0, i32* %11
store i32 4096, i32* %13
%17 = load i32*, i32** %9
%18 = load i32, i32* %17
store i32 %18, i32* %16
%19 = call i32 (...) @sanity_check()
store i32 %19, i32* %14
%20 = load i32, i32* %14
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %4
%23 = load i32, i32* %14
store i32 %23, i32* %5
br label %105
24: ; preds = %4
%25 = load i32*, i32** %9
%26 = load i32, i32* %25
%27 = icmp eq i32 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %24
%29 = load i32*, i32** %9
%30 = load i32, i32* %29
%31 = load i32, i32* %13
%32 = icmp sgt i32 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %28, %24
store i32 -1098, i32* %5
br label %105
34: ; preds = %28
%35 = load i32, i32* %13
%36 = call i32 @rtas_get_rmo_buffer(i32 %35, i8** %12, i32* %10)
store i32 %36, i32* %14
%37 = load i32, i32* %14
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = load i32, i32* %14
store i32 %40, i32* %5
br label %105
41: ; preds = %34
%42 = load i8*, i8** %12
%43 = load i8*, i8** %6
%44 = load i32*, i32** %9
%45 = load i32, i32* %44
%46 = call i32 @memcpy(i8* %42, i8* %43, i32 %45)
br label %47
47: ; preds = %62, %41
%48 = load i32, i32* %10
%49 = call i32 @htobe32(i32 %48)
%50 = load i32, i32* %13
%51 = call i32 @htobe32(i32 %50)
%52 = load i32, i32* %7
%53 = call i32 @htobe32(i32 %52)
%54 = load i32*, i32** %8
%55 = call i32 @rtas_call(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 3, i32 3, i32 %49, i32 %51, i32 %53, i32* %15, i32* %54, i32* %16)
store i32 %55, i32* %14
%56 = load i32, i32* %14
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %47
br label %65
59: ; preds = %47
%60 = load i32, i32* %15
%61 = call i32 @handle_delay(i32 %60, i32* %11)
store i32 %61, i32* %14
br label %62
62: ; preds = %59
%63 = load i32, i32* %14
%64 = icmp eq i32 %63, 1
br i1 %64, label %47, label %65
65: ; preds = %62, %58
%66 = load i32*, i32** %8
%67 = load i32, i32* %66
%68 = call i32 @be32toh(i32 %67)
%69 = load i32*, i32** %8
store i32 %68, i32* %69
%70 = load i32, i32* %16
%71 = call i32 @be32toh(i32 %70)
%72 = load i32*, i32** %9
%73 = load i32, i32* %72
%74 = icmp sgt i32 %71, %73
br i1 %74, label %75, label %80
75: ; preds = %65
%76 = load i8*, i8** %12
%77 = load i32, i32* %10
%78 = load i32, i32* %13
%79 = call i32 @rtas_free_rmo_buffer(i8* %76, i32 %77, i32 %78)
store i32 -1098, i32* %5
br label %105
80: ; preds = %65
%81 = load i32, i32* %16
%82 = call i32 @be32toh(i32 %81)
%83 = load i32*, i32** %9
store i32 %82, i32* %83
%84 = load i32, i32* %14
%85 = icmp eq i32 %84, 0
br i1 %85, label %86, label %92
86: ; preds = %80
%87 = load i8*, i8** %6
%88 = load i8*, i8** %12
%89 = load i32*, i32** %9
%90 = load i32, i32* %89
%91 = call i32 @memcpy(i8* %87, i8* %88, i32 %90)
br label %92
92: ; preds = %86, %80
%93 = load i8*, i8** %12
%94 = load i32, i32* %10
%95 = load i32, i32* %13
%96 = call i32 @rtas_free_rmo_buffer(i8* %93, i32 %94, i32 %95)
%97 = load i32, i32* %14
%98 = icmp ne i32 %97, 0
br i1 %98, label %99, label %101
99: ; preds = %92
%100 = load i32, i32* %14
br label %103
101: ; preds = %92
%102 = load i32, i32* %15
br label %103
103: ; preds = %101, %99
%104 = phi i32 [ %100, %99 ], [ %102, %101 ]
store i32 %104, i32* %5
br label %105
105: ; preds = %103, %75, %39, %33, %22
%106 = load i32, i32* %5
ret i32 %106
}

LLVM IR (-O2):
@.str = [25 x i8] c"ibm,physical-attestation\00"
define i32 @rtas_physical_attestation(i8* %0, i32 %1, i32* %2, i32* nocapture %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = bitcast i32* %5 to i8*
%11 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%12 = bitcast i8** %7 to i8*
%13 = bitcast i32* %8 to i8*
%14 = bitcast i32* %9 to i8*
%15 = load i32, i32* %3
store i32 %15, i32* %9
%16 = tail call i32 (...) @sanity_check()
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %66
18: ; preds = %4
%19 = load i32, i32* %3
%20 = icmp eq i32 %19, 0
%21 = icmp sgt i32 %19, 4096
%22 = or i1 %20, %21
br i1 %22, label %66, label %23
23: ; preds = %18
%24 = call i32 @rtas_get_rmo_buffer(i32 4096, i8** %7, i32* %5)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %66
26: ; preds = %23
%27 = load i8*, i8** %7
%28 = load i32, i32* %3
%29 = call i32 @memcpy(i8* %27, i8* %0, i32 %28)
br label %30
30: ; preds = %37, %26
%31 = load i32, i32* %5
%32 = call i32 @htobe32(i32 %31)
%33 = call i32 @htobe32(i32 4096)
%34 = call i32 @htobe32(i32 %1)
%35 = call i32 @rtas_call(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 3, i32 3, i32 %32, i32 %33, i32 %34, i32* %8, i32* %2, i32* %9)
%36 = icmp slt i32 %35, 0
br i1 %36, label %41, label %37
37: ; preds = %30
%38 = load i32, i32* %8
%39 = call i32 @handle_delay(i32 %38, i32* %6)
%40 = icmp eq i32 %39, 1
br i1 %40, label %30, label %41
41: ; preds = %30, %37
%42 = phi i32 [ %35, %30 ], [ %39, %37 ]
%43 = load i32, i32* %2
%44 = call i32 @be32toh(i32 %43)
store i32 %44, i32* %2
%45 = load i32, i32* %9
%46 = call i32 @be32toh(i32 %45)
%47 = load i32, i32* %3
%48 = icmp sgt i32 %46, %47
br i1 %48, label %49, label %53
49: ; preds = %41
%50 = load i8*, i8** %7
%51 = load i32, i32* %5
%52 = call i32 @rtas_free_rmo_buffer(i8* %50, i32 %51, i32 4096)
br label %66
53: ; preds = %41
%54 = load i32, i32* %9
%55 = call i32 @be32toh(i32 %54)
store i32 %55, i32* %3
%56 = icmp eq i32 %42, 0
br i1 %56, label %57, label %60
57: ; preds = %53
%58 = load i8*, i8** %7
%59 = call i32 @memcpy(i8* %0, i8* %58, i32 %55)
br label %60
60: ; preds = %57, %53
%61 = load i8*, i8** %7
%62 = load i32, i32* %5
%63 = call i32 @rtas_free_rmo_buffer(i8* %61, i32 %62, i32 4096)
%64 = load i32, i32* %8
%65 = select i1 %56, i32 %64, i32 %42
br label %66
66: ; preds = %23, %18, %4, %60, %49
%67 = phi i32 [ -1098, %49 ], [ %65, %60 ], [ %16, %4 ], [ -1098, %18 ], [ %24, %23 ]
ret i32 %67
}