#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 duffs_device(char *, char *, int);
int getline(char **, int *, int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stdin;
int main(void) {
  int bytes_read;
  int bytes_copied;
  int buff_siz = 20;
  char *my_name;
  char *name_cp;
  puts("Please enter your name.");
  my_name = malloc(buff_siz + 1);
  bytes_read = getline(&my_name, &buff_siz, stdin);
  if (bytes_read == -1) {
    perror("Insufficient input.");
    return -1;
  }
  name_cp = malloc(buff_siz + 1);
  bytes_copied = duffs_device(my_name, name_cp, bytes_read);
  if (bytes_read == bytes_copied) {
    puts("Success. Your name in the second buffer is:");
    puts(name_cp);
  } else {
    perror("Failed to copy as many bytes as there were read.");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x14,-0x10(%rbp)
lea    0xe93(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e60(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1070 <getline@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11bc <main+0x6c>
lea    0xe6d(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1223 <main+0xd3>
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1040 <duffs_device@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    1204 <main+0xb4>
lea    0xe3b(%rip),%rdi
call   1030 <puts@plt>
mov    -0x20(%rbp),%rdi
call   1030 <puts@plt>
jmp    121c <main+0xcc>
lea    0xe4d(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1223 <main+0xd3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movl   $0x14,0xc(%rsp)
lea    0xe9a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x15,%edi
call   1050 <malloc@plt>
mov    %rax,0x10(%rsp)
mov    0x2e67(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rsi
call   1070 <getline@plt>
cmp    $0xffffffff,%eax
je     11d9 <main+0x89>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    0x10(%rsp),%rdi
mov    %rax,%rsi
mov    %ebx,%edx
call   1040 <duffs_device@plt>
cmp    %eax,%ebx
jne    11e2 <main+0x92>
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe38(%rip),%rdi
jmp    11e9 <main+0x99>
lea    0xe6f(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    11d1 <main+0x81>

LLVM IR (-O0):
@.str = [24 x i8] c"Please enter your name.\00"
@stdin = global i32 0
@.str.1 = [20 x i8] c"Insufficient input.\00"
@.str.2 = [44 x i8] c"Success. Your name in the second buffer is:\00"
@.str.3 = [49 x i8] c"Failed to copy as many bytes as there were read.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i32 0, i32* %1
store i32 20, i32* %4
%7 = call i32 @puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* %4
%9 = add nsw i32 %8, 1
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %5
%11 = load i32, i32* @stdin
%12 = call i32 @getline(i8** %5, i32* %4, i32 %11)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %35
17: ; preds = %0
%18 = load i32, i32* %4
%19 = add nsw i32 %18, 1
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** %6
%21 = load i8*, i8** %5
%22 = load i8*, i8** %6
%23 = load i32, i32* %2
%24 = call i32 @duffs_device(i8* %21, i8* %22, i32 %23)
store i32 %24, i32* %3
%25 = load i32, i32* %2
%26 = load i32, i32* %3
%27 = icmp eq i32 %25, %26
br i1 %27, label %28, label %32
28: ; preds = %17
%29 = call i32 @puts(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%30 = load i8*, i8** %6
%31 = call i32 @puts(i8* %30)
br label %34
32: ; preds = %17
%33 = call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %35
34: ; preds = %28
store i32 0, i32* %1
br label %35
35: ; preds = %34, %32, %15
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
@.str = [24 x i8] c"Please enter your name.\00"
@stdin = global i32 0
@.str.1 = [20 x i8] c"Insufficient input.\00"
@.str.2 = [44 x i8] c"Success. Your name in the second buffer is:\00"
@.str.3 = [49 x i8] c"Failed to copy as many bytes as there were read.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = bitcast i32* %1 to i8*
store i32 20, i32* %1
%4 = bitcast i8** %2 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%6 = tail call dereferenceable_or_null(21) i8* @malloc(i32 21)
store i8* %6, i8** %2
%7 = load i32, i32* @stdin
%8 = call i32 @getline(i8** %2, i32* %1, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %24
12: ; preds = %0
%13 = load i32, i32* %1
%14 = add nsw i32 %13, 1
%15 = call i8* @malloc(i32 %14)
%16 = load i8*, i8** %2
%17 = call i32 @duffs_device(i8* %16, i8* %15, i32 %8)
%18 = icmp eq i32 %8, %17
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @puts(i8* dereferenceable(1) %15)
br label %24
22: ; preds = %12
%23 = call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %19, %22, %10
%25 = phi i32 [ -1, %10 ], [ 0, %19 ], [ -1, %22 ]
ret i32 %25
}