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


x86 assembly (clang, -O0):
<dl_write_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
lea    0xe85(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11a3 <dl_write_cmd+0x53>
lea    0xe7e(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x14(%rbp),%esi
lea    -0x1c(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11db <dl_write_cmd+0x8b>
lea    0xe52(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1206 <dl_write_cmd+0xb6>
lea    0xe36(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dl_write_cmd>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    %esi,(%rsp)
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     119c <dl_write_cmd+0x4c>
mov    %eax,%ebx
mov    %rsp,%rdx
mov    %eax,%edi
mov    %ebp,%esi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11a5 <dl_write_cmd+0x55>
mov    %ebx,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     11ae <dl_write_cmd+0x5e>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe6f(%rip),%rdi
jmp    11b5 <dl_write_cmd+0x65>
lea    0xe72(%rip),%rdi
jmp    11b5 <dl_write_cmd+0x65>
lea    0xe78(%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_write_cmd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [2 x i32]
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %6
%9 = load i32, i32* %4
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
store i32 %9, i32* %10
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %2
%19 = load i32, i32* %3
store i32 %19, i32* %7
%20 = load i32, i32* %5
%21 = load i32, i32* %7
%22 = bitcast [2 x i32]* %8 to i32**
%23 = call i32 @ioctl(i32 %20, i32 %21, i32** %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %18
%27 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %18
%30 = load i32, i32* %5
%31 = call i32 @close(i32 %30)
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %29
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%39 = load i32, i32* %38
ret i32 %39
}

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_write_cmd(i32 %0, i32 %1) {
%3 = alloca i32*
%4 = bitcast i32** %3 to i8*
%5 = bitcast i32** %3 to i32*
store i32 %1, i32* %5
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
%13 = call i32 @ioctl(i32 %7, i32 %0, i32** %3)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %12
%19 = call i32 @close(i32 %7)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %18
%25 = bitcast i32** %3 to [2 x i32]*
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %25, i64 0, i64 1
%27 = load i32, i32* %26
ret i32 %27
}