#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*/ I2C_SLAVE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *, ...);
int getopt(int, char **, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, long);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ write_as_csv(char *, int);
int /*<<< orphan*/ write_as_text(char *, int);
int main(int argc, char **argv) {
  int opt;
  int csv_mode = 0;
  int dev;
  char message[1024];
  int n;
  while ((opt = getopt(argc, argv, "hc")) != -1) {
    switch (opt) {
    case 'c':
      csv_mode = 1;
      break;
    default:
      printf("Usage: %s [-c]\n", argv[0]);
      printf("\t-c\tWrite output as CSV format\n");
      return 1;
    }
  }
  if ((dev = open("/dev/i2c-0", O_RDWR)) < 0) {
    fprintf(stderr, "%s: failed to open %s: %s\n", argv[0], "/dev/i2c-0",
            strerror(errno));
    return 1;
  }
  if (ioctl(dev, I2C_SLAVE, (long)0x41) < 0) {
    fprintf(stderr, "%s: ioctl(I2C_SLAVE) failed: %s\n", argv[0],
            strerror(errno));
    close(dev);
    return 1;
  }
  if ((n = read(dev, message, 1024)) < 0) {
    fprintf(stderr, "%s: message read failed: %s\n", argv[0], strerror(errno));
    close(dev);
    return 1;
  }
  if (csv_mode)
    write_as_csv(message, n);
  else
    write_as_text(message, n);
  close(dev);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe32(%rip),%rdx
call   10b0 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     1230 <main+0x90>
mov    -0x14(%rbp),%eax
sub    $0x63,%eax
jne    11fc <main+0x5c>
jmp    11f0 <main+0x50>
movl   $0x1,-0x18(%rbp)
jmp    122b <main+0x8b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ec <main+0x24c>
jmp    11c0 <main+0x20>
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
lea    0xdf3(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    12ad <main+0x10d>
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x434(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x430(%rbp)
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x434(%rbp),%edi
mov    -0x430(%rbp),%rdx
mov    %rax,%r8
lea    0xdab(%rip),%rsi
lea    0xd99(%rip),%rcx
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ec <main+0x24c>
mov    -0x1c(%rbp),%edi
mov    0x2d29(%rip),%rax
mov    (%rax),%esi
mov    $0x41,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    132a <main+0x18a>
mov    0x2cfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x444(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x440(%rbp)
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x444(%rbp),%edi
mov    -0x440(%rbp),%rdx
mov    %rax,%rcx
lea    0xd4a(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ec <main+0x24c>
mov    -0x1c(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x424(%rbp)
cmp    $0x0,%eax
jge    13aa <main+0x20a>
mov    0x2c7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x454(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x450(%rbp)
mov    0x2c57(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x454(%rbp),%edi
mov    -0x450(%rbp),%rdx
mov    %rax,%rcx
lea    0xceb(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ec <main+0x24c>
cmpl   $0x0,-0x18(%rbp)
je     13cb <main+0x22b>
lea    -0x420(%rbp),%rdi
mov    -0x424(%rbp),%esi
call   1090 <write_as_csv@plt>
jmp    13dd <main+0x23d>
lea    -0x420(%rbp),%rdi
mov    -0x424(%rbp),%esi
call   1030 <write_as_text@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    $0x1,%cl
lea    0xe35(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %ecx,%ebx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10c0 <getopt@plt>
xor    %ecx,%ecx
cmp    $0x63,%eax
je     11d0 <main+0x20>
cmp    $0xffffffff,%eax
jne    1253 <main+0xa3>
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
lea    0xe18(%rip),%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     127a <main+0xca>
mov    %eax,%ebp
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    $0x41,%edx
mov    %ebp,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
js     12b8 <main+0x108>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
js     12db <main+0x12b>
mov    %rsp,%rdi
mov    %eax,%esi
test   $0x1,%bl
je     1319 <main+0x169>
call   1040 <write_as_text@plt>
jmp    131e <main+0x16e>
mov    (%r14),%rsi
lea    0xda6(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe0c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    1327 <main+0x177>
mov    0x2d4f(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%rbp
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd83(%rip),%rsi
lea    0xd71(%rip),%rcx
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%r8
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%eax
jmp    1327 <main+0x177>
mov    0x2d11(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%r14
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd60(%rip),%rsi
jmp    12fc <main+0x14c>
mov    0x2cee(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%r14
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd5e(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    $0x1,%eax
jmp    1327 <main+0x177>
call   10a0 <write_as_csv@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"hc\00"
@.str.1 = [16 x i8] c"Usage: %s [-c]\0A\00"
@.str.2 = [32 x i8] c"\09-c\09Write output as CSV format\0A\00"
@.str.3 = [11 x i8] c"/dev/i2c-0\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.4 = [27 x i8] c"%s: failed to open %s: %s\0A\00"
@errno = global i32 0
@I2C_SLAVE = global i32 0
@.str.5 = [33 x i8] c"%s: ioctl(I2C_SLAVE) failed: %s\0A\00"
@.str.6 = [29 x i8] c"%s: message read failed: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
br label %11
11: ; preds = %25, %2
%12 = load i32, i32* %4
%13 = load i8**, i8*** %5
%14 = call i32 @getopt(i32 %12, i8** %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 %14, i32* %6
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %26
16: ; preds = %11
%17 = load i32, i32* %6
switch i32 %17, label %19 [
i32 99, label %18
]
18: ; preds = %16
store i32 1, i32* %7
br label %25
19: ; preds = %16
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %82
25: ; preds = %18
br label %11
26: ; preds = %11
%27 = load i32, i32* @O_RDWR
%28 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %27)
store i32 %28, i32* %8
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %38
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 0
%34 = load i8*, i8** %33
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %34, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %36)
store i32 1, i32* %3
br label %82
38: ; preds = %26
%39 = load i32, i32* %8
%40 = load i32, i32* @I2C_SLAVE
%41 = call i64 @ioctl(i32 %39, i32 %40, i64 65)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %53
43: ; preds = %38
%44 = load i32, i32* @stderr
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 0
%47 = load i8*, i8** %46
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %47, i8* %49)
%51 = load i32, i32* %8
%52 = call i32 @close(i32 %51)
store i32 1, i32* %3
br label %82
53: ; preds = %38
%54 = load i32, i32* %8
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%56 = call i32 @read(i32 %54, i8* %55, i32 1024)
store i32 %56, i32* %10
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %68
58: ; preds = %53
%59 = load i32, i32* @stderr
%60 = load i8**, i8*** %5
%61 = getelementptr inbounds i8*, i8** %60, i64 0
%62 = load i8*, i8** %61
%63 = load i32, i32* @errno
%64 = call i8* @strerror(i32 %63)
%65 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* %62, i8* %64)
%66 = load i32, i32* %8
%67 = call i32 @close(i32 %66)
store i32 1, i32* %3
br label %82
68: ; preds = %53
%69 = load i32, i32* %7
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %75
71: ; preds = %68
%72 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%73 = load i32, i32* %10
%74 = call i32 @write_as_csv(i8* %72, i32 %73)
br label %79
75: ; preds = %68
%76 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%77 = load i32, i32* %10
%78 = call i32 @write_as_text(i8* %76, i32 %77)
br label %79
79: ; preds = %75, %71
%80 = load i32, i32* %8
%81 = call i32 @close(i32 %80)
store i32 0, i32* %3
br label %82
82: ; preds = %79, %58, %43, %30, %19
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
@.str = [3 x i8] c"hc\00"
@.str.1 = [16 x i8] c"Usage: %s [-c]\0A\00"
@.str.3 = [11 x i8] c"/dev/i2c-0\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.4 = [27 x i8] c"%s: failed to open %s: %s\0A\00"
@errno = global i32 0
@I2C_SLAVE = global i32 0
@.str.5 = [33 x i8] c"%s: ioctl(I2C_SLAVE) failed: %s\0A\00"
@.str.6 = [29 x i8] c"%s: message read failed: %s\0A\00"
@str = [31 x i8] c"\09-c\09Write output as CSV format\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %5, %2
%6 = phi i1 [ true, %2 ], [ false, %5 ]
%7 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
switch i32 %7, label %8 [
i32 -1, label %12
i32 99, label %5
]
8: ; preds = %5
%9 = load i8*, i8** %1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %50
12: ; preds = %5
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %1
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %18, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %20)
br label %50
22: ; preds = %12
%23 = load i32, i32* @I2C_SLAVE
%24 = tail call i64 @ioctl(i32 %14, i32 %23, i64 65)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %1
%29 = load i32, i32* @errno
%30 = tail call i8* @strerror(i32 %29)
%31 = tail call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %28, i8* %30)
%32 = tail call i32 @close(i32 %14)
br label %50
33: ; preds = %22
%34 = call i32 @read(i32 %14, i8* %4, i32 1024)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %1
%39 = load i32, i32* @errno
%40 = tail call i8* @strerror(i32 %39)
%41 = tail call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* %38, i8* %40)
%42 = tail call i32 @close(i32 %14)
br label %50
43: ; preds = %33
br i1 %6, label %46, label %44
44: ; preds = %43
%45 = call i32 @write_as_csv(i8* %4, i32 %34)
br label %48
46: ; preds = %43
%47 = call i32 @write_as_text(i8* %4, i32 %34)
br label %48
48: ; preds = %46, %44
%49 = call i32 @close(i32 %14)
br label %50
50: ; preds = %48, %36, %26, %16, %8
%51 = phi i32 [ 1, %8 ], [ 1, %16 ], [ 1, %26 ], [ 1, %36 ], [ 0, %48 ]
ret i32 %51
}