#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;
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int read(int, int *, size_t);
int /*<<< orphan*/ sleep(int);
int write(int, unsigned int *, int);
int work_mode(int fd, unsigned int pin) {
  int value = 0;
  size_t len = sizeof(value);
  ssize_t n = 0;
  if (pin == 0)
    pin = 244;
  printf("Use pin %d\n", pin);
  n = write(fd, &pin, sizeof(unsigned int));
  if (n == -1) {
    perror("write");
    return -1;
  } else {
    printf("Wrote %d bytes\n", (int)n);
  }
  sleep(1);
  for (;;) {
    n = read(fd, &value, len);
    if (n == -1)
      perror("read");
    printf("read %d from %s\n", value, "/dev/gpio_irq_driver");
  }
  return 0;
}


x86 assembly (clang, -O0):
<work_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movq   $0x4,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1185 <work_mode+0x35>
movl   $0xf4,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11cb <work_mode+0x7b>
lea    0xe51(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x1c(%rbp),%esi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rdx
lea    -0xc(%rbp),%rsi
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    120f <work_mode+0xbf>
lea    0xe18(%rip),%rdi
call   1060 <perror@plt>
mov    -0xc(%rbp),%esi
lea    0xe0e(%rip),%rdi
lea    0xe18(%rip),%rdx
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11e6 <work_mode+0x96>

x86 assembly (clang, -O2):
<work_mode>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
movl   $0x0,0x8(%rsp)
test   %esi,%esi
mov    $0xf4,%eax
cmove  %eax,%esi
mov    %esi,0xc(%rsp)
lea    0xe84(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11ff <work_mode+0xaf>
lea    0xe72(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
lea    0x8(%rsp),%r14
lea    0xe63(%rip),%r15
lea    0xe61(%rip),%r12
lea    0xe6b(%rip),%r13
jmp    11e1 <work_mode+0x91>
nop
mov    0x8(%rsp),%esi
mov    %r12,%rdi
mov    %r13,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x4,%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    11d0 <work_mode+0x80>
mov    %r15,%rdi
call   1060 <perror@plt>
jmp    11d0 <work_mode+0x80>
lea    0xe06(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [12 x i8] c"Use pin %d\0A\00"
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [16 x i8] c"Wrote %d bytes\0A\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [17 x i8] c"read %d from %s\0A\00"
@.str.5 = [21 x i8] c"/dev/gpio_irq_driver\00"
define i32 @work_mode(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i64 4, i64* %6
store i32 0, i32* %7
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 244, i32* %4
br label %11
11: ; preds = %10, %2
%12 = load i32, i32* %4
%13 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %3
%15 = call i32 @write(i32 %14, i32* %4, i32 4)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
ret i32 -1
20: ; preds = %11
%21 = load i32, i32* %7
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %20
%24 = call i32 @sleep(i32 1)
br label %25
25: ; preds = %33, %23
%26 = load i32, i32* %3
%27 = load i64, i64* %6
%28 = call i32 @read(i32 %26, i32* %5, i64 %27)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %31, %25
%34 = load i32, i32* %5
%35 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %34, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %25
}

LLVM IR (-O2):
@.str = [12 x i8] c"Use pin %d\0A\00"
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [16 x i8] c"Wrote %d bytes\0A\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [17 x i8] c"read %d from %s\0A\00"
@.str.5 = [21 x i8] c"/dev/gpio_irq_driver\00"
define i32 @work_mode(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%6 = icmp eq i32 %1, 0
%7 = select i1 %6, i32 244, i32 %1
store i32 %7, i32* %3
%8 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 @write(i32 %0, i32* %3, i32 4)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
ret i32 -1
13: ; preds = %2
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%15 = tail call i32 @sleep(i32 1)
br label %16
16: ; preds = %21, %13
%17 = call i32 @read(i32 %0, i32* %4, i64 4)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = load i32, i32* %4
%23 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %22, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %16
}