#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 */
struct iovec {
  char *iov_base;
  int iov_len;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ SYS_process_vm_readv;
int /*<<< orphan*/ printf(char *);
int syscall(int /*<<< orphan*/, int, struct iovec *, int, struct iovec *, int,
            unsigned long long);
int read_ksuse(int pid, int *ksuseopc_addr, int *ksusetim_addr) {
  struct iovec local[2];
  struct iovec remote[2];
  char buf1[4];
  char buf2[4];
  ssize_t nread;
  int ksusetim;
  int ksuseopc;
  local[0].iov_base = buf1;
  local[0].iov_len = 4;
  remote[0].iov_len = 4;
  remote[0].iov_base = ksuseopc_addr;
  local[1].iov_base = buf2;
  local[1].iov_len = 4;
  remote[1].iov_len = 4;
  remote[1].iov_base = ksusetim_addr;
  nread = syscall(SYS_process_vm_readv, pid, local, 2, remote, 2, 0ULL);
  if (nread != 8) {
    printf("Cannot read target process memory.\n");
    return -1;
  }
  ksuseopc = *(int *)buf1;
  ksusetim = *(int *)buf2;
  if (ksusetim > 0) {
    return 0;
  } else {
    return ksuseopc;
  }
}


x86 assembly (clang, -O0):
<read_ksuse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x64(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x4,-0x38(%rbp)
movl   $0x4,-0x58(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    -0x68(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x4,-0x28(%rbp)
movl   $0x4,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
lea    -0x40(%rbp),%rdx
lea    -0x60(%rbp),%r8
mov    $0x2,%r9d
xor    %eax,%eax
mov    %r9d,%ecx
movq   $0x0,(%rsp)
call   1040 <syscall@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x8,-0x6c(%rbp)
je     11c3 <read_ksuse+0xa3>
lea    0xe4e(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <read_ksuse+0xcb>
mov    -0x64(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0x68(%rbp),%eax
mov    %eax,-0x70(%rbp)
cmpl   $0x0,-0x70(%rbp)
jle    11e5 <read_ksuse+0xc5>
movl   $0x0,-0x4(%rbp)
jmp    11eb <read_ksuse+0xcb>
mov    -0x74(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_ksuse>:
sub    $0x58,%rsp
mov    %edi,%eax
lea    0xc(%rsp),%rcx
mov    %rcx,0x30(%rsp)
movl   $0x4,0x38(%rsp)
movl   $0x4,0x18(%rsp)
mov    %rsi,0x10(%rsp)
lea    0x8(%rsp),%rcx
mov    %rcx,0x40(%rsp)
movl   $0x4,0x48(%rsp)
movl   $0x4,0x28(%rsp)
mov    %rdx,0x20(%rsp)
mov    0x2e75(%rip),%rcx
mov    (%rcx),%edi
movq   $0x0,(%rsp)
lea    0x30(%rsp),%rdx
lea    0x10(%rsp),%r8
mov    %eax,%esi
mov    $0x2,%ecx
mov    $0x2,%r9d
call   1040 <syscall@plt>
cmp    $0x8,%eax
jne    11a8 <read_ksuse+0x88>
xor    %eax,%eax
cmpl   $0x0,0x8(%rsp)
jg     11a3 <read_ksuse+0x83>
mov    0xc(%rsp),%eax
add    $0x58,%rsp
ret
lea    0xe51(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x58,%rsp
ret

LLVM IR (-O0):
%struct.iovec = type { i8*, i32 }
@SYS_process_vm_readv = global i32 0
@.str = [36 x i8] c"Cannot read target process memory.\0A\00"
define i32 @read_ksuse(i32 %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [2 x %struct.iovec]
%9 = alloca [2 x %struct.iovec]
%10 = alloca [4 x i8]
%11 = alloca [4 x i8]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%15 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%16 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %8, i64 0, i64 0
%17 = getelementptr inbounds %struct.iovec, %struct.iovec* %16, i32 0, i32 0
store i8* %15, i8** %17
%18 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %8, i64 0, i64 0
%19 = getelementptr inbounds %struct.iovec, %struct.iovec* %18, i32 0, i32 1
store i32 4, i32* %19
%20 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %9, i64 0, i64 0
%21 = getelementptr inbounds %struct.iovec, %struct.iovec* %20, i32 0, i32 1
store i32 4, i32* %21
%22 = load i32*, i32** %6
%23 = bitcast i32* %22 to i8*
%24 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %9, i64 0, i64 0
%25 = getelementptr inbounds %struct.iovec, %struct.iovec* %24, i32 0, i32 0
store i8* %23, i8** %25
%26 = getelementptr inbounds [4 x i8], [4 x i8]* %11, i64 0, i64 0
%27 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %8, i64 0, i64 1
%28 = getelementptr inbounds %struct.iovec, %struct.iovec* %27, i32 0, i32 0
store i8* %26, i8** %28
%29 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %8, i64 0, i64 1
%30 = getelementptr inbounds %struct.iovec, %struct.iovec* %29, i32 0, i32 1
store i32 4, i32* %30
%31 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %9, i64 0, i64 1
%32 = getelementptr inbounds %struct.iovec, %struct.iovec* %31, i32 0, i32 1
store i32 4, i32* %32
%33 = load i32*, i32** %7
%34 = bitcast i32* %33 to i8*
%35 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %9, i64 0, i64 1
%36 = getelementptr inbounds %struct.iovec, %struct.iovec* %35, i32 0, i32 0
store i8* %34, i8** %36
%37 = load i32, i32* @SYS_process_vm_readv
%38 = load i32, i32* %5
%39 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %8, i64 0, i64 0
%40 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %9, i64 0, i64 0
%41 = call i32 @syscall(i32 %37, i32 %38, %struct.iovec* %39, i32 2, %struct.iovec* %40, i32 2, i64 0)
store i32 %41, i32* %12
%42 = load i32, i32* %12
%43 = icmp ne i32 %42, 8
br i1 %43, label %44, label %46
44: ; preds = %3
%45 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %58
46: ; preds = %3
%47 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%48 = bitcast i8* %47 to i32*
%49 = load i32, i32* %48
store i32 %49, i32* %14
%50 = getelementptr inbounds [4 x i8], [4 x i8]* %11, i64 0, i64 0
%51 = bitcast i8* %50 to i32*
%52 = load i32, i32* %51
store i32 %52, i32* %13
%53 = load i32, i32* %13
%54 = icmp sgt i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %46
store i32 0, i32* %4
br label %58
56: ; preds = %46
%57 = load i32, i32* %14
store i32 %57, i32* %4
br label %58
58: ; preds = %56, %55, %44
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
@SYS_process_vm_readv = global i32 0
@str = [35 x i8] c"Cannot read target process memory.\00"
define i32 @read_ksuse(i32 %0, i32* %1, i32* %2) {
%4 = alloca [2 x %struct.iovec]
%5 = alloca [2 x %struct.iovec]
%6 = alloca i32
%7 = alloca i32
%8 = bitcast [2 x %struct.iovec]* %4 to i8*
%9 = bitcast [2 x %struct.iovec]* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = bitcast i32* %7 to i8*
%12 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %4, i64 0, i64 0
%13 = bitcast [2 x %struct.iovec]* %4 to i32**
store i32* %6, i32** %13
%14 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %4, i64 0, i64 0, i32 1
store i32 4, i32* %14
%15 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %5, i64 0, i64 0
%16 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %5, i64 0, i64 0, i32 1
store i32 4, i32* %16
%17 = bitcast [2 x %struct.iovec]* %5 to i32**
store i32* %1, i32** %17
%18 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %4, i64 0, i64 1
%19 = bitcast %struct.iovec* %18 to i32**
store i32* %7, i32** %19
%20 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %4, i64 0, i64 1, i32 1
store i32 4, i32* %20
%21 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %5, i64 0, i64 1, i32 1
store i32 4, i32* %21
%22 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %5, i64 0, i64 1, i32 0
%23 = bitcast i8** %22 to i32**
store i32* %2, i32** %23
%24 = load i32, i32* @SYS_process_vm_readv
%25 = call i32 @syscall(i32 %24, i32 %0, %struct.iovec* %12, i32 2, %struct.iovec* %15, i32 2, i64 0)
%26 = icmp eq i32 %25, 8
br i1 %26, label %29, label %27
27: ; preds = %3
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %34
29: ; preds = %3
%30 = load i32, i32* %7
%31 = icmp sgt i32 %30, 0
%32 = load i32, i32* %6
%33 = select i1 %31, i32 0, i32 %32
br label %34
34: ; preds = %29, %27
%35 = phi i32 [ -1, %27 ], [ %33, %29 ]
ret i32 %35
}