#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 open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int fd;
  unsigned char val = 0x01;
  unsigned char out;
  char buf[4];
  fd = open("/dev/pcf8574_sample", O_RDWR);
  if (fd < 0) {
    printf("open error\n");
    return -1;
  }
  while (val) {
    out = ~val;
    sprintf(buf, "%X", out);
    printf("0x%s\n", buf);
    write(fd, buf, 8);
    sleep(2);
    val = val << 1;
  }
  printf("0xFF\n");
  write(fd, "FF", 8);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x1,-0x15(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
lea    0xe76(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b6 <main+0x56>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <main+0xf0>
jmp    11bb <main+0x5b>
cmpb   $0x0,-0x15(%rbp)
je     121f <main+0xbf>
movzbl -0x15(%rbp),%eax
xor    $0xffffffff,%eax
mov    %al,-0x16(%rbp)
lea    -0x1a(%rbp),%rdi
lea    0xe46(%rip),%rsi
movzbl -0x16(%rbp),%edx
call   1070 <sprintf@plt>
lea    -0x1a(%rbp),%rsi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
movzbl -0x15(%rbp),%eax
shl    $0x1,%eax
mov    %al,-0x15(%rbp)
jmp    11bb <main+0x5b>
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    0xdf8(%rip),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
lea    0xe74(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1389 <main+0x219>
mov    %eax,%ebx
lea    0xe72(%rip),%r15
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0xfe,%edx
call   1080 <sprintf@plt>
lea    0xe59(%rip),%r12
xor    %r14d,%r14d
mov    %r12,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0xfd,%edx
call   1080 <sprintf@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0xfb,%edx
call   1080 <sprintf@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0xf7,%edx
call   1080 <sprintf@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0xef,%edx
call   1080 <sprintf@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0xdf,%edx
call   1080 <sprintf@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0xbf,%edx
call   1080 <sprintf@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0x7f,%edx
call   1080 <sprintf@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
lea    0xcb5(%rip),%rdi
call   1030 <puts@plt>
lea    0xcab(%rip),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    139b <main+0x22b>
lea    0xc92(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"/dev/pcf8574_sample\00"
@O_RDWR = global i32 0
@.str.1 = [12 x i8] c"open error\0A\00"
@.str.2 = [3 x i8] c"%X\00"
@.str.3 = [6 x i8] c"0x%s\0A\00"
@.str.4 = [6 x i8] c"0xFF\0A\00"
@.str.5 = [3 x i8] c"FF\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
%9 = alloca [4 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8 1, i8* %7
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %44
16: ; preds = %2
br label %17
17: ; preds = %20, %16
%18 = load i8, i8* %7
%19 = icmp ne i8 %18, 0
br i1 %19, label %20, label %38
20: ; preds = %17
%21 = load i8, i8* %7
%22 = zext i8 %21 to i32
%23 = xor i32 %22, -1
%24 = trunc i32 %23 to i8
store i8 %24, i8* %8
%25 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%26 = load i8, i8* %8
%27 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %26)
%28 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %28)
%30 = load i32, i32* %6
%31 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%32 = call i32 @write(i32 %30, i8* %31, i32 8)
%33 = call i32 @sleep(i32 2)
%34 = load i8, i8* %7
%35 = zext i8 %34 to i32
%36 = shl i32 %35, 1
%37 = trunc i32 %36 to i8
store i8 %37, i8* %7
br label %17
38: ; preds = %17
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%40 = load i32, i32* %6
%41 = call i32 @write(i32 %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 8)
%42 = load i32, i32* %6
%43 = call i32 @close(i32 %42)
store i32 0, i32* %3
br label %44
44: ; preds = %38, %14
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@.str = [20 x i8] c"/dev/pcf8574_sample\00"
@O_RDWR = global i32 0
@.str.2 = [3 x i8] c"%X\00"
@.str.3 = [6 x i8] c"0x%s\0A\00"
@.str.5 = [3 x i8] c"FF\00"
@str = [5 x i8] c"0xFF\00"
@str.6 = [11 x i8] c"open error\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [4 x i8]
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %44, label %8
8: ; preds = %2
%9 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8 zeroext -2)
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%11 = call i32 @write(i32 %6, i8* %4, i32 8)
%12 = call i32 @sleep(i32 2)
%13 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8 zeroext -3)
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%15 = call i32 @write(i32 %6, i8* %4, i32 8)
%16 = call i32 @sleep(i32 2)
%17 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8 zeroext -5)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%19 = call i32 @write(i32 %6, i8* %4, i32 8)
%20 = call i32 @sleep(i32 2)
%21 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8 zeroext -9)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%23 = call i32 @write(i32 %6, i8* %4, i32 8)
%24 = call i32 @sleep(i32 2)
%25 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8 zeroext -17)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%27 = call i32 @write(i32 %6, i8* %4, i32 8)
%28 = call i32 @sleep(i32 2)
%29 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8 zeroext -33)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%31 = call i32 @write(i32 %6, i8* %4, i32 8)
%32 = call i32 @sleep(i32 2)
%33 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8 zeroext -65)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%35 = call i32 @write(i32 %6, i8* %4, i32 8)
%36 = call i32 @sleep(i32 2)
%37 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8 zeroext 127)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%39 = call i32 @write(i32 %6, i8* %4, i32 8)
%40 = call i32 @sleep(i32 2)
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%42 = call i32 @write(i32 %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 8)
%43 = call i32 @close(i32 %6)
br label %46
44: ; preds = %2
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.6, i64 0, i64 0))
br label %46
46: ; preds = %8, %44
%47 = phi i32 [ -1, %44 ], [ 0, %8 ]
ret i32 %47
}