#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_hello(void *arg) {
  int fd, count, size_buffer = 5, retval = 0;
  char *buffer = malloc(sizeof(char) * size_buffer);
  fd = open("/dev/ZERO1", O_RDONLY);
  if (fd < 0) {
    printf("Open /dev/ZERO1 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/ZERO1 only %s.\n", strerror(errno));
    close(fd);
    retval = -1;
    pthread_exit(&retval);
  }
  close(fd);
  pthread_exit(&retval);
}


x86 assembly (clang, -O0):
<test_read_hello>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x5,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
lea    0xe53(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ee <test_read_hello+0x7e>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <pthread_exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1257 <test_read_hello+0xe7>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <pthread_exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
lea    -0x20(%rbp),%rdi
call   1060 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_read_hello>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
mov    $0x5,%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_hello+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    $0x5,%edx
call   1050 <read@plt>
mov    %eax,%ebp
lea    0xe40(%rip),%rdi
mov    %eax,%esi
mov    $0x5,%edx
xor    %eax,%eax
call   1030 <printf@plt>
cmp    $0x5,%ebp
je     123c <test_read_hello+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/ZERO1\00"
@O_RDONLY = global i32 0
@.str.1 = [38 x i8] c"Open /dev/ZERO1 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/ZERO1 only %s.\0A\00"
define i8* @test_read_hello(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %3
store i32 5, i32* %6
store i32 0, i32* %7
%9 = load i32, i32* %6
%10 = sext i32 %9 to i64
%11 = mul i64 1, %10
%12 = trunc i64 %11 to i32
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %8
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %1
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %20)
store i32 -1, i32* %7
%22 = call i32 @pthread_exit(i32* %7)
br label %23
23: ; preds = %18, %1
%24 = load i32, i32* %4
%25 = load i8*, i8** %8
%26 = load i32, i32* %6
%27 = call i32 @read(i32 %24, i8* %25, i32 %26)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = load i32, i32* %6
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %28, i32 %29)
%31 = load i32, i32* %5
%32 = load i32, i32* %6
%33 = icmp ne i32 %31, %32
br i1 %33, label %34, label %41
34: ; preds = %23
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %36)
%38 = load i32, i32* %4
%39 = call i32 @close(i32 %38)
store i32 -1, i32* %7
%40 = call i32 @pthread_exit(i32* %7)
br label %41
41: ; preds = %34, %23
%42 = load i32, i32* %4
%43 = call i32 @close(i32 %42)
%44 = call i32 @pthread_exit(i32* %7)
%45 = load i8*, i8** %2
ret i8* %45
}

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