#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 *, int, int, ...);
int write(int, unsigned char *, int);
int local_cam_set_register(int filehandle, int iDataWidth, int reg_addr,
                           int val) {
  unsigned char buf[10];
  buf[0] = (unsigned char)reg_addr;
  if (iDataWidth == 1) {
    buf[1] = val & 0xFF;
  } else {
    buf[1] = (val >> 8) & 0xFF;
    buf[2] = val & 0xFF;
  }
  if (write(filehandle, buf, iDataWidth + 1) == iDataWidth + 1) {
    if (iDataWidth == 1) {
      printf("local_cam_set_register: 0x%x 0x%x written\r\n", buf[0], buf[1]);
    } else {
      printf("local_cam_set_register: 0x%x 0x%x 0x%x written\r\n", buf[0],
             buf[1], buf[2]);
    }
    return 0;
  }
  if (iDataWidth == 1) {
    printf("local_cam_set_register: Error sending 0x%02x at 0x%02x, errno = "
           "%d\r\n",
           val, reg_addr, errno);
  } else {
    printf("local_cam_set_register: Error sending 0x%04x at 0x%02x, errno = "
           "%d\r\n",
           val, reg_addr, errno);
  }
  return (-1);
}


x86 assembly (clang, -O0):
<local_cam_set_register>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    %al,-0x1e(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    1154 <local_cam_set_register+0x34>
mov    -0x14(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x1d(%rbp)
jmp    116d <local_cam_set_register+0x4d>
mov    -0x14(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x1d(%rbp)
mov    -0x14(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1e(%rbp),%rsi
mov    -0xc(%rbp),%edx
add    $0x1,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jne    11d8 <local_cam_set_register+0xb8>
cmpl   $0x1,-0xc(%rbp)
jne    11b2 <local_cam_set_register+0x92>
movzbl -0x1e(%rbp),%esi
movzbl -0x1d(%rbp),%edx
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11cc <local_cam_set_register+0xac>
movzbl -0x1e(%rbp),%esi
movzbl -0x1d(%rbp),%edx
movzbl -0x1c(%rbp),%ecx
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1228 <local_cam_set_register+0x108>
cmpl   $0x1,-0xc(%rbp)
jne    1204 <local_cam_set_register+0xe4>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    0x2df1(%rip),%rax
mov    (%rax),%ecx
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1221 <local_cam_set_register+0x101>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    0x2dcf(%rip),%rax
mov    (%rax),%ecx
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<local_cam_set_register>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%ebx
mov    %edx,%r14d
mov    %r14b,0x6(%rsp)
cmp    $0x1,%esi
jne    1168 <local_cam_set_register+0x48>
mov    %bl,0x7(%rsp)
lea    0x6(%rsp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
jne    11a9 <local_cam_set_register+0x89>
movzbl %r14b,%esi
movzbl %bl,%edx
lea    0xea3(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11dc <local_cam_set_register+0xbc>
mov    %esi,%ebp
mov    %ebx,%eax
rol    $0x8,%ax
mov    %ax,0x7(%rsp)
add    $0x1,%ebp
lea    0x6(%rsp),%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jne    11bb <local_cam_set_register+0x9b>
mov    %ebx,%eax
shr    $0x8,%eax
movzbl %r14b,%esi
movzbl %al,%edx
movzbl %bl,%ecx
lea    0xe8e(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11dc <local_cam_set_register+0xbc>
mov    0x2e30(%rip),%rax
mov    (%rax),%ecx
lea    0xea4(%rip),%rdi
jmp    11cb <local_cam_set_register+0xab>
mov    0x2e1e(%rip),%rax
mov    (%rax),%ecx
lea    0xed7(%rip),%rdi
mov    %ebx,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [44 x i8] c"local_cam_set_register: 0x%x 0x%x written\0D\0A\00"
@.str.1 = [49 x i8] c"local_cam_set_register: 0x%x 0x%x 0x%x written\0D\0A\00"
@.str.2 = [69 x i8] c"local_cam_set_register: Error sending 0x%02x at 0x%02x, errno = %d\0D\0A\00"
@errno = global i32 0
@.str.3 = [69 x i8] c"local_cam_set_register: Error sending 0x%04x at 0x%02x, errno = %d\0D\0A\00"
define i32 @local_cam_set_register(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [10 x i8]
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%11 = load i32, i32* %8
%12 = trunc i32 %11 to i8
%13 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
store i8 %12, i8* %13
%14 = load i32, i32* %7
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %21
16: ; preds = %4
%17 = load i32, i32* %9
%18 = and i32 %17, 255
%19 = trunc i32 %18 to i8
%20 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 1
store i8 %19, i8* %20
br label %31
21: ; preds = %4
%22 = load i32, i32* %9
%23 = ashr i32 %22, 8
%24 = and i32 %23, 255
%25 = trunc i32 %24 to i8
%26 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 1
store i8 %25, i8* %26
%27 = load i32, i32* %9
%28 = and i32 %27, 255
%29 = trunc i32 %28 to i8
%30 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 2
store i8 %29, i8* %30
br label %31
31: ; preds = %21, %16
%32 = load i32, i32* %6
%33 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%34 = load i32, i32* %7
%35 = add nsw i32 %34, 1
%36 = call i32 @write(i32 %32, i8* %33, i32 %35)
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
%39 = icmp eq i32 %36, %38
br i1 %39, label %40, label %63
40: ; preds = %31
%41 = load i32, i32* %7
%42 = icmp eq i32 %41, 1
br i1 %42, label %43, label %51
43: ; preds = %40
%44 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%45 = load i8, i8* %44
%46 = zext i8 %45 to i32
%47 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 1
%48 = load i8, i8* %47
%49 = zext i8 %48 to i32
%50 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %46, i32 %49)
br label %62
51: ; preds = %40
%52 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%53 = load i8, i8* %52
%54 = zext i8 %53 to i32
%55 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 1
%56 = load i8, i8* %55
%57 = zext i8 %56 to i32
%58 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 2
%59 = load i8, i8* %58
%60 = zext i8 %59 to i32
%61 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %54, i32 %57, i32 %60)
br label %62
62: ; preds = %51, %43
store i32 0, i32* %5
br label %77
63: ; preds = %31
%64 = load i32, i32* %7
%65 = icmp eq i32 %64, 1
br i1 %65, label %66, label %71
66: ; preds = %63
%67 = load i32, i32* %9
%68 = load i32, i32* %8
%69 = load i32, i32* @errno
%70 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.2, i64 0, i64 0), i32 %67, i32 %68, i32 %69)
br label %76
71: ; preds = %63
%72 = load i32, i32* %9
%73 = load i32, i32* %8
%74 = load i32, i32* @errno
%75 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.3, i64 0, i64 0), i32 %72, i32 %73, i32 %74)
br label %76
76: ; preds = %71, %66
store i32 -1, i32* %5
br label %77
77: ; preds = %76, %62
%78 = load i32, i32* %5
ret i32 %78
}

LLVM IR (-O2):
@.str = [44 x i8] c"local_cam_set_register: 0x%x 0x%x written\0D\0A\00"
@.str.1 = [49 x i8] c"local_cam_set_register: 0x%x 0x%x 0x%x written\0D\0A\00"
@.str.2 = [69 x i8] c"local_cam_set_register: Error sending 0x%02x at 0x%02x, errno = %d\0D\0A\00"
@errno = global i32 0
@.str.3 = [69 x i8] c"local_cam_set_register: Error sending 0x%04x at 0x%02x, errno = %d\0D\0A\00"
define i32 @local_cam_set_register(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca [10 x i8]
%6 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%7 = trunc i32 %2 to i8
store i8 %7, i8* %6
%8 = icmp eq i32 %1, 1
br i1 %8, label %9, label %14
9: ; preds = %4
%10 = trunc i32 %3 to i8
%11 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 1
store i8 %10, i8* %11
%12 = call i32 @write(i32 %0, i8* %6, i32 2)
%13 = icmp eq i32 %12, 2
br i1 %13, label %23, label %32
14: ; preds = %4
%15 = lshr i32 %3, 8
%16 = trunc i32 %15 to i8
%17 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 1
store i8 %16, i8* %17
%18 = trunc i32 %3 to i8
%19 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 2
store i8 %18, i8* %19
%20 = add nsw i32 %1, 1
%21 = call i32 @write(i32 %0, i8* %6, i32 %20)
%22 = icmp eq i32 %21, %20
br i1 %22, label %27, label %35
23: ; preds = %9
%24 = and i32 %2, 255
%25 = and i32 %3, 255
%26 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %25)
br label %38
27: ; preds = %14
%28 = and i32 %2, 255
%29 = and i32 %15, 255
%30 = and i32 %3, 255
%31 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %28, i32 %29, i32 %30)
br label %38
32: ; preds = %9
%33 = load i32, i32* @errno
%34 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.2, i64 0, i64 0), i32 %3, i32 %2, i32 %33)
br label %38
35: ; preds = %14
%36 = load i32, i32* @errno
%37 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.3, i64 0, i64 0), i32 %3, i32 %2, i32 %36)
br label %38
38: ; preds = %32, %35, %23, %27
%39 = phi i32 [ 0, %27 ], [ 0, %23 ], [ -1, %35 ], [ -1, %32 ]
ret i32 %39
}