#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 /*<<< orphan*/ close(int);
int errno;
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ write(int, unsigned short *, unsigned short);
int write_dac(unsigned short *buffer, unsigned short cnt) {
  int fd;
  fd = open("/dev/ppi", O_RDWR, 0);
  if (fd < 1) {
    printf("Could not open dev ppi : %d \n", errno);
    return fd;
  }
  write(fd, buffer, cnt * 2);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<write_dac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
lea    0xe9d(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jge    1197 <write_dac+0x57>
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xe82(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bc <write_dac+0x7c>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
movzwl -0x12(%rbp),%eax
shl    $0x1,%eax
movzwl %ax,%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_dac>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
lea    0xea7(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1180 <write_dac+0x40>
add    %ebp,%ebp
movzwl %bp,%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
jmp    1195 <write_dac+0x55>
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1040 <printf@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/ppi\00"
@O_RDWR = global i32 0
@.str.1 = [30 x i8] c"Could not open dev ppi : %d \0A\00"
@errno = global i32 0
define i32 @write_dac(i16* %0, i16 zeroext %1) {
%3 = alloca i32
%4 = alloca i16*
%5 = alloca i16
%6 = alloca i32
store i16* %0, i16** %4
store i16 %1, i16* %5
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7, i32 0)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @errno
%13 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %6
store i32 %14, i32* %3
br label %25
15: ; preds = %2
%16 = load i32, i32* %6
%17 = load i16*, i16** %4
%18 = load i16, i16* %5
%19 = zext i16 %18 to i32
%20 = mul nsw i32 %19, 2
%21 = trunc i32 %20 to i16
%22 = call i32 @write(i32 %16, i16* %17, i16 zeroext %21)
%23 = load i32, i32* %6
%24 = call i32 @close(i32 %23)
store i32 0, i32* %3
br label %25
25: ; preds = %15, %11
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/ppi\00"
@O_RDWR = global i32 0
@.str.1 = [30 x i8] c"Could not open dev ppi : %d \0A\00"
@errno = global i32 0
define i32 @write_dac(i16* nocapture %0, i16 zeroext %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3, i32 0)
%5 = icmp slt i32 %4, 1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @errno
%8 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %7)
br label %13
9: ; preds = %2
%10 = shl i16 %1, 1
%11 = tail call i32 @write(i32 %4, i16* %0, i16 zeroext %10)
%12 = tail call i32 @close(i32 %4)
br label %13
13: ; preds = %9, %6
%14 = phi i32 [ %4, %6 ], [ 0, %9 ]
ret i32 %14
}