#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 */
typedef int ssize_t;
typedef scalar_t__ off_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
char *data_map(unsigned int, unsigned int, scalar_t__);
int /*<<< orphan*/ err(int, char *, char *, scalar_t__, ...);
int lseek(int, scalar_t__, int /*<<< orphan*/);
char *name_get(unsigned int);
int /*<<< orphan*/ printf(char *, char const *, scalar_t__, scalar_t__);
int write(int, char *, scalar_t__);
void do_write(int fd, unsigned name, unsigned code, unsigned seq, off_t pos,
              off_t len) {
  off_t done = 0;
  ssize_t ret;
  char *buf;
  const char *namestr;
  namestr = name_get(name);
  buf = data_map(code, seq, len);
  if (lseek(fd, pos, SEEK_SET) == -1) {
    err(1, "%s: lseek to %lld", name_get(name), pos);
  }
  while (done < len) {
    ret = write(fd, buf + done, len - done);
    if (ret == -1) {
      err(1, "%s: write %lld at %lld", name_get(name), len, pos);
    }
    done += ret;
  }
  printf("write %s: %lld at %lld\n", namestr, len, pos);
}


x86 assembly (clang, -O0):
<do_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    %r8,-0x18(%rbp)
mov    %r9,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
call   1080 <name_get@plt>
mov    %rax,-0x40(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1060 <data_map@plt>
mov    %rax,-0x38(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
jne    11e3 <do_write+0x83>
mov    -0x8(%rbp),%edi
call   1080 <name_get@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%edi
lea    0xe24(%rip),%rsi
mov    $0x0,%al
call   1070 <err@plt>
jmp    11e8 <do_write+0x88>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    1252 <do_write+0xf2>
mov    -0x4(%rbp),%edi
mov    -0x38(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1241 <do_write+0xe1>
mov    -0x8(%rbp),%edi
call   1080 <name_get@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x1,%edi
lea    0xdd8(%rip),%rsi
mov    $0x0,%al
call   1070 <err@plt>
movslq -0x2c(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11e8 <do_write+0x88>
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xdc4(%rip),%rdi
call   1040 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%rbx
mov    %r8,%r14
mov    %ecx,%r12d
mov    %edx,%ebp
mov    %edi,%r13d
mov    %esi,0xc(%rsp)
mov    %esi,%edi
call   1080 <name_get@plt>
mov    %rax,0x10(%rsp)
mov    %ebp,%edi
mov    %r12d,%esi
mov    %rbx,%rdx
call   1060 <data_map@plt>
mov    %rax,%rbp
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
mov    %r14,%rsi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     11bf <do_write+0x5f>
test   %rbx,%rbx
jg     11e6 <do_write+0x86>
jmp    123c <do_write+0xdc>
mov    0xc(%rsp),%edi
call   1080 <name_get@plt>
lea    0xe31(%rip),%rsi
mov    $0x1,%edi
mov    %rax,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1070 <err@plt>
test   %rbx,%rbx
jle    123c <do_write+0xdc>
xor    %r15d,%r15d
jmp    11fb <do_write+0x9b>
nopl   0x0(%rax,%rax,1)
movslq %r12d,%rax
add    %rax,%r15
cmp    %rbx,%r15
jge    123c <do_write+0xdc>
lea    (%r15,%rbp,1),%rsi
mov    %rbx,%rdx
sub    %r15,%rdx
mov    %r13d,%edi
call   1030 <write@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    11f0 <do_write+0x90>
mov    0xc(%rsp),%edi
call   1080 <name_get@plt>
mov    $0x1,%edi
lea    0xde8(%rip),%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1070 <err@plt>
jmp    11f0 <do_write+0x90>
lea    0xde6(%rip),%rdi
mov    0x10(%rsp),%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [18 x i8] c"%s: lseek to %lld\00"
@.str.1 = [23 x i8] c"%s: write %lld at %lld\00"
@.str.2 = [24 x i8] c"write %s: %lld at %lld\0A\00"
define void @do_write(i32 %0, i32 %1, i32 %2, i32 %3, i64 %4, i64 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i8*
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i64 %4, i64* %11
store i64 %5, i64* %12
store i64 0, i64* %13
%17 = load i32, i32* %8
%18 = call i8* @name_get(i32 %17)
store i8* %18, i8** %16
%19 = load i32, i32* %9
%20 = load i32, i32* %10
%21 = load i64, i64* %12
%22 = call i8* @data_map(i32 %19, i32 %20, i64 %21)
store i8* %22, i8** %15
%23 = load i32, i32* %7
%24 = load i64, i64* %11
%25 = load i32, i32* @SEEK_SET
%26 = call i32 @lseek(i32 %23, i64 %24, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %33
28: ; preds = %6
%29 = load i32, i32* %8
%30 = call i8* @name_get(i32 %29)
%31 = load i64, i64* %11
%32 = call i32 (i32, i8*, i8*, i64, ...) @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %30, i64 %31)
br label %33
33: ; preds = %28, %6
br label %34
34: ; preds = %55, %33
%35 = load i64, i64* %13
%36 = load i64, i64* %12
%37 = icmp slt i64 %35, %36
br i1 %37, label %38, label %60
38: ; preds = %34
%39 = load i32, i32* %7
%40 = load i8*, i8** %15
%41 = load i64, i64* %13
%42 = getelementptr inbounds i8, i8* %40, i64 %41
%43 = load i64, i64* %12
%44 = load i64, i64* %13
%45 = sub nsw i64 %43, %44
%46 = call i32 @write(i32 %39, i8* %42, i64 %45)
store i32 %46, i32* %14
%47 = load i32, i32* %14
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %55
49: ; preds = %38
%50 = load i32, i32* %8
%51 = call i8* @name_get(i32 %50)
%52 = load i64, i64* %12
%53 = load i64, i64* %11
%54 = call i32 (i32, i8*, i8*, i64, ...) @err(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %51, i64 %52, i64 %53)
br label %55
55: ; preds = %49, %38
%56 = load i32, i32* %14
%57 = sext i32 %56 to i64
%58 = load i64, i64* %13
%59 = add nsw i64 %58, %57
store i64 %59, i64* %13
br label %34
60: ; preds = %34
%61 = load i8*, i8** %16
%62 = load i64, i64* %12
%63 = load i64, i64* %11
%64 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %61, i64 %62, i64 %63)
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [18 x i8] c"%s: lseek to %lld\00"
@.str.1 = [23 x i8] c"%s: write %lld at %lld\00"
@.str.2 = [24 x i8] c"write %s: %lld at %lld\0A\00"
define void @do_write(i32 %0, i32 %1, i32 %2, i32 %3, i64 %4, i64 %5) {
%7 = tail call i8* @name_get(i32 %1)
%8 = tail call i8* @data_map(i32 %2, i32 %3, i64 %5)
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @lseek(i32 %0, i64 %4, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %6
%13 = tail call i8* @name_get(i32 %1)
%14 = tail call i32 (i32, i8*, i8*, i64, ...) @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %13, i64 %4)
br label %15
15: ; preds = %12, %6
%16 = icmp sgt i64 %5, 0
br i1 %16, label %17, label %30
17: ; preds = %15, %26
%18 = phi i64 [ %28, %26 ], [ 0, %15 ]
%19 = getelementptr inbounds i8, i8* %8, i64 %18
%20 = sub nsw i64 %5, %18
%21 = tail call i32 @write(i32 %0, i8* %19, i64 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = tail call i8* @name_get(i32 %1)
%25 = tail call i32 (i32, i8*, i8*, i64, ...) @err(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %24, i64 %5, i64 %4)
br label %26
26: ; preds = %23, %17
%27 = sext i32 %21 to i64
%28 = add nsw i64 %18, %27
%29 = icmp slt i64 %28, %5
br i1 %29, label %17, label %30
30: ; preds = %26, %15
%31 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %7, i64 %5, i64 %4)
ret void
}