#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*/ value;
struct timespec {
  int tv_nsec;
  scalar_t__ tv_sec;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/,
                                   struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct timespec *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
int read(int, int *, size_t);
int write(int, int *, size_t);
int work_mode(int fd, unsigned char mode) {
  struct timespec t;
  memset(&t, 0, sizeof(struct timespec));
  t.tv_sec = 0;
  t.tv_nsec = 500000000;
  int value = 0;
  size_t len = sizeof(value);
  ssize_t n = 0;
  if (mode == 0x0001) {
    for (;;) {
      n = write(fd, &value, len);
      if (n == -1)
        perror("write");
      clock_nanosleep(CLOCK_MONOTONIC, 0, &t, NULL);
      value = 1;
      n = write(fd, &value, len);
      if (n == -1)
        perror("write");
      clock_nanosleep(CLOCK_MONOTONIC, 0, &t, NULL);
      value = 0;
    }
    return 0;
  }
  if (mode == 0x0002) {
    for (;;) {
      n = read(fd, &value, len);
      if (n == -1)
        perror("read");
      printf("read %d from %s\n", value, "/dev/pcf8574");
      clock_nanosleep(CLOCK_MONOTONIC, 0, &t, NULL);
    }
    return 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<work_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
movq   $0x0,-0x10(%rbp)
movl   $0x1dcd6500,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x4,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movzbl -0x5(%rbp),%eax
cmp    $0x1,%eax
jne    124d <work_mode+0xed>
jmp    11b8 <work_mode+0x58>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rdx
lea    -0x1c(%rbp),%rsi
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11e1 <work_mode+0x81>
lea    0xe24(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1080 <clock_nanosleep@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rdx
lea    -0x1c(%rbp),%rsi
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1229 <work_mode+0xc9>
lea    0xddc(%rip),%rdi
call   1070 <perror@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1080 <clock_nanosleep@plt>
movl   $0x0,-0x1c(%rbp)
jmp    11b8 <work_mode+0x58>
movzbl -0x5(%rbp),%eax
cmp    $0x2,%eax
jne    12bb <work_mode+0x15b>
jmp    125f <work_mode+0xff>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rdx
lea    -0x1c(%rbp),%rsi
call   1060 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1288 <work_mode+0x128>
lea    0xd83(%rip),%rdi
call   1070 <perror@plt>
mov    -0x1c(%rbp),%esi
lea    0xd79(%rip),%rdi
lea    0xd83(%rip),%rdx
call   1040 <printf@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1080 <clock_nanosleep@plt>
jmp    125f <work_mode+0xff>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<work_mode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
movq   $0x0,0x10(%rsp)
movl   $0x1dcd6500,0x8(%rsp)
1d
movl   $0x0,0x4(%rsp)
cmp    $0x1,%bpl
je     1210 <work_mode+0xb0>
cmp    $0x2,%bpl
jne    1298 <work_mode+0x138>
lea    0x4(%rsp),%r14
lea    0xe53(%rip),%r12
lea    0xe5d(%rip),%r13
mov    0x2e22(%rip),%r15
lea    0x8(%rsp),%rbp
jmp    11ee <work_mode+0x8e>
nopl   (%rax)
mov    0x4(%rsp),%esi
mov    %r12,%rdi
mov    %r13,%rdx
call   1040 <printf@plt>
mov    (%r15),%edi
xor    %esi,%esi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1080 <clock_nanosleep@plt>
mov    $0x4,%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <read@plt>
cmp    $0xffffffff,%eax
jne    11d0 <work_mode+0x70>
lea    0xdfd(%rip),%rdi
call   1070 <perror@plt>
jmp    11d0 <work_mode+0x70>
lea    0x4(%rsp),%r14
lea    0xde4(%rip),%r15
mov    0x2dc5(%rip),%rbp
lea    0x8(%rsp),%r12
jmp    1247 <work_mode+0xe7>
nopw   0x0(%rax,%rax,1)
mov    0x0(%rbp),%edi
xor    %esi,%esi
mov    %r12,%rdx
xor    %ecx,%ecx
call   1080 <clock_nanosleep@plt>
movl   $0x0,0x4(%rsp)
mov    $0x4,%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1263 <work_mode+0x103>
mov    %r15,%rdi
call   1070 <perror@plt>
mov    0x0(%rbp),%edi
xor    %esi,%esi
mov    %r12,%rdx
xor    %ecx,%ecx
call   1080 <clock_nanosleep@plt>
movl   $0x1,0x4(%rsp)
mov    $0x4,%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1230 <work_mode+0xd0>
mov    %r15,%rdi
call   1070 <perror@plt>
jmp    1230 <work_mode+0xd0>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@.str = [6 x i8] c"write\00"
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [17 x i8] c"read %d from %s\0A\00"
@.str.3 = [13 x i8] c"/dev/pcf8574\00"
define i32 @work_mode(i32 %0, i8 zeroext %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca %struct.timespec
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %3
store i8 %1, i8* %4
%9 = call i32 @memset(%struct.timespec* %5, i32 0, i32 16)
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i64 0, i64* %10
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i32 500000000, i32* %11
store i32 0, i32* %6
store i64 4, i64* %7
store i32 0, i32* %8
%12 = load i8, i8* %4
%13 = zext i8 %12 to i32
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %37
15: ; preds = %2
br label %16
16: ; preds = %34, %15
%17 = load i32, i32* %3
%18 = load i64, i64* %7
%19 = call i32 @write(i32 %17, i32* %6, i64 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %16
%25 = load i32, i32* @CLOCK_MONOTONIC
%26 = call i32 @clock_nanosleep(i32 %25, i32 0, %struct.timespec* %5, i32* null)
store i32 1, i32* %6
%27 = load i32, i32* %3
%28 = load i64, i64* %7
%29 = call i32 @write(i32 %27, i32* %6, i64 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %24
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %34
34: ; preds = %32, %24
%35 = load i32, i32* @CLOCK_MONOTONIC
%36 = call i32 @clock_nanosleep(i32 %35, i32 0, %struct.timespec* %5, i32* null)
store i32 0, i32* %6
br label %16
37: ; preds = %2
%38 = load i8, i8* %4
%39 = zext i8 %38 to i32
%40 = icmp eq i32 %39, 2
br i1 %40, label %41, label %55
41: ; preds = %37
br label %42
42: ; preds = %50, %41
%43 = load i32, i32* %3
%44 = load i64, i64* %7
%45 = call i32 @read(i32 %43, i32* %6, i64 %44)
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %50
50: ; preds = %48, %42
%51 = load i32, i32* %6
%52 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %51, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%53 = load i32, i32* @CLOCK_MONOTONIC
%54 = call i32 @clock_nanosleep(i32 %53, i32 0, %struct.timespec* %5, i32* null)
br label %42
55: ; preds = %37
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@.str = [6 x i8] c"write\00"
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [17 x i8] c"read %d from %s\0A\00"
@.str.3 = [13 x i8] c"/dev/pcf8574\00"
define i32 @work_mode(i32 %0, i8 zeroext %1) {
%3 = alloca %struct.timespec
%4 = alloca i32
%5 = bitcast %struct.timespec* %3 to i8*
%6 = call i32 @memset(%struct.timespec* %3, i32 0, i32 16)
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i64 0, i64* %7
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
store i32 500000000, i32* %8
%9 = bitcast i32* %4 to i8*
store i32 0, i32* %4
switch i8 %1, label %35 [
i8 1, label %10
i8 2, label %25
]
10: ; preds = %2, %22
%11 = call i32 @write(i32 %0, i32* %4, i64 4)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = load i32, i32* @CLOCK_MONOTONIC
%17 = call i32 @clock_nanosleep(i32 %16, i32 0, %struct.timespec* %3, i32* null)
store i32 1, i32* %4
%18 = call i32 @write(i32 %0, i32* %4, i64 4)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = load i32, i32* @CLOCK_MONOTONIC
%24 = call i32 @clock_nanosleep(i32 %23, i32 0, %struct.timespec* %3, i32* null)
store i32 0, i32* %4
br label %10
25: ; preds = %2, %30
%26 = call i32 @read(i32 %0, i32* %4, i64 4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
%31 = load i32, i32* %4
%32 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %31, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* @CLOCK_MONOTONIC
%34 = call i32 @clock_nanosleep(i32 %33, i32 0, %struct.timespec* %3, i32* null)
br label %25
35: ; preds = %2
ret i32 0
}