#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 errno;
int gettid();
int /*<<< orphan*/ printf(char *, int, ...);
int read(int, char *, int);
char *strerror(int);
int _read(int fd) {
  int ret;
  char buf;
  printf("%d: read(%d)\n", gettid(), fd);
  ret = read(fd, &buf, 1);
  printf("%d: read(%d) = %d [%d]\n", gettid(), fd, ret, (int)buf);
  if (ret < 0)
    printf("\terr %d (%s)\n", errno, strerror(errno));
  return ret;
}


x86 assembly (clang, -O0):
<_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1030 <gettid@plt>
mov    %eax,%esi
mov    -0x4(%rbp),%edx
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1030 <gettid@plt>
mov    %eax,%esi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
movsbl -0x9(%rbp),%r8d
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jge    11d3 <_read+0x93>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x10(%rbp),%esi
mov    %rax,%rdx
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_read>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
xor    %eax,%eax
call   1030 <gettid@plt>
lea    0xead(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x7(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <gettid@plt>
movsbl 0x7(%rsp),%r8d
lea    0xe89(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
jns    11b9 <_read+0x79>
mov    0x2e43(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1060 <strerror@plt>
lea    0xe79(%rip),%rdi
mov    %ebp,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"%d: read(%d)\0A\00"
@.str.1 = [24 x i8] c"%d: read(%d) = %d [%d]\0A\00"
@.str.2 = [14 x i8] c"\09err %d (%s)\0A\00"
@errno = global i32 0
define i32 @_read(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
store i32 %0, i32* %2
%5 = call i32 (...) @gettid()
%6 = load i32, i32* %2
%7 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6)
%8 = load i32, i32* %2
%9 = call i32 @read(i32 %8, i8* %4, i32 1)
store i32 %9, i32* %3
%10 = call i32 (...) @gettid()
%11 = load i32, i32* %2
%12 = load i32, i32* %3
%13 = load i8, i8* %4
%14 = sext i8 %13 to i32
%15 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %11, i32 %12, i32 %14)
%16 = load i32, i32* %3
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %1
%19 = load i32, i32* @errno
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %19, i8* %21)
br label %23
23: ; preds = %18, %1
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
@.str = [14 x i8] c"%d: read(%d)\0A\00"
@.str.1 = [24 x i8] c"%d: read(%d) = %d [%d]\0A\00"
@.str.2 = [14 x i8] c"\09err %d (%s)\0A\00"
@errno = global i32 0
define i32 @_read(i32 %0) {
%2 = alloca i8
%3 = tail call i32 (...) @gettid()
%4 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %0)
%5 = call i32 @read(i32 %0, i8* %2, i32 1)
%6 = tail call i32 (...) @gettid()
%7 = load i8, i8* %2
%8 = sext i8 %7 to i32
%9 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 %0, i32 %5, i32 %8)
%10 = icmp slt i32 %5, 0
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %12, i8* %13)
br label %15
15: ; preds = %11, %1
ret i32 %5
}