#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 errno;
int /*<<< orphan*/ printf(char *, char *, ...);
char *strerror(int);
int write(int, char *, int);
void writeToDevice(int fd, int reg, int val, char *name) {
  char buf[2];
  buf[0] = reg;
  buf[1] = val;
  if (write(fd, buf, 2) != 2) {
    printf("Can't write to %s\n", name);
    printf("error: %s (%d)\n", strerror(errno), errno);
  }
}


x86 assembly (clang, -O0):
<writeToDevice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %al,-0x1a(%rbp)
mov    -0xc(%rbp),%eax
mov    %al,-0x19(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
je     11a5 <writeToDevice+0x75>
mov    -0x18(%rbp),%rsi
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %rax,%rsi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeToDevice>:
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%rbx
mov    %sil,0xe(%rsp)
mov    %dl,0xf(%rsp)
lea    0xe(%rsp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
jne    115b <writeToDevice+0x2b>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe9e(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e6d(%rip),%rbx
mov    (%rbx),%edi
call   1050 <strerror@plt>
mov    (%rbx),%edx
lea    0xe90(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"Can't write to %s\0A\00"
@.str.1 = [16 x i8] c"error: %s (%d)\0A\00"
@errno = global i32 0
define void @writeToDevice(i32 %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca [2 x i8]
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%10 = load i32, i32* %6
%11 = trunc i32 %10 to i8
%12 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
store i8 %11, i8* %12
%13 = load i32, i32* %7
%14 = trunc i32 %13 to i8
%15 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 1
store i8 %14, i8* %15
%16 = load i32, i32* %5
%17 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
%18 = call i32 @write(i32 %16, i8* %17, i32 2)
%19 = icmp ne i32 %18, 2
br i1 %19, label %20, label %27
20: ; preds = %4
%21 = load i8*, i8** %8
%22 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = load i32, i32* @errno
%26 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %24, i32 %25)
br label %27
27: ; preds = %20, %4
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"Can't write to %s\0A\00"
@.str.1 = [16 x i8] c"error: %s (%d)\0A\00"
@errno = global i32 0
define void @writeToDevice(i32 %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca [2 x i8]
%6 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%7 = trunc i32 %1 to i8
store i8 %7, i8* %6
%8 = trunc i32 %2 to i8
%9 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
store i8 %8, i8* %9
%10 = call i32 @write(i32 %0, i8* %6, i32 2)
%11 = icmp eq i32 %10, 2
br i1 %11, label %18, label %12
12: ; preds = %4
%13 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %3)
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = load i32, i32* @errno
%17 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %15, i32 %16)
br label %18
18: ; preds = %12, %4
ret void
}