#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_WRONLY;
int atoi(char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ getcwd(int /*<<< orphan*/ *, int /*<<< orphan*/);
scalar_t__ malloc(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int readn(int, void *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int writen(int, void *, int /*<<< orphan*/);
int main(int argc, const char *argv[]) {
  char path[1024];
  strcpy(path, getcwd(NULL, 0));
  strcat(path, "/");
  strcat(path, argv[1]);
  int rfd = open(path, O_RDONLY);
  int wfd = open("hello.c", O_WRONLY | O_CREAT);
  if (rfd == -1 || wfd == -1) {
    perror("open");
    exit(1);
  }
  int size = atoi(argv[2]);
  char *buf = (char *)malloc(32);
  int nread;
  nread = readn(rfd, (void *)buf, size);
  int nwrite = writen(wfd, (void *)buf, strlen(buf));
  fprintf(stdout, "%s\n", buf);
  fprintf(stdout, "nread = %d,nwrite = %d\n", nread, nwrite);
  close(rfd);
  close(wfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x438(%rbp)
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1050 <getcwd@plt>
mov    -0x438(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
lea    -0x410(%rbp),%rdi
lea    0xde2(%rip),%rsi
call   10e0 <strcat@plt>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   10e0 <strcat@plt>
lea    -0x410(%rbp),%rdi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x414(%rbp)
mov    0x2d67(%rip),%rax
mov    (%rax),%esi
mov    0x2d96(%rip),%rax
or     (%rax),%esi
lea    0xd97(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x418(%rbp)
cmpl   $0xffffffff,-0x414(%rbp)
je     1290 <main+0xc0>
cmpl   $0xffffffff,-0x418(%rbp)
jne    12a6 <main+0xd6>
lea    0xd73(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x41c(%rbp)
mov    $0x20,%edi
call   1090 <malloc@plt>
mov    %rax,-0x428(%rbp)
mov    -0x414(%rbp),%edi
mov    -0x428(%rbp),%rsi
mov    -0x41c(%rbp),%edx
call   1060 <readn@plt>
mov    %eax,-0x42c(%rbp)
mov    -0x418(%rbp),%eax
mov    %eax,-0x444(%rbp)
mov    -0x428(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    -0x428(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x444(%rbp),%edi
mov    -0x440(%rbp),%rsi
mov    %eax,%edx
call   10c0 <writen@plt>
mov    %eax,-0x430(%rbp)
mov    0x2ca1(%rip),%rax
mov    (%rax),%edi
mov    -0x428(%rbp),%rdx
lea    0xcd0(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2c83(%rip),%rax
mov    (%rax),%edi
mov    -0x42c(%rbp),%edx
mov    -0x430(%rbp),%ecx
lea    0xcb1(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x414(%rbp),%edi
call   1070 <close@plt>
mov    -0x418(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
xor    %edi,%edi
xor    %esi,%esi
call   1050 <getcwd@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
lea    0xdff(%rip),%rsi
mov    %rbx,%rdi
call   10e0 <strcat@plt>
mov    0x8(%r14),%rsi
mov    %rbx,%rdi
call   10e0 <strcat@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
mov    %eax,%r15d
mov    0x2d90(%rip),%rax
mov    0x2dc1(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xdc0(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%r15d
je     12f3 <main+0x123>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
je     12f3 <main+0x123>
mov    0x10(%r14),%rdi
call   10d0 <atoi@plt>
mov    %eax,%ebx
mov    $0x20,%edi
call   1090 <malloc@plt>
mov    %rax,%rbp
mov    %r15d,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1060 <readn@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   10c0 <writen@plt>
mov    %eax,%ebx
mov    0x2d2d(%rip),%r13
mov    0x0(%r13),%edi
lea    0xd61(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x0(%r13),%edi
lea    0xd50(%rip),%rsi
mov    %r14d,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %r15d,%edi
call   1070 <close@plt>
mov    %r12d,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd10(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@O_RDONLY = global i32 0
@.str.1 = [8 x i8] c"hello.c\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.2 = [5 x i8] c"open\00"
@stdout = global i32 0
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [24 x i8] c"nread = %d,nwrite = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%14 = call i32 @getcwd(i32* null, i32 0)
%15 = call i32 @strcpy(i8* %13, i32 %14)
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%17 = call i32 @strcat(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @strcat(i8* %18, i8* %21)
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %7
%26 = load i32, i32* @O_WRONLY
%27 = load i32, i32* @O_CREAT
%28 = or i32 %26, %27
%29 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %7
%31 = icmp eq i32 %30, -1
br i1 %31, label %35, label %32
32: ; preds = %2
%33 = load i32, i32* %8
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %32, %2
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %32
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 2
%41 = load i8*, i8** %40
%42 = call i32 @atoi(i8* %41)
store i32 %42, i32* %9
%43 = call i64 @malloc(i32 32)
%44 = inttoptr i64 %43 to i8*
store i8* %44, i8** %10
%45 = load i32, i32* %7
%46 = load i8*, i8** %10
%47 = load i32, i32* %9
%48 = call i32 @readn(i32 %45, i8* %46, i32 %47)
store i32 %48, i32* %11
%49 = load i32, i32* %8
%50 = load i8*, i8** %10
%51 = load i8*, i8** %10
%52 = call i32 @strlen(i8* %51)
%53 = call i32 @writen(i32 %49, i8* %50, i32 %52)
store i32 %53, i32* %12
%54 = load i32, i32* @stdout
%55 = load i8*, i8** %10
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %55)
%57 = load i32, i32* @stdout
%58 = load i32, i32* %11
%59 = load i32, i32* %12
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %58, i32 %59)
%61 = load i32, i32* %7
%62 = call i32 @close(i32 %61)
%63 = load i32, i32* %8
%64 = call i32 @close(i32 %63)
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@O_RDONLY = global i32 0
@.str.1 = [8 x i8] c"hello.c\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.2 = [5 x i8] c"open\00"
@stdout = global i32 0
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [24 x i8] c"nread = %d,nwrite = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @getcwd(i32* null, i32 0)
%6 = call i32 @strcpy(i8* %4, i32 %5)
%7 = call i32 @strcat(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = call i32 @strcat(i8* %4, i8* %9)
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %4, i32 %11)
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_CREAT
%15 = or i32 %14, %13
%16 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = icmp eq i32 %12, -1
%18 = icmp eq i32 %16, -1
%19 = or i1 %17, %18
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = getelementptr inbounds i8*, i8** %1, i64 2
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
%27 = call i64 @malloc(i32 32)
%28 = inttoptr i64 %27 to i8*
%29 = call i32 @readn(i32 %12, i8* %28, i32 %26)
%30 = call i32 @strlen(i8* %28)
%31 = call i32 @writen(i32 %16, i8* %28, i32 %30)
%32 = load i32, i32* @stdout
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %28)
%34 = load i32, i32* @stdout
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %29, i32 %31)
%36 = call i32 @close(i32 %12)
%37 = call i32 @close(i32 %16)
ret i32 0
}