#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int write(int, char *, int /*<<< orphan*/);
int test() {
  char buf[48];
  int fd;
  int err = 0;
  fd = open("/dev/sdma_test", O_RDWR);
  if (fd < 0) {
    printf("Failed open\n");
    return 1;
  }
  if (err = write(fd, buf, 0)) {
    close(fd);
    return err;
  }
  sleep(1);
  if (err = read(fd, buf, 0)) {
    close(fd);
    return err;
  }
  close(fd);
  return err;
}


x86 assembly (clang, -O0):
<test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x48(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    11a9 <test+0x49>
lea    0xe77(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    121b <test+0xbb>
mov    -0x44(%rbp),%edi
lea    -0x40(%rbp),%rsi
xor    %edx,%edx
call   1030 <write@plt>
mov    %eax,-0x48(%rbp)
cmp    $0x0,%eax
je     11d6 <test+0x76>
mov    -0x44(%rbp),%edi
call   1050 <close@plt>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121b <test+0xbb>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0x44(%rbp),%edi
lea    -0x40(%rbp),%rsi
xor    %edx,%edx
call   1060 <read@plt>
mov    %eax,-0x48(%rbp)
cmp    $0x0,%eax
je     120d <test+0xad>
mov    -0x44(%rbp),%edi
call   1050 <close@plt>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121b <test+0xbb>
mov    -0x44(%rbp),%edi
call   1050 <close@plt>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11b2 <test+0x52>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1040 <write@plt>
test   %eax,%eax
jne    11a7 <test+0x47>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1060 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11c3 <test+0x63>
lea    0xe56(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/dev/sdma_test\00"
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"Failed open\0A\00"
define i32 @test() {
%1 = alloca i32
%2 = alloca [48 x i8]
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %4
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %34
11: ; preds = %0
%12 = load i32, i32* %3
%13 = getelementptr inbounds [48 x i8], [48 x i8]* %2, i64 0, i64 0
%14 = call i32 @write(i32 %12, i8* %13, i32 0)
store i32 %14, i32* %4
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = load i32, i32* %3
%18 = call i32 @close(i32 %17)
%19 = load i32, i32* %4
store i32 %19, i32* %1
br label %34
20: ; preds = %11
%21 = call i32 @sleep(i32 1)
%22 = load i32, i32* %3
%23 = getelementptr inbounds [48 x i8], [48 x i8]* %2, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 0)
store i32 %24, i32* %4
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = load i32, i32* %3
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %4
store i32 %29, i32* %1
br label %34
30: ; preds = %20
%31 = load i32, i32* %3
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %4
store i32 %33, i32* %1
br label %34
34: ; preds = %30, %26, %16, %9
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
@.str = [15 x i8] c"/dev/sdma_test\00"
@O_RDWR = global i32 0
@str = [12 x i8] c"Failed open\00"
define i32 @test() {
%1 = alloca [48 x i8]
%2 = getelementptr inbounds [48 x i8], [48 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %17
8: ; preds = %0
%9 = call i32 @write(i32 %4, i8* %2, i32 0)
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 @close(i32 %4)
br label %17
13: ; preds = %8
%14 = tail call i32 @sleep(i32 1)
%15 = call i32 @read(i32 %4, i8* %2, i32 0)
%16 = tail call i32 @close(i32 %4)
br label %17
17: ; preds = %13, %11, %6
%18 = phi i32 [ 1, %6 ], [ %9, %11 ], [ %15, %13 ]
ret i32 %18
}