#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 /*<<< orphan*/ O_RDWR;
int close(int);
int /*<<< orphan*/ exit(int);
int ioctl(int, int, int *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
unsigned int dl_read_cmd(unsigned int func) {
  int fd;
  int rc = 0, data, cmd;
  fd = open("/dev/spi_download", O_RDWR);
  if (fd == -1) {
    perror("\nopen faild");
    exit(-1);
  };
  cmd = func;
  rc = ioctl(fd, cmd, &data);
  if (rc == -1) {
    perror("\nioctrl faild\n");
    exit(-1);
  };
  rc = close(fd);
  if (rc == -1) {
    perror("\nclose faild");
    exit(-1);
  };
  return data;
}


x86 assembly (clang, -O0):
<dl_read_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
lea    0xe8e(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    119a <dl_read_cmd+0x4a>
lea    0xe87(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
lea    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11d2 <dl_read_cmd+0x82>
lea    0xe5b(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11fd <dl_read_cmd+0xad>
lea    0xe3f(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dl_read_cmd>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
lea    0xe9b(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     119b <dl_read_cmd+0x4b>
mov    %eax,%ebx
lea    0x4(%rsp),%rdx
mov    %eax,%edi
mov    %ebp,%esi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11a4 <dl_read_cmd+0x54>
mov    %ebx,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     11ad <dl_read_cmd+0x5d>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe70(%rip),%rdi
jmp    11b4 <dl_read_cmd+0x64>
lea    0xe73(%rip),%rdi
jmp    11b4 <dl_read_cmd+0x64>
lea    0xe79(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"/dev/spi_download\00"
@O_RDWR = global i32 0
@.str.1 = [12 x i8] c"\0Aopen faild\00"
@.str.2 = [15 x i8] c"\0Aioctrl faild\0A\00"
@.str.3 = [13 x i8] c"\0Aclose faild\00"
define i32 @dl_read_cmd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %4
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %1
%15 = load i32, i32* %2
store i32 %15, i32* %6
%16 = load i32, i32* %3
%17 = load i32, i32* %6
%18 = call i32 @ioctl(i32 %16, i32 %17, i32* %5)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %14
%25 = load i32, i32* %3
%26 = call i32 @close(i32 %25)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %24
%33 = load i32, i32* %5
ret i32 %33
}

LLVM IR (-O2):
@.str = [18 x i8] c"/dev/spi_download\00"
@O_RDWR = global i32 0
@.str.1 = [12 x i8] c"\0Aopen faild\00"
@.str.2 = [15 x i8] c"\0Aioctrl faild\0A\00"
@.str.3 = [13 x i8] c"\0Aclose faild\00"
define i32 @dl_read_cmd(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %1
%11 = call i32 @ioctl(i32 %5, i32 %0, i32* %2)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %10
%17 = call i32 @close(i32 %5)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %16
%23 = load i32, i32* %2
ret i32 %23
}