#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 */
typedef long uint8_t;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDWR;
scalar_t__ errno;
scalar_t__ ioctl(int, int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int strtol(char *, char **, int);
scalar_t__ write(int, long *, int);
int main(int argc, char *argv[]) {
  int fd;
  char *end;
  long number;
  uint8_t buffer[2];
  errno = 0;
  number = (argc == 2) ? strtol(argv[1], &end, 10) : -1;
  if (errno != 0 || end == argv[1] || number < 0 || number > 4095) {
    printf("Usage: i2c-write [0-4095]\n");
    return EXIT_FAILURE;
  }
  if ((fd = open("/dev/i2c-0", O_RDWR)) >= 0) {
    if (ioctl(fd, 0x0706, 0x60) >= 0) {
      buffer[0] = number >> 8;
      buffer[1] = number;
      if (write(fd, buffer, 2) > 0)
        return EXIT_SUCCESS;
    }
  }
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e6b(%rip),%rax
movq   $0x0,(%rax)
cmpl   $0x2,-0x8(%rbp)
jne    119c <main+0x4c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,-0x44(%rbp)
jmp    11a9 <main+0x59>
mov    $0xffffffff,%eax
mov    %eax,-0x44(%rbp)
jmp    11a9 <main+0x59>
mov    -0x44(%rbp),%eax
cltq
mov    %rax,-0x28(%rbp)
mov    0x2e1f(%rip),%rax
cmpq   $0x0,(%rax)
jne    11ee <main+0x9e>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    0x8(%rcx),%rax
je     11ee <main+0x9e>
cmpq   $0x0,-0x28(%rbp)
jl     11ee <main+0x9e>
cmpq   $0xfff,-0x28(%rbp)
jle    120b <main+0xbb>
lea    0xe0b(%rip),%rdi
call   1040 <printf@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    129e <main+0x14e>
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
lea    0xe00(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jl     1292 <main+0x142>
mov    -0x14(%rbp),%edi
mov    $0x706,%esi
mov    $0x60,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jl     128d <main+0x13d>
mov    -0x28(%rbp),%rax
sar    $0x8,%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jle    1288 <main+0x138>
mov    0x2d4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    129e <main+0x14e>
jmp    128d <main+0x13d>
jmp    1292 <main+0x142>
mov    0x2d37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e79(%rip),%rbx
movq   $0x0,(%rbx)
cmp    $0x2,%edi
jne    1187 <main+0x37>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1060 <strtol@plt>
cmpq   $0x0,(%rbx)
je     11a5 <main+0x55>
lea    0xe7d(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %eax,%r14d
mov    0x8(%rsp),%rax
cmp    0x8(%rbp),%rax
je     1187 <main+0x37>
test   %r14d,%r14d
js     1187 <main+0x37>
cmp    $0x1000,%r14d
jge    1187 <main+0x37>
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
lea    0xe2f(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1193 <main+0x43>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x706,%esi
mov    $0x60,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
js     1193 <main+0x43>
movslq %r14d,%rax
mov    %rax,%rcx
shr    $0x8,%rcx
mov    %rcx,0x10(%rsp)
mov    %rax,0x18(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1040 <write@plt>
test   %rax,%rax
jle    1193 <main+0x43>
mov    0x2da1(%rip),%rax
jmp    119a <main+0x4a>

LLVM IR (-O0):
@errno = global i64 0
@.str = [27 x i8] c"Usage: i2c-write [0-4095]\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [11 x i8] c"/dev/i2c-0\00"
@O_RDWR = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca [2 x i64]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* @errno
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, 2
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @strtol(i8* %15, i8** %7, i32 10)
br label %18
17: ; preds = %2
br label %18
18: ; preds = %17, %12
%19 = phi i32 [ %16, %12 ], [ -1, %17 ]
%20 = sext i32 %19 to i64
store i64 %20, i64* %8
%21 = load i64, i64* @errno
%22 = icmp ne i64 %21, 0
br i1 %22, label %35, label %23
23: ; preds = %18
%24 = load i8*, i8** %7
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = icmp eq i8* %24, %27
br i1 %28, label %35, label %29
29: ; preds = %23
%30 = load i64, i64* %8
%31 = icmp slt i64 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = load i64, i64* %8
%34 = icmp sgt i64 %33, 4095
br i1 %34, label %35, label %38
35: ; preds = %32, %29, %23, %18
%36 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
store i32 %37, i32* %3
br label %62
38: ; preds = %32
%39 = load i32, i32* @O_RDWR
%40 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %39)
store i32 %40, i32* %6
%41 = icmp sge i32 %40, 0
br i1 %41, label %42, label %60
42: ; preds = %38
%43 = load i32, i32* %6
%44 = call i64 @ioctl(i32 %43, i32 1798, i32 96)
%45 = icmp sge i64 %44, 0
br i1 %45, label %46, label %59
46: ; preds = %42
%47 = load i64, i64* %8
%48 = ashr i64 %47, 8
%49 = getelementptr inbounds [2 x i64], [2 x i64]* %9, i64 0, i64 0
store i64 %48, i64* %49
%50 = load i64, i64* %8
%51 = getelementptr inbounds [2 x i64], [2 x i64]* %9, i64 0, i64 1
store i64 %50, i64* %51
%52 = load i32, i32* %6
%53 = getelementptr inbounds [2 x i64], [2 x i64]* %9, i64 0, i64 0
%54 = call i64 @write(i32 %52, i64* %53, i32 2)
%55 = icmp sgt i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %46
%57 = load i32, i32* @EXIT_SUCCESS
store i32 %57, i32* %3
br label %62
58: ; preds = %46
br label %59
59: ; preds = %58, %42
br label %60
60: ; preds = %59, %38
%61 = load i32, i32* @EXIT_FAILURE
store i32 %61, i32* %3
br label %62
62: ; preds = %60, %56, %35
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@errno = global i64 0
@EXIT_FAILURE = global i32 0
@.str.1 = [11 x i8] c"/dev/i2c-0\00"
@O_RDWR = global i32 0
@EXIT_SUCCESS = global i32 0
@str = [26 x i8] c"Usage: i2c-write [0-4095]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca [2 x i64]
%5 = bitcast i8** %3 to i8*
%6 = bitcast [2 x i64]* %4 to i8*
store i64 0, i64* @errno
%7 = icmp eq i32 %0, 2
br i1 %7, label %8, label %24
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = call i32 @strtol(i8* %10, i8** %3, i32 10)
%12 = load i64, i64* @errno
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %24
14: ; preds = %8
%15 = sext i32 %11 to i64
%16 = load i8*, i8** %3
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = icmp eq i8* %16, %18
%20 = icmp slt i32 %11, 0
%21 = select i1 %19, i1 true, i1 %20
%22 = icmp sgt i32 %11, 4095
%23 = select i1 %21, i1 true, i1 %22
br i1 %23, label %24, label %26
24: ; preds = %2, %14, %8
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %40
26: ; preds = %14
%27 = load i32, i32* @O_RDWR
%28 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = icmp sgt i32 %28, -1
br i1 %29, label %30, label %39
30: ; preds = %26
%31 = tail call i64 @ioctl(i32 %28, i32 1798, i32 96)
%32 = icmp sgt i64 %31, -1
br i1 %32, label %33, label %39
33: ; preds = %30
%34 = lshr i64 %15, 8
%35 = getelementptr inbounds [2 x i64], [2 x i64]* %4, i64 0, i64 0
store i64 %34, i64* %35
%36 = getelementptr inbounds [2 x i64], [2 x i64]* %4, i64 0, i64 1
store i64 %15, i64* %36
%37 = call i64 @write(i32 %28, i64* %35, i32 2)
%38 = icmp sgt i64 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %30, %33, %26
br label %40
40: ; preds = %33, %39, %24
%41 = phi i32* [ @EXIT_FAILURE, %24 ], [ @EXIT_FAILURE, %39 ], [ @EXIT_SUCCESS, %33 ]
%42 = load i32, i32* %41
ret i32 %42
}