#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 O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ fsync(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int open_test() {
  int fd;
  fd = open("/dev/ttyACM0", O_RDWR | O_NONBLOCK);
  printf("fd: %d\n", fd);
  if (fd < 0) {
    perror("damnit");
    return 1;
  }
  printf("wrote %d\n", write(fd, "~out9=1~", strlen("~out9=1~")));
  perror("shit");
  fsync(fd);
  sleep(1);
  printf("wrote %d\n", write(fd, "~out9=0~", strlen("~out9=0~")));
  fsync(fd);
  return 0;
}


x86 assembly (clang, -O0):
<open_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
lea    0xe6f(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe6a(%rip),%rdi
call   1050 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jge    11ca <open_test+0x5a>
lea    0xe5c(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1259 <open_test+0xe9>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
lea    0xe4f(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x10(%rbp),%edi
mov    %eax,%edx
lea    0xe3e(%rip),%rsi
call   1030 <write@plt>
mov    %eax,%esi
lea    0xe26(%rip),%rdi
call   1050 <printf@plt>
lea    0xe2d(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%edi
call   1060 <fsync@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
lea    0xe0e(%rip),%rdi
call   1040 <strlen@plt>
mov    -0xc(%rbp),%edi
mov    %eax,%edx
lea    0xdfd(%rip),%rsi
call   1030 <write@plt>
mov    %eax,%esi
lea    0xdd7(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <fsync@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_test>:
push   %r15
push   %r14
push   %rbx
mov    0x2e6c(%rip),%rax
mov    0x2e5d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe72(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
lea    0xe71(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
test   %ebx,%ebx
jns    11ba <open_test+0x4a>
lea    0xe67(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    1231 <open_test+0xc1>
lea    0xe65(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0xe40(%rip),%r14
mov    %r14,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
lea    0xe42(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1060 <fsync@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
lea    0xe2a(%rip),%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <fsync@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/ttyACM0\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [8 x i8] c"fd: %d\0A\00"
@.str.2 = [7 x i8] c"damnit\00"
@.str.3 = [10 x i8] c"wrote %d\0A\00"
@.str.4 = [9 x i8] c"~out9=1~\00"
@.str.5 = [5 x i8] c"shit\00"
@.str.6 = [9 x i8] c"~out9=0~\00"
define i32 @open_test() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NONBLOCK
%5 = or i32 %3, %4
%6 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %28
13: ; preds = %0
%14 = load i32, i32* %2
%15 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%16 = call i32 @write(i32 %14, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %15)
%17 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%19 = load i32, i32* %2
%20 = call i32 @fsync(i32 %19)
%21 = call i32 @sleep(i32 1)
%22 = load i32, i32* %2
%23 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%24 = call i32 @write(i32 %22, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i32 %23)
%25 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %2
%27 = call i32 @fsync(i32 %26)
store i32 0, i32* %1
br label %28
28: ; preds = %13, %11
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/ttyACM0\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [8 x i8] c"fd: %d\0A\00"
@.str.2 = [7 x i8] c"damnit\00"
@.str.3 = [10 x i8] c"wrote %d\0A\00"
@.str.4 = [9 x i8] c"~out9=1~\00"
@.str.5 = [5 x i8] c"shit\00"
@.str.6 = [9 x i8] c"~out9=0~\00"
define i32 @open_test() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_NONBLOCK
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %4, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %20
9: ; preds = %0
%10 = tail call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%11 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %10)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%14 = tail call i32 @fsync(i32 %4)
%15 = tail call i32 @sleep(i32 1)
%16 = tail call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%17 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i32 %16)
%18 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%19 = tail call i32 @fsync(i32 %4)
br label %20
20: ; preds = %9, %7
%21 = phi i32 [ 1, %7 ], [ 0, %9 ]
ret i32 %21
}