#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 int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
int main() {
  int fd, num;
  fd_set rfds;
  char buf[20];
  fd = open("/sys/devices/platform/tve.0/headphone", O_RDWR, S_IRUSR | S_IWUSR);
  if (fd != -1) {
    while (1) {
      FD_ZERO(&rfds);
      FD_SET(fd, &rfds);
      select(fd + 1, &rfds, NULL, NULL, NULL);
      if (FD_ISSET(fd, &rfds)) {
        lseek(fd, 0, SEEK_SET);
        memset(buf, 0, 20);
        num = read(fd, buf, 20);
        if (num != 0) {
          printf("\nThe string read len is %d\n", num);
          printf("The string read is %s\n", buf);
          if (strcmp(buf, "tve power off\n") == 0)
            printf("Confirm: tve power off!\n");
          if (strcmp(buf, "cvbs\n") == 0)
            printf("Confirm: cvbs cable connected!\n");
          if (strcmp(buf, "component\n") == 0)
            printf("Confirm: component cable connected!\n");
          if (strcmp(buf, "svideo\n") == 0)
            printf("Confirm: svideo cable connected!\n");
          if (strcmp(buf, "headset\n") == 0)
            printf("Confirm: headset connected!\n");
          if (strcmp(buf, "none\n") == 0)
            printf("Confirm: none connect!\n");
        }
      }
    }
  } else {
    printf("sys file open failure\n");
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
mov    0x2e18(%rip),%rax
or     (%rax),%edx
lea    0xe2f(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     1390 <main+0x1f0>
jmp    11e8 <main+0x48>
lea    -0x10(%rbp),%rdi
call   1090 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   10c0 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10a0 <select@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     138b <main+0x1eb>
mov    -0x8(%rbp),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x14,%edx
call   1070 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1386 <main+0x1e6>
mov    -0xc(%rbp),%esi
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x30(%rbp),%rsi
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x30(%rbp),%rdi
lea    0xdbd(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    12b9 <main+0x119>
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x30(%rbp),%rdi
lea    0xdbd(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    12e1 <main+0x141>
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x30(%rbp),%rdi
lea    0xdbb(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1309 <main+0x169>
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x30(%rbp),%rdi
lea    0xdc3(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1331 <main+0x191>
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x30(%rbp),%rdi
lea    0xdc5(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1359 <main+0x1b9>
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x30(%rbp),%rdi
lea    0xdc3(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1381 <main+0x1e1>
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1386 <main+0x1e6>
jmp    138b <main+0x1eb>
jmp    11e8 <main+0x48>
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    0x2dfa(%rip),%rax
mov    0x2e0b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe20(%rip),%rdi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     1358 <main+0x1a8>
mov    %eax,%ebx
mov    %eax,%r14d
add    $0x1,%r14d
lea    0xc(%rsp),%r15
mov    0x2dcd(%rip),%r12
lea    0x10(%rsp),%rbp
lea    0xe7b(%rip),%r13
nop
mov    %r15,%rdi
call   10a0 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   10d0 <FD_SET@plt>
mov    %r14d,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10b0 <select@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1210 <main+0x60>
mov    (%r12),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1070 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x14,%edx
call   1080 <read@plt>
test   %eax,%eax
je     1210 <main+0x60>
lea    0xdad(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdb9(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
lea    0xdbc(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    12b3 <main+0x103>
lea    0xe8f(%rip),%rdi
call   1030 <puts@plt>
mov    %rbp,%rdi
lea    0xdab(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    12d3 <main+0x123>
lea    0xe50(%rip),%rdi
call   1030 <puts@plt>
mov    %rbp,%rdi
lea    0xd91(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    12f3 <main+0x143>
lea    0xe0c(%rip),%rdi
call   1030 <puts@plt>
mov    %rbp,%rdi
lea    0xd7c(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1313 <main+0x163>
lea    0xdcb(%rip),%rdi
call   1030 <puts@plt>
mov    %rbp,%rdi
lea    0xd64(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1333 <main+0x183>
lea    0xd8f(%rip),%rdi
call   1030 <puts@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1210 <main+0x60>
lea    0xd58(%rip),%rdi
call   1030 <puts@plt>
jmp    1210 <main+0x60>
lea    0xd31(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"/sys/devices/platform/tve.0/headphone\00"
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [28 x i8] c"\0AThe string read len is %d\0A\00"
@.str.2 = [23 x i8] c"The string read is %s\0A\00"
@.str.3 = [15 x i8] c"tve power off\0A\00"
@.str.4 = [25 x i8] c"Confirm: tve power off!\0A\00"
@.str.5 = [6 x i8] c"cvbs\0A\00"
@.str.6 = [32 x i8] c"Confirm: cvbs cable connected!\0A\00"
@.str.7 = [11 x i8] c"component\0A\00"
@.str.8 = [37 x i8] c"Confirm: component cable connected!\0A\00"
@.str.9 = [8 x i8] c"svideo\0A\00"
@.str.10 = [34 x i8] c"Confirm: svideo cable connected!\0A\00"
@.str.11 = [9 x i8] c"headset\0A\00"
@.str.12 = [29 x i8] c"Confirm: headset connected!\0A\00"
@.str.13 = [6 x i8] c"none\0A\00"
@.str.14 = [24 x i8] c"Confirm: none connect!\0A\00"
@.str.15 = [23 x i8] c"sys file open failure\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [20 x i8]
store i32 0, i32* %1
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @S_IRUSR
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %9)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp ne i32 %11, -1
br i1 %12, label %13, label %78
13: ; preds = %0
br label %14
14: ; preds = %13, %77
%15 = call i32 @FD_ZERO(i32* %4)
%16 = load i32, i32* %2
%17 = call i32 @FD_SET(i32 %16, i32* %4)
%18 = load i32, i32* %2
%19 = add nsw i32 %18, 1
%20 = call i32 @select(i32 %19, i32* %4, i32* null, i32* null, i32* null)
%21 = load i32, i32* %2
%22 = call i64 @FD_ISSET(i32 %21, i32* %4)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %77
24: ; preds = %14
%25 = load i32, i32* %2
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @lseek(i32 %25, i32 0, i32 %26)
%28 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%29 = call i32 @memset(i8* %28, i32 0, i32 20)
%30 = load i32, i32* %2
%31 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%32 = call i32 @read(i32 %30, i8* %31, i32 20)
store i32 %32, i32* %3
%33 = load i32, i32* %3
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %76
35: ; preds = %24
%36 = load i32, i32* %3
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %36)
%38 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%41 = call i64 @strcmp(i8* %40, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %35
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %43, %35
%46 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%47 = call i64 @strcmp(i8* %46, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
br label %51
51: ; preds = %49, %45
%52 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%53 = call i64 @strcmp(i8* %52, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %51
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.8, i64 0, i64 0))
br label %57
57: ; preds = %55, %51
%58 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%59 = call i64 @strcmp(i8* %58, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %57
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.10, i64 0, i64 0))
br label %63
63: ; preds = %61, %57
%64 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%65 = call i64 @strcmp(i8* %64, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.11, i64 0, i64 0))
%66 = icmp eq i64 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %63
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.12, i64 0, i64 0))
br label %69
69: ; preds = %67, %63
%70 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%71 = call i64 @strcmp(i8* %70, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.13, i64 0, i64 0))
%72 = icmp eq i64 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %69
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.14, i64 0, i64 0))
br label %75
75: ; preds = %73, %69
br label %76
76: ; preds = %75, %24
br label %77
77: ; preds = %76, %14
br label %14
78: ; preds = %0
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.15, i64 0, i64 0))
ret i32 -1
}

LLVM IR (-O2):
@.str = [38 x i8] c"/sys/devices/platform/tve.0/headphone\00"
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [28 x i8] c"\0AThe string read len is %d\0A\00"
@.str.2 = [23 x i8] c"The string read is %s\0A\00"
@.str.3 = [15 x i8] c"tve power off\0A\00"
@.str.5 = [6 x i8] c"cvbs\0A\00"
@.str.7 = [11 x i8] c"component\0A\00"
@.str.9 = [8 x i8] c"svideo\0A\00"
@.str.11 = [9 x i8] c"headset\0A\00"
@.str.13 = [6 x i8] c"none\0A\00"
@str = [22 x i8] c"sys file open failure\00"
@str.16 = [23 x i8] c"Confirm: none connect!\00"
@str.17 = [28 x i8] c"Confirm: headset connected!\00"
@str.18 = [33 x i8] c"Confirm: svideo cable connected!\00"
@str.19 = [36 x i8] c"Confirm: component cable connected!\00"
@str.20 = [31 x i8] c"Confirm: cvbs cable connected!\00"
@str.21 = [24 x i8] c"Confirm: tve power off!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [20 x i8]
%3 = bitcast i32* %1 to i8*
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %58, label %11
11: ; preds = %0
%12 = add nuw nsw i32 %9, 1
br label %13
13: ; preds = %25, %11
%14 = call i32 @FD_ZERO(i32* %1)
%15 = call i32 @FD_SET(i32 %9, i32* %1)
%16 = call i32 @select(i32 %12, i32* %1, i32* null, i32* null, i32* null)
%17 = call i64 @FD_ISSET(i32 %9, i32* %1)
%18 = icmp eq i64 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %13
%20 = load i32, i32* @SEEK_SET
%21 = call i32 @lseek(i32 %9, i32 0, i32 %20)
%22 = call i32 @memset(i8* %4, i32 0, i32 20)
%23 = call i32 @read(i32 %9, i8* %4, i32 20)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %19, %56, %53, %13
br label %13
26: ; preds = %19
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%29 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.21, i64 0, i64 0))
br label %33
33: ; preds = %31, %26
%34 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.20, i64 0, i64 0))
br label %38
38: ; preds = %36, %33
%39 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.19, i64 0, i64 0))
br label %43
43: ; preds = %41, %38
%44 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.18, i64 0, i64 0))
br label %48
48: ; preds = %46, %43
%49 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.11, i64 0, i64 0))
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.17, i64 0, i64 0))
br label %53
53: ; preds = %51, %48
%54 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.13, i64 0, i64 0))
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %25
56: ; preds = %53
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.16, i64 0, i64 0))
br label %25
58: ; preds = %0
%59 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
ret i32 -1
}