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


x86 assembly (clang, -O0):
<test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11b0 <test+0x40>
lea    0xe6e(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1060 <read@plt>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test>:
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11c8 <test+0x58>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %eax,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <write@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1060 <read@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe40(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

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 [48 x i8]
%2 = alloca i32
%3 = load i32, i32* @O_RDWR
%4 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %3)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @exit(i32 -1)
unreachable
10: ; preds = %0
%11 = load i32, i32* %2
%12 = getelementptr inbounds [48 x i8], [48 x i8]* %1, i64 0, i64 0
%13 = call i32 @write(i32 %11, i8* %12, i32 0)
%14 = call i32 @sleep(i32 1)
%15 = load i32, i32* %2
%16 = getelementptr inbounds [48 x i8], [48 x i8]* %1, i64 0, i64 0
%17 = call i32 @read(i32 %15, i8* %16, i32 0)
%18 = load i32, i32* %2
%19 = call i32 @close(i32 %18)
ret i32 0
}

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 %9
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %0
%10 = call i32 @write(i32 %4, i8* %2, i32 0)
%11 = tail call i32 @sleep(i32 1)
%12 = call i32 @read(i32 %4, i8* %2, i32 0)
%13 = tail call i32 @close(i32 %4)
ret i32 0
}