#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 atoi(char *);
scalar_t__ cbm_talk(int, int, int);
int /*<<< orphan*/ cbm_untalk(int);
int read(int, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int cbm_device_status(int f, int drv, char *buf, int bufsize) {
  strncpy(buf, "99, DRIVER ERROR,00,00\r", bufsize);
  if (cbm_talk(f, drv, 15) == 0) {
    int bytes_read = read(f, buf, bufsize);
    if (bytes_read == bufsize) {
      bytes_read--;
    }
    buf[bytes_read] = '\0';
    cbm_untalk(f);
  }
  return atoi(buf);
}


x86 assembly (clang, -O0):
<cbm_device_status>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe8d(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    $0xf,%edx
call   1070 <cbm_talk@plt>
cmp    $0x0,%rax
jne    11cd <cbm_device_status+0x7d>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    11b9 <cbm_device_status+0x69>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x4(%rbp),%edi
call   1050 <cbm_untalk@plt>
mov    -0x10(%rbp),%rdi
call   1060 <atoi@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cbm_device_status>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %edi,%r14d
lea    0xe97(%rip),%rsi
mov    %rdx,%rdi
mov    %ecx,%edx
call   1030 <strncpy@plt>
mov    %r14d,%edi
mov    %ebp,%esi
mov    $0xf,%edx
call   1070 <cbm_talk@plt>
test   %rax,%rax
jne    11ad <cbm_device_status+0x5d>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <read@plt>
xor    %ecx,%ecx
cmp    %r15d,%eax
sete   %cl
sub    %ecx,%eax
cltq
movb   $0x0,(%rbx,%rax,1)
mov    %r14d,%edi
call   1050 <cbm_untalk@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <atoi@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"99, DRIVER ERROR,00,00\0D\00"
define i32 @cbm_device_status(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%10 = load i8*, i8** %7
%11 = load i32, i32* %8
%12 = call i32 @strncpy(i8* %10, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %5
%14 = load i32, i32* %6
%15 = call i64 @cbm_talk(i32 %13, i32 %14, i32 15)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %35
17: ; preds = %4
%18 = load i32, i32* %5
%19 = load i8*, i8** %7
%20 = load i32, i32* %8
%21 = call i32 @read(i32 %18, i8* %19, i32 %20)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = load i32, i32* %8
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = load i32, i32* %9
%27 = add nsw i32 %26, -1
store i32 %27, i32* %9
br label %28
28: ; preds = %25, %17
%29 = load i8*, i8** %7
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 0, i8* %32
%33 = load i32, i32* %5
%34 = call i32 @cbm_untalk(i32 %33)
br label %35
35: ; preds = %28, %4
%36 = load i8*, i8** %7
%37 = call i32 @atoi(i8* %36)
ret i32 %37
}

LLVM IR (-O2):
@.str = [24 x i8] c"99, DRIVER ERROR,00,00\0D\00"
define i32 @cbm_device_status(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = tail call i32 @strncpy(i8* %2, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %3)
%6 = tail call i64 @cbm_talk(i32 %0, i32 %1, i32 15)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %16
8: ; preds = %4
%9 = tail call i32 @read(i32 %0, i8* %2, i32 %3)
%10 = icmp eq i32 %9, %3
%11 = sext i1 %10 to i32
%12 = add nsw i32 %9, %11
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %2, i64 %13
store i8 0, i8* %14
%15 = tail call i32 @cbm_untalk(i32 %0)
br label %16
16: ; preds = %8, %4
%17 = tail call i32 @atoi(i8* %2)
ret i32 %17
}