#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 uint8_t;
typedef int /*<<< orphan*/ dat;
typedef int /*<<< orphan*/ cmd;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(int *, int, int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ pabort(char *);
int /*<<< orphan*/ print_buffer(int *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ psx_spi_do_msg(int, int *, int *, int);
int /*<<< orphan*/ psx_spi_setup(int);
int /*<<< orphan*/ spi_dump_stat(int);
int psx_get_id(const char *spi_device) {
  uint8_t cmd[] = {0x81, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  uint8_t dat[(sizeof(cmd) / sizeof((cmd)[0]))] = {
      0,
  };
  int ret = 0;
  int fd;
  memset(dat, 0xff, (sizeof(cmd) / sizeof((cmd)[0])));
  fd = open(spi_device, O_RDWR);
  if (fd < 0)
    pabort("psx_get_id() can't open device");
  psx_spi_setup(fd);
  spi_dump_stat(fd);
  psx_spi_do_msg(fd, cmd, dat, (sizeof(cmd) / sizeof((cmd)[0])));
  close(fd);
  printf("PSX get id\n");
  print_buffer(dat, (sizeof(dat) / sizeof((dat)[0])));
  return ret;
}


x86 assembly (clang, -O0):
<psx_get_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
movl   $0x81,-0x30(%rbp)
movl   $0x53,-0x2c(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
movl   $0x0,-0x64(%rbp)
lea    -0x60(%rbp),%rdi
mov    $0xff,%esi
mov    $0xa,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x68(%rbp)
cmpl   $0x0,-0x68(%rbp)
jge    120f <psx_get_id+0x7f>
lea    0xdf6(%rip),%rdi
call   10a0 <pabort@plt>
mov    -0x68(%rbp),%edi
call   1080 <psx_spi_setup@plt>
mov    -0x68(%rbp),%edi
call   1030 <spi_dump_stat@plt>
mov    -0x68(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x60(%rbp),%rdx
mov    $0xa,%ecx
call   1040 <psx_spi_do_msg@plt>
mov    -0x68(%rbp),%edi
call   1070 <close@plt>
lea    0xddc(%rip),%rdi
call   1050 <printf@plt>
lea    -0x60(%rbp),%rdi
mov    $0xa,%esi
call   10b0 <print_buffer@plt>
mov    -0x64(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<psx_get_id>:
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    %rdi,%rbx
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x40(%rsp)
movq   $0x0,0x50(%rsp)
movabs $0x5300000081,%rax
mov    %rax,0x30(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movq   $0x0,0x20(%rsp)
mov    %rsp,%rdi
mov    $0xff,%esi
mov    $0xa,%edx
call   1060 <memset@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1206 <psx_get_id+0x76>
lea    0xdff(%rip),%rdi
call   10a0 <pabort@plt>
mov    %ebx,%edi
call   1080 <psx_spi_setup@plt>
mov    %ebx,%edi
call   1030 <spi_dump_stat@plt>
lea    0x30(%rsp),%rsi
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rdx
mov    $0xa,%ecx
call   1050 <psx_spi_do_msg@plt>
mov    %ebx,%edi
call   1070 <close@plt>
lea    0xde6(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
mov    $0xa,%esi
call   10b0 <print_buffer@plt>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [31 x i8] c"psx_get_id() can't open device\00"
@.str.1 = [12 x i8] c"PSX get id\0A\00"
define i32 @psx_get_id(i8* %0) {
%2 = alloca i8*
%3 = alloca [10 x i32]
%4 = alloca [10 x i32]
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = bitcast [10 x i32]* %3 to i8*
%8 = bitcast i8* %7 to <{ i32, i32, [8 x i32] }>*
%9 = getelementptr inbounds <{ i32, i32, [8 x i32] }>, <{ i32, i32, [8 x i32] }>* %8, i32 0, i32 0
store i32 129, i32* %9
%10 = getelementptr inbounds <{ i32, i32, [8 x i32] }>, <{ i32, i32, [8 x i32] }>* %8, i32 0, i32 1
store i32 83, i32* %10
%11 = bitcast [10 x i32]* %4 to i8*
store i32 0, i32* %5
%12 = getelementptr inbounds [10 x i32], [10 x i32]* %4, i64 0, i64 0
%13 = call i32 @memset(i32* %12, i32 255, i32 10)
%14 = load i8*, i8** %2
%15 = load i32, i32* @O_RDWR
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = call i32 @pabort(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %1
%22 = load i32, i32* %6
%23 = call i32 @psx_spi_setup(i32 %22)
%24 = load i32, i32* %6
%25 = call i32 @spi_dump_stat(i32 %24)
%26 = load i32, i32* %6
%27 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 0
%28 = getelementptr inbounds [10 x i32], [10 x i32]* %4, i64 0, i64 0
%29 = call i32 @psx_spi_do_msg(i32 %26, i32* %27, i32* %28, i32 10)
%30 = load i32, i32* %6
%31 = call i32 @close(i32 %30)
%32 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%33 = getelementptr inbounds [10 x i32], [10 x i32]* %4, i64 0, i64 0
%34 = call i32 @print_buffer(i32* %33, i32 10)
%35 = load i32, i32* %5
ret i32 %35
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [31 x i8] c"psx_get_id() can't open device\00"
@str = [11 x i8] c"PSX get id\00"
define i32 @psx_get_id(i8* nocapture %0) {
%2 = alloca [10 x i32]
%3 = alloca [10 x i32]
%4 = bitcast [10 x i32]* %2 to i8*
%5 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
store i32 129, i32* %5
%6 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 1
store i32 83, i32* %6
%7 = bitcast [10 x i32]* %3 to i8*
%8 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 0
%9 = call i32 @memset(i32* %8, i32 255, i32 10)
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* %0, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @pabort(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %1
%16 = call i32 @psx_spi_setup(i32 %11)
%17 = call i32 @spi_dump_stat(i32 %11)
%18 = call i32 @psx_spi_do_msg(i32 %11, i32* %5, i32* %8, i32 10)
%19 = call i32 @close(i32 %11)
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%21 = call i32 @print_buffer(i32* %8, i32 10)
ret i32 0
}