#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*/ p;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
scalar_t__ write(int, struct led_buf *, int);
int main() {
  int fd, i, j;
  struct led_buf {
    int no;
    int led_no;
  };
  struct led_buf *p;
  if (fd = open("/dev/led", O_RDWR) < 0) {
    perror("fail to open");
    exit(-1);
  }
  printf("\n/dev/led opened fd = %d \n", fd);
  while (1) {
    for (i = 0; i < 4; i++) {
      p->no = 1;
      p->led_no = i;
      if (write(fd, p, sizeof(p)) < 0)
        perror("fail to write");
      else
        printf("led(%d) is ON\n", i);
      sleep(1);
      p->no = 0;
      if (write(fd, p, sizeof(p)) < 0)
        perror("fail to write");
      else
        printf("led(%d) is ON\n", i);
      sleep(1);
    }
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1050 <open@plt>
cmp    $0x0,%rax
setl   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x8(%rbp)
test   $0x1,%al
jne    11a3 <main+0x43>
jmp    11b9 <main+0x59>
lea    0xe5f(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xe53(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x4,-0xc(%rbp)
jge    128f <main+0x12f>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1219 <main+0xb9>
lea    0xe22(%rip),%rdi
call   1060 <perror@plt>
jmp    1228 <main+0xc8>
mov    -0xc(%rbp),%esi
lea    0xe1c(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1268 <main+0x108>
lea    0xdd3(%rip),%rdi
call   1060 <perror@plt>
jmp    1277 <main+0x117>
mov    -0xc(%rbp),%esi
lea    0xdcd(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11cf <main+0x6f>
jmp    11c8 <main+0x68>

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
lea    0xeaf(%rip),%rdi
call   1040 <open@plt>
test   %rax,%rax
jns    1171 <main+0x31>
lea    0xea7(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
shr    $0x3f,%rax
lea    0xe9a(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>

LLVM IR (-O0):
%struct.led_buf = type { i32, i32 }
%struct.led_buf.0 = type opaque
@.str = [9 x i8] c"/dev/led\00"
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"fail to open\00"
@.str.2 = [27 x i8] c"\0A/dev/led opened fd = %d \0A\00"
@.str.3 = [14 x i8] c"fail to write\00"
@.str.4 = [15 x i8] c"led(%d) is ON\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.led_buf*
store i32 0, i32* %1
%6 = load i32, i32* @O_RDWR
%7 = call i64 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i64 %7, 0
%9 = zext i1 %8 to i32
store i32 %9, i32* %2
br i1 %8, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %0
%14 = load i32, i32* %2
%15 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %13, %55
store i32 0, i32* %3
br label %17
17: ; preds = %52, %16
%18 = load i32, i32* %3
%19 = icmp slt i32 %18, 4
br i1 %19, label %20, label %55
20: ; preds = %17
%21 = load %struct.led_buf*, %struct.led_buf** %5
%22 = getelementptr inbounds %struct.led_buf, %struct.led_buf* %21, i32 0, i32 0
store i32 1, i32* %22
%23 = load i32, i32* %3
%24 = load %struct.led_buf*, %struct.led_buf** %5
%25 = getelementptr inbounds %struct.led_buf, %struct.led_buf* %24, i32 0, i32 1
store i32 %23, i32* %25
%26 = load i32, i32* %2
%27 = load %struct.led_buf*, %struct.led_buf** %5
%28 = bitcast %struct.led_buf* %27 to %struct.led_buf.0*
%29 = call i64 @write(i32 %26, %struct.led_buf.0* %28, i32 8)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %20
%32 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %36
33: ; preds = %20
%34 = load i32, i32* %3
%35 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %33, %31
%37 = call i32 @sleep(i32 1)
%38 = load %struct.led_buf*, %struct.led_buf** %5
%39 = getelementptr inbounds %struct.led_buf, %struct.led_buf* %38, i32 0, i32 0
store i32 0, i32* %39
%40 = load i32, i32* %2
%41 = load %struct.led_buf*, %struct.led_buf** %5
%42 = bitcast %struct.led_buf* %41 to %struct.led_buf.0*
%43 = call i64 @write(i32 %40, %struct.led_buf.0* %42, i32 8)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %36
%46 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %50
47: ; preds = %36
%48 = load i32, i32* %3
%49 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %48)
br label %50
50: ; preds = %47, %45
%51 = call i32 @sleep(i32 1)
br label %52
52: ; preds = %50
%53 = load i32, i32* %3
%54 = add nsw i32 %53, 1
store i32 %54, i32* %3
br label %17
55: ; preds = %17
br label %16
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/led\00"
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"fail to open\00"
@.str.2 = [27 x i8] c"\0A/dev/led opened fd = %d \0A\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i64 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %0
%8 = lshr i64 %2, 63
%9 = trunc i64 %8 to i32
%10 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %9)
unreachable
}