#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*/ _SC_PAGESIZE;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
scalar_t__ posix_memalign(void **, size_t, size_t);
size_t read(int, char *, size_t);
int /*<<< orphan*/ stderr;
size_t sysconf(int /*<<< orphan*/);
size_t write(int, char *, size_t);
int dd(int in_fd, int out_fd, size_t buffersize) {
  char *buffer;
  size_t pagesize = sysconf(_SC_PAGESIZE);
  size_t count;
  if (posix_memalign((void **)&buffer, pagesize, buffersize) != 0 ||
      buffer == NULL) {
    fprintf(stderr, "Failed to allocate buffer.\n");
    return 1;
  }
  memset(buffer, 0, buffersize);
  while ((count = read(in_fd, buffer, buffersize)) != 0) {
    if (write(out_fd, buffer, count) < count) {
      fprintf(stderr, "Failed to write the buffer to output.\n");
      goto error;
    }
    memset(buffer, 0, buffersize);
  }
  free(buffer);
  return 0;
error:
  if (buffer != NULL) {
    memset(buffer, 0, buffersize);
    free(buffer);
  }
  return 1;
}


x86 assembly (clang, -O0):
<dd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rdi
call   1090 <posix_memalign@plt>
cmp    $0x0,%rax
jne    11ba <dd+0x4a>
cmpq   $0x0,-0x20(%rbp)
jne    11db <dd+0x6b>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    128f <dd+0x11f>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <read@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1250 <dd+0xe0>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <write@plt>
cmp    -0x30(%rbp),%rax
jae    123c <dd+0xcc>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1265 <dd+0xf5>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
jmp    11ea <dd+0x7a>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    128f <dd+0x11f>
cmpq   $0x0,-0x20(%rbp)
je     1288 <dd+0x118>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %edi,%r12d
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
lea    0x8(%rsp),%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1090 <posix_memalign@plt>
test   %rax,%rax
jne    121e <dd+0xae>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     121e <dd+0xae>
xor    %r14d,%r14d
xor    %esi,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
mov    0x8(%rsp),%rsi
mov    %r12d,%edi
mov    %rbx,%rdx
call   1060 <read@plt>
test   %rax,%rax
je     126a <dd+0xfa>
mov    %rax,%rbp
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rsi
mov    %r15d,%edi
mov    %rbp,%rdx
call   1040 <write@plt>
cmp    %rbp,%rax
jb     123b <dd+0xcb>
mov    0x8(%rsp),%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
mov    0x8(%rsp),%rsi
mov    %r12d,%edi
mov    %rbx,%rdx
call   1060 <read@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11e0 <dd+0x70>
jmp    126a <dd+0xfa>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    0xdd2(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%r14d
jmp    1274 <dd+0x104>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x8(%rsp),%rdi
mov    $0x1,%r14d
test   %rdi,%rdi
je     1274 <dd+0x104>
xor    %esi,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Failed to allocate buffer.\0A\00"
@.str.1 = [39 x i8] c"Failed to write the buffer to output.\0A\00"
define i32 @dd(i32 %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64 %2, i64* %7
%11 = load i32, i32* @_SC_PAGESIZE
%12 = call i64 @sysconf(i32 %11)
store i64 %12, i64* %9
%13 = load i64, i64* %9
%14 = load i64, i64* %7
%15 = call i64 @posix_memalign(i8** %8, i64 %13, i64 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %3
%18 = load i8*, i8** %8
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17, %3
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %60
23: ; preds = %17
%24 = load i8*, i8** %8
%25 = load i64, i64* %7
%26 = call i32 @memset(i8* %24, i32 0, i64 %25)
br label %27
27: ; preds = %43, %23
%28 = load i32, i32* %5
%29 = load i8*, i8** %8
%30 = load i64, i64* %7
%31 = call i64 @read(i32 %28, i8* %29, i64 %30)
store i64 %31, i64* %10
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %47
33: ; preds = %27
%34 = load i32, i32* %6
%35 = load i8*, i8** %8
%36 = load i64, i64* %10
%37 = call i64 @write(i32 %34, i8* %35, i64 %36)
%38 = load i64, i64* %10
%39 = icmp ult i64 %37, %38
br i1 %39, label %40, label %43
40: ; preds = %33
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %50
43: ; preds = %33
%44 = load i8*, i8** %8
%45 = load i64, i64* %7
%46 = call i32 @memset(i8* %44, i32 0, i64 %45)
br label %27
47: ; preds = %27
%48 = load i8*, i8** %8
%49 = call i32 @free(i8* %48)
store i32 0, i32* %4
br label %60
50: ; preds = %40
%51 = load i8*, i8** %8
%52 = icmp ne i8* %51, null
br i1 %52, label %53, label %59
53: ; preds = %50
%54 = load i8*, i8** %8
%55 = load i64, i64* %7
%56 = call i32 @memset(i8* %54, i32 0, i64 %55)
%57 = load i8*, i8** %8
%58 = call i32 @free(i8* %57)
br label %59
59: ; preds = %53, %50
store i32 1, i32* %4
br label %60
60: ; preds = %59, %47, %20
%61 = load i32, i32* %4
ret i32 %61
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Failed to allocate buffer.\0A\00"
@.str.1 = [39 x i8] c"Failed to write the buffer to output.\0A\00"
define i32 @dd(i32 %0, i32 %1, i64 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = load i32, i32* @_SC_PAGESIZE
%7 = tail call i64 @sysconf(i32 %6)
%8 = call i64 @posix_memalign(i8** %4, i64 %7, i64 %2)
%9 = icmp ne i64 %8, 0
%10 = load i8*, i8** %4
%11 = icmp eq i8* %10, null
%12 = select i1 %9, i1 true, i1 %11
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %44
16: ; preds = %3
%17 = call i32 @memset(i8* %10, i32 0, i64 %2)
%18 = load i8*, i8** %4
%19 = call i64 @read(i32 %0, i8* %18, i64 %2)
%20 = icmp eq i64 %19, 0
br i1 %20, label %37, label %21
21: ; preds = %16, %31
%22 = phi i64 [ %35, %31 ], [ %19, %16 ]
%23 = load i8*, i8** %4
%24 = call i64 @write(i32 %1, i8* %23, i64 %22)
%25 = icmp ult i64 %24, %22
br i1 %25, label %26, label %31
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%29 = load i8*, i8** %4
%30 = icmp eq i8* %29, null
br i1 %30, label %44, label %40
31: ; preds = %21
%32 = load i8*, i8** %4
%33 = call i32 @memset(i8* %32, i32 0, i64 %2)
%34 = load i8*, i8** %4
%35 = call i64 @read(i32 %0, i8* %34, i64 %2)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %21
37: ; preds = %31, %16
%38 = load i8*, i8** %4
%39 = call i32 @free(i8* %38)
br label %44
40: ; preds = %26
%41 = call i32 @memset(i8* %29, i32 0, i64 %2)
%42 = load i8*, i8** %4
%43 = call i32 @free(i8* %42)
br label %44
44: ; preds = %26, %40, %37, %13
%45 = phi i32 [ 1, %13 ], [ 0, %37 ], [ 1, %40 ], [ 1, %26 ]
ret i32 %45
}