#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*/ SEEK_SET;
int /*<<< orphan*/ free(char *);
int lseek(int, int, int /*<<< orphan*/);
scalar_t__ malloc(int);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
void write_image(int src, int des, int bs, int count, int sp, int sk) {
  int i;
  char *buf;
  i = 0;
  buf = (char *)malloc(bs * sizeof(char));
  if (!buf)
    return;
  if (lseek(src, sp * bs, SEEK_SET) == -1)
    return;
  if (lseek(des, sk * bs, SEEK_SET) == -1)
    return;
  while (read(src, buf, bs) && i < count) {
    write(des, buf, bs);
    ++i;
  }
  free(buf);
}


x86 assembly (clang, -O0):
<write_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    %r8d,-0x14(%rbp)
mov    %r9d,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1196 <write_image+0x46>
jmp    124a <write_image+0xfa>
mov    -0x4(%rbp),%edi
mov    -0x14(%rbp),%esi
imul   -0xc(%rbp),%esi
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
jne    11bc <write_image+0x6c>
jmp    124a <write_image+0xfa>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%esi
imul   -0xc(%rbp),%esi
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
jne    11e2 <write_image+0x92>
jmp    124a <write_image+0xfa>
jmp    11e7 <write_image+0x97>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1060 <read@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x29(%rbp)
je     1214 <write_image+0xc4>
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
setl   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1224 <write_image+0xd4>
jmp    1241 <write_image+0xf1>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <write@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11e7 <write_image+0x97>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_image>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%ebx
mov    %r8d,%r13d
mov    %ecx,0x4(%rsp)
mov    %edx,%ebp
mov    %esi,%r15d
mov    %edi,%r12d
mov    %edx,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     121f <write_image+0xcf>
mov    %rax,%r14
imul   %ebp,%r13d
mov    0x2e55(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
mov    %r13d,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     121f <write_image+0xcf>
imul   %ebp,%ebx
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
mov    %ebx,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     121f <write_image+0xcf>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1060 <read@plt>
test   %rax,%rax
mov    0x4(%rsp),%r13d
je     1209 <write_image+0xb9>
test   %r13d,%r13d
jle    1209 <write_image+0xb9>
mov    $0x1,%ebx
nopl   (%rax)
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1060 <read@plt>
test   %rax,%rax
je     1209 <write_image+0xb9>
lea    0x1(%rbx),%eax
cmp    %r13d,%ebx
mov    %eax,%ebx
jl     11e0 <write_image+0x90>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
define void @write_image(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
store i32 0, i32* %13
%15 = load i32, i32* %9
%16 = sext i32 %15 to i64
%17 = mul i64 %16, 1
%18 = trunc i64 %17 to i32
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %14
%21 = load i8*, i8** %14
%22 = icmp ne i8* %21, null
br i1 %22, label %24, label %23
23: ; preds = %6
br label %65
24: ; preds = %6
%25 = load i32, i32* %7
%26 = load i32, i32* %11
%27 = load i32, i32* %9
%28 = mul nsw i32 %26, %27
%29 = load i32, i32* @SEEK_SET
%30 = call i32 @lseek(i32 %25, i32 %28, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %33
32: ; preds = %24
br label %65
33: ; preds = %24
%34 = load i32, i32* %8
%35 = load i32, i32* %12
%36 = load i32, i32* %9
%37 = mul nsw i32 %35, %36
%38 = load i32, i32* @SEEK_SET
%39 = call i32 @lseek(i32 %34, i32 %37, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %42
41: ; preds = %33
br label %65
42: ; preds = %33
br label %43
43: ; preds = %55, %42
%44 = load i32, i32* %7
%45 = load i8*, i8** %14
%46 = load i32, i32* %9
%47 = call i64 @read(i32 %44, i8* %45, i32 %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %43
%50 = load i32, i32* %13
%51 = load i32, i32* %10
%52 = icmp slt i32 %50, %51
br label %53
53: ; preds = %49, %43
%54 = phi i1 [ false, %43 ], [ %52, %49 ]
br i1 %54, label %55, label %62
55: ; preds = %53
%56 = load i32, i32* %8
%57 = load i8*, i8** %14
%58 = load i32, i32* %9
%59 = call i32 @write(i32 %56, i8* %57, i32 %58)
%60 = load i32, i32* %13
%61 = add nsw i32 %60, 1
store i32 %61, i32* %13
br label %43
62: ; preds = %53
%63 = load i8*, i8** %14
%64 = call i32 @free(i8* %63)
br label %65
65: ; preds = %62, %41, %32, %23
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define void @write_image(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = tail call i64 @malloc(i32 %2)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %35, label %10
10: ; preds = %6
%11 = mul nsw i32 %4, %2
%12 = load i32, i32* @SEEK_SET
%13 = tail call i32 @lseek(i32 %0, i32 %11, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %35, label %15
15: ; preds = %10
%16 = mul nsw i32 %5, %2
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @lseek(i32 %1, i32 %16, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %35, label %20
20: ; preds = %15
%21 = tail call i64 @read(i32 %0, i8* %8, i32 %2)
%22 = icmp ne i64 %21, 0
%23 = icmp sgt i32 %3, 0
%24 = and i1 %22, %23
br i1 %24, label %25, label %33
25: ; preds = %20, %25
%26 = phi i32 [ %28, %25 ], [ 0, %20 ]
%27 = tail call i32 @write(i32 %1, i8* %8, i32 %2)
%28 = add nuw nsw i32 %26, 1
%29 = tail call i64 @read(i32 %0, i8* %8, i32 %2)
%30 = icmp ne i64 %29, 0
%31 = icmp slt i32 %28, %3
%32 = select i1 %30, i1 %31, i1 false
br i1 %32, label %25, label %33
33: ; preds = %25, %20
%34 = tail call i32 @free(i8* %8)
br label %35
35: ; preds = %15, %10, %6, %33
ret void
}