#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 */
typedef int ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fwrite(char *, int, int, int /*<<< orphan*/);
scalar_t__ malloc(size_t);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int strlen(char const *);
void read_and_print(const char *dirname, char filename[]) {
  size_t fullpathlen = strlen(dirname) + strlen(filename) + 2;
  char buf[1024];
  char *fullname = (char *)malloc(fullpathlen);
  if (fullname == NULL) {
    fprintf(stderr, "Error allocating memory\n");
    exit(EXIT_FAILURE);
  }
  snprintf(fullname, fullpathlen, "%s/%s", dirname, filename);
  int fd = open(fullname, O_RDONLY);
  if (fd == -1) {
    perror("file open failed.\n");
    abort();
  } else {
    fprintf(stdout, "%s is opened successfully.\n", fullname);
    while (1) {
      ssize_t rd = read(fd, buf, 1024);
      if (rd == 0)
        break;
      else if (rd == -1 && errno == EINTR) {
        fprintf(stderr, "Some Error happened.\n");
        exit(EXIT_FAILURE);
      } else {
        if (fwrite(buf, rd, 1, stdout) != 1) {
          fprintf(stderr, "fwrite error\n");
          exit(EXIT_FAILURE);
        }
      }
    }
  }
  close(fd);
}


x86 assembly (clang, -O0):
<read_and_print>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x434(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x434(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    1231 <read_and_print+0x81>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
lea    0xde4(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x428(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xdce(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x428(%rbp),%rdi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x42c(%rbp)
cmpl   $0xffffffff,-0x42c(%rbp)
jne    128b <read_and_print+0xdb>
lea    0xda0(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
mov    -0x428(%rbp),%rdx
lea    0xd90(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x42c(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x430(%rbp)
cmpl   $0x0,-0x430(%rbp)
jne    12d8 <read_and_print+0x128>
jmp    1381 <read_and_print+0x1d1>
cmpl   $0xffffffff,-0x430(%rbp)
jne    1324 <read_and_print+0x174>
mov    0x2ce4(%rip),%rax
mov    (%rax),%rax
mov    0x2cca(%rip),%rcx
cmp    (%rcx),%rax
jne    1324 <read_and_print+0x174>
mov    0x2cd2(%rip),%rax
mov    (%rax),%edi
lea    0xd3f(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    -0x420(%rbp),%rdi
mov    -0x430(%rbp),%esi
mov    0x2c80(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%edx
call   10d0 <fwrite@plt>
cmp    $0x1,%eax
je     1372 <read_and_print+0x1c2>
mov    0x2c84(%rip),%rax
mov    (%rax),%edi
lea    0xd07(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2c5d(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    1377 <read_and_print+0x1c7>
jmp    137c <read_and_print+0x1cc>
jmp    12a9 <read_and_print+0xf9>
jmp    1386 <read_and_print+0x1d6>
mov    -0x42c(%rbp),%edi
call   1060 <close@plt>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_and_print>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %ebx,%eax
add    $0x2,%eax
movslq %eax,%rbp
mov    %rbp,%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     12e6 <read_and_print+0x136>
mov    %rax,%rbx
lea    0xe22(%rip),%rdx
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r15,%rcx
mov    %r14,%r8
call   1050 <snprintf@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12f8 <read_and_print+0x148>
mov    %eax,%ebp
mov    0x2d8d(%rip),%r14
mov    (%r14),%edi
lea    0xdfd(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %rsp,%rbx
mov    0x2d87(%rip),%r15
mov    0x2d70(%rip),%r12
jmp    1280 <read_and_print+0xd0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r15),%rcx
cmp    (%r12),%rcx
je     12c1 <read_and_print+0x111>
mov    (%r14),%ecx
mov    %rbx,%rdi
mov    %eax,%esi
mov    $0x1,%edx
call   10d0 <fwrite@plt>
cmp    $0x1,%eax
jne    12af <read_and_print+0xff>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     1260 <read_and_print+0xb0>
test   %eax,%eax
jne    1269 <read_and_print+0xb9>
mov    %ebp,%edi
call   1060 <close@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
jmp    12d1 <read_and_print+0x121>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
lea    0xd7d(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
lea    0xd0a(%rip),%rsi
jmp    12d1 <read_and_print+0x121>
lea    0xd20(%rip),%rdi
call   10b0 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [25 x i8] c"Error allocating memory\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [6 x i8] c"%s/%s\00"
@O_RDONLY = global i32 0
@.str.2 = [19 x i8] c"file open failed.\0A\00"
@stdout = global i32 0
@.str.3 = [28 x i8] c"%s is opened successfully.\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.4 = [22 x i8] c"Some Error happened.\0A\00"
@.str.5 = [14 x i8] c"fwrite error\0A\00"
define void @read_and_print(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca [1024 x i8]
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
%12 = load i8*, i8** %4
%13 = call i32 @strlen(i8* %12)
%14 = add nsw i32 %11, %13
%15 = add nsw i32 %14, 2
%16 = sext i32 %15 to i64
store i64 %16, i64* %5
%17 = load i64, i64* %5
%18 = call i64 @malloc(i64 %17)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %27
22: ; preds = %2
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %2
%28 = load i8*, i8** %7
%29 = load i64, i64* %5
%30 = load i8*, i8** %3
%31 = load i8*, i8** %4
%32 = call i32 @snprintf(i8* %28, i64 %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %30, i8* %31)
%33 = load i8*, i8** %7
%34 = load i32, i32* @O_RDONLY
%35 = call i32 @open(i8* %33, i32 %34)
store i32 %35, i32* %8
%36 = load i32, i32* %8
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %27
%39 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 (...) @abort()
unreachable
41: ; preds = %27
%42 = load i32, i32* @stdout
%43 = load i8*, i8** %7
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %41, %77
%46 = load i32, i32* %8
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%48 = call i32 @read(i32 %46, i8* %47, i32 1024)
store i32 %48, i32* %9
%49 = load i32, i32* %9
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %45
br label %78
52: ; preds = %45
%53 = load i32, i32* %9
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %64
55: ; preds = %52
%56 = load i64, i64* @errno
%57 = load i64, i64* @EINTR
%58 = icmp eq i64 %56, %57
br i1 %58, label %59, label %64
59: ; preds = %55
%60 = load i32, i32* @stderr
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%62 = load i32, i32* @EXIT_FAILURE
%63 = call i32 @exit(i32 %62)
unreachable
64: ; preds = %55, %52
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%66 = load i32, i32* %9
%67 = load i32, i32* @stdout
%68 = call i32 @fwrite(i8* %65, i32 %66, i32 1, i32 %67)
%69 = icmp ne i32 %68, 1
br i1 %69, label %70, label %75
70: ; preds = %64
%71 = load i32, i32* @stderr
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%73 = load i32, i32* @EXIT_FAILURE
%74 = call i32 @exit(i32 %73)
unreachable
75: ; preds = %64
br label %76
76: ; preds = %75
br label %77
77: ; preds = %76
br label %45
78: ; preds = %51
br label %79
79: ; preds = %78
%80 = load i32, i32* %8
%81 = call i32 @close(i32 %80)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [25 x i8] c"Error allocating memory\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [6 x i8] c"%s/%s\00"
@O_RDONLY = global i32 0
@.str.2 = [19 x i8] c"file open failed.\0A\00"
@stdout = global i32 0
@.str.3 = [28 x i8] c"%s is opened successfully.\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.4 = [22 x i8] c"Some Error happened.\0A\00"
@.str.5 = [14 x i8] c"fwrite error\0A\00"
define void @read_and_print(i8* %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i32 @strlen(i8* %1)
%6 = add i32 %4, 2
%7 = add i32 %6, %5
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%10 = tail call i64 @malloc(i64 %8)
%11 = inttoptr i64 %10 to i8*
%12 = icmp eq i64 %10, 0
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
%19 = tail call i32 @snprintf(i8* %11, i64 %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1)
%20 = load i32, i32* @O_RDONLY
%21 = tail call i32 @open(i8* %11, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%25 = tail call i32 (...) @abort()
unreachable
26: ; preds = %18
%27 = load i32, i32* @stdout
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %11)
br label %29
29: ; preds = %40, %26
%30 = call i32 @read(i32 %21, i8* %9, i32 1024)
switch i32 %30, label %40 [
i32 0, label %49
i32 -1, label %31
]
31: ; preds = %29
%32 = load i64, i64* @errno
%33 = load i64, i64* @EINTR
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %40
35: ; preds = %31
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %29, %31
%41 = load i32, i32* @stdout
%42 = call i32 @fwrite(i8* %9, i32 %30, i32 1, i32 %41)
%43 = icmp eq i32 %42, 1
br i1 %43, label %29, label %44
44: ; preds = %40
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %29
%50 = call i32 @close(i32 %21)
ret void
}