#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 O_CREAT;
int O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  char sourcefile[30] = "task1.c";
  char destfile[30] = "hehe.c";
  int i;
  int source = open(sourcefile, O_RDONLY, 0);
  int dest = open(destfile, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  char buf[4096];
  if (source > 0 && dest > 0) {
    do {
      i = read(source, buf, 4096);
      write(dest, buf, i);
    } while (i);
  } else {
    printf("Error!");
  }
  close(source);
  close(dest);
  return 10;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe97(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe94(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe91(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe8f(%rip),%eax
mov    %eax,-0x18(%rbp)
mov    0xe89(%rip),%ax
mov    %ax,-0x14(%rbp)
mov    0xe82(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0xe7f(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0xe7c(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe7a(%rip),%eax
mov    %eax,-0x38(%rbp)
mov    0xe74(%rip),%ax
mov    %ax,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x58(%rbp)
lea    -0x50(%rbp),%rdi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    0x2e01(%rip),%rax
or     (%rax),%esi
mov    0x2df0(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1070 <open@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x58(%rbp)
jle    1260 <main+0x110>
cmpl   $0x0,-0x5c(%rbp)
jle    1260 <main+0x110>
jmp    1228 <main+0xd8>
mov    -0x58(%rbp),%edi
lea    -0x1060(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0x54(%rbp)
mov    -0x5c(%rbp),%edi
lea    -0x1060(%rbp),%rsi
mov    -0x54(%rbp),%edx
call   1030 <write@plt>
cmpl   $0x0,-0x54(%rbp)
jne    1228 <main+0xd8>
jmp    126c <main+0x11c>
lea    0xdd7(%rip),%rdi
call   1040 <printf@plt>
mov    -0x58(%rbp),%edi
call   1050 <close@plt>
mov    -0x5c(%rbp),%edi
call   1050 <close@plt>
mov    $0xa,%eax
add    $0x1060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1048,%rsp
movups 0xeaa(%rip),%xmm0
movups %xmm0,0x2e(%rsp)
movaps 0xe90(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movups 0xeb2(%rip),%xmm0
movups %xmm0,0xe(%rsp)
movaps 0xe98(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
lea    0x20(%rsp),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    0x2e18(%rip),%rcx
mov    0x2e49(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    %eax,%r14d
mov    0x2e33(%rip),%rax
or     (%rax),%esi
mov    %rsp,%rdi
mov    $0x180,%edx
call   1070 <open@plt>
mov    %eax,%r15d
test   %r14d,%r14d
jle    1205 <main+0xb5>
test   %r15d,%r15d
jle    1205 <main+0xb5>
lea    0x40(%rsp),%rbx
xchg   %ax,%ax
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,%ebp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %ebp,%ebp
jne    11e0 <main+0x90>
jmp    1211 <main+0xc1>
lea    0xe32(%rip),%rdi
call   1040 <printf@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    $0xa,%eax
add    $0x1048,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.sourcefile = [30 x i8] c"task1.c\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@__const.main.destfile = [30 x i8] c"hehe.c\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [7 x i8] c"Error!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [30 x i8]
%3 = alloca [30 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [4096 x i8]
store i32 0, i32* %1
%8 = bitcast [30 x i8]* %2 to i8*
%9 = bitcast [30 x i8]* %3 to i8*
%10 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11, i32 0)
store i32 %12, i32* %5
%13 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = call i32 @open(i8* %13, i32 %18, i32 384)
store i32 %19, i32* %6
%20 = load i32, i32* %5
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %38
22: ; preds = %0
%23 = load i32, i32* %6
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %38
25: ; preds = %22
br label %26
26: ; preds = %34, %25
%27 = load i32, i32* %5
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%29 = call i32 @read(i32 %27, i8* %28, i32 4096)
store i32 %29, i32* %4
%30 = load i32, i32* %6
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%32 = load i32, i32* %4
%33 = call i32 @write(i32 %30, i8* %31, i32 %32)
br label %34
34: ; preds = %26
%35 = load i32, i32* %4
%36 = icmp ne i32 %35, 0
br i1 %36, label %26, label %37
37: ; preds = %34
br label %40
38: ; preds = %22, %0
%39 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %40
40: ; preds = %38, %37
%41 = load i32, i32* %5
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
ret i32 10
}

LLVM IR (-O2):
@__const.main.sourcefile = [30 x i8] c"task1.c\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@__const.main.destfile = [30 x i8] c"hehe.c\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [7 x i8] c"Error!\00"
define i32 @main() {
%1 = alloca [30 x i8]
%2 = alloca [30 x i8]
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* %4, i32 %6, i32 0)
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %9, %8
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %5, i32 %12, i32 384)
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%15 = icmp sgt i32 %7, 0
%16 = icmp sgt i32 %13, 0
%17 = and i1 %15, %16
br i1 %17, label %18, label %22
18: ; preds = %0, %18
%19 = call i32 @read(i32 %7, i8* %14, i32 4096)
%20 = call i32 @write(i32 %13, i8* %14, i32 %19)
%21 = icmp eq i32 %19, 0
br i1 %21, label %24, label %18
22: ; preds = %0
%23 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %18, %22
%25 = tail call i32 @close(i32 %7)
%26 = tail call i32 @close(i32 %13)
ret i32 10
}