#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_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ pthread_exit(int *);
int read(int, char *, int);
char *strerror(int /*<<< orphan*/);
void *test_read_zero() {
  int fd, count, size_buffer = 2, retval = 0;
  char *buffer = malloc(sizeof(char) * size_buffer);
  fd = open("/dev/ZERO0", O_RDONLY);
  if (fd < 0) {
    printf("open /dev/ZERO0 is not possible, %s.\n", strerror(errno));
    retval = -1;
    pthread_exit(&retval);
  }
  count = read(fd, buffer, size_buffer);
  printf("count %d buffer %d\n", count, size_buffer);
  if (count != size_buffer) {
    printf("Error read /dev/ZERO0 only %s.\n", strerror(errno));
    close(fd);
    retval = -1;
    pthread_exit(&retval);
  }
  close(fd);
  pthread_exit(&retval);
}


x86 assembly (clang, -O0):
<test_read_zero>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x2,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
lea    0xe57(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11ea <test_read_zero+0x7a>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <pthread_exit@plt>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     1253 <test_read_zero+0xe3>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <pthread_exit@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
lea    -0x18(%rbp),%rdi
call   1060 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_read_zero>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
mov    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
lea    0xe63(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11d9 <test_read_zero+0x69>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe4e(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
movl   $0xffffffff,0xc(%rsp)
ff
lea    0xc(%rsp),%rdi
call   1060 <pthread_exit@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x2,%edx
call   1050 <read@plt>
mov    %eax,%ebp
lea    0xe40(%rip),%rdi
mov    %eax,%esi
mov    $0x2,%edx
xor    %eax,%eax
call   1030 <printf@plt>
cmp    $0x2,%ebp
je     123c <test_read_zero+0xcc>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe2c(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
movl   $0xffffffff,0xc(%rsp)
ff
lea    0xc(%rsp),%rdi
call   1060 <pthread_exit@plt>
mov    %ebx,%edi
call   1040 <close@plt>
lea    0xc(%rsp),%rdi
call   1060 <pthread_exit@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/ZERO0\00"
@O_RDONLY = global i32 0
@.str.1 = [38 x i8] c"open /dev/ZERO0 is not possible, %s.\0A\00"
@errno = global i32 0
@.str.2 = [20 x i8] c"count %d buffer %d\0A\00"
@.str.3 = [32 x i8] c"Error read /dev/ZERO0 only %s.\0A\00"
define i8* @test_read_zero() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 2, i32* %4
store i32 0, i32* %5
%7 = load i32, i32* %4
%8 = sext i32 %7 to i64
%9 = mul i64 1, %8
%10 = trunc i64 %9 to i32
%11 = call i8* @malloc(i32 %10)
store i8* %11, i8** %6
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %0
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %18)
store i32 -1, i32* %5
%20 = call i32 @pthread_exit(i32* %5)
br label %21
21: ; preds = %16, %0
%22 = load i32, i32* %2
%23 = load i8*, i8** %6
%24 = load i32, i32* %4
%25 = call i32 @read(i32 %22, i8* %23, i32 %24)
store i32 %25, i32* %3
%26 = load i32, i32* %3
%27 = load i32, i32* %4
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %26, i32 %27)
%29 = load i32, i32* %3
%30 = load i32, i32* %4
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %39
32: ; preds = %21
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%36 = load i32, i32* %2
%37 = call i32 @close(i32 %36)
store i32 -1, i32* %5
%38 = call i32 @pthread_exit(i32* %5)
br label %39
39: ; preds = %32, %21
%40 = load i32, i32* %2
%41 = call i32 @close(i32 %40)
%42 = call i32 @pthread_exit(i32* %5)
%43 = load i8*, i8** %1
ret i8* %43
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/ZERO0\00"
@O_RDONLY = global i32 0
@.str.1 = [38 x i8] c"open /dev/ZERO0 is not possible, %s.\0A\00"
@errno = global i32 0
@.str.2 = [20 x i8] c"count %d buffer %d\0A\00"
@.str.3 = [32 x i8] c"Error read /dev/ZERO0 only %s.\0A\00"
define noalias i8* @test_read_zero() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%3 = tail call dereferenceable_or_null(2) i8* @malloc(i32 2)
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %9)
store i32 -1, i32* %1
%11 = call i32 @pthread_exit(i32* %1)
br label %12
12: ; preds = %7, %0
%13 = call i32 @read(i32 %5, i8* %3, i32 2)
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 2)
%15 = icmp eq i32 %13, 2
br i1 %15, label %22, label %16
16: ; preds = %12
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %18)
%20 = call i32 @close(i32 %5)
store i32 -1, i32* %1
%21 = call i32 @pthread_exit(i32* %1)
br label %22
22: ; preds = %16, %12
%23 = call i32 @close(i32 %5)
%24 = call i32 @pthread_exit(i32* %1)
ret i8* undef
}