#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, ...);
char *strerror(int);
int write(int, char *, int);
int _write(int fd, char *buf, int len) {
  int ret;
  printf("%d: write(%d)\n", gettid(), fd);
  ret = write(fd, buf, len);
  printf("%d: write(%d) = %d\n", gettid(), fd, ret);
  if (ret < 0)
    printf("\terr %d (%s)\n", errno, strerror(errno));
  return ret;
}


x86 assembly (clang, -O0):
<_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x0,%al
call   1030 <gettid@plt>
mov    %eax,%esi
mov    -0x4(%rbp),%edx
lea    0xe9b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <gettid@plt>
mov    %eax,%esi
mov    -0x4(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
jge    11d3 <_write+0x93>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x1c(%rbp),%esi
mov    %rax,%rdx
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_write>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1030 <gettid@plt>
lea    0xea6(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <write@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <gettid@plt>
lea    0xe8d(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebx,%ebx
jns    11b6 <_write+0x76>
mov    0x2e46(%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   1050 <printf@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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