#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ putchar(int);
int serv_connect(char *);
int serv_open(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    fprintf(stderr, "Usage: %s filename1 [filename2] [filename3] ...\n",
            argv[0]);
    exit(EXIT_FAILURE);
  }
  int sockfd;
  if ((sockfd = serv_connect("/tmp/opend.socket")) < 0) {
    perror("Couldn't connect to opend socket");
    exit(EXIT_FAILURE);
  }
  int i;
  for (i = 1; i < argc; i++) {
    int fd = serv_open(sockfd, argv[i], O_RDONLY);
    if (fd < 0) {
      fprintf(stderr, "Couldn't open %s: %s\n", argv[i], strerror(errno));
      continue;
    }
    FILE *fptr = fdopen(fd, "r");
    if (fptr == NULL) {
      fprintf(stderr, "Couldn't get file pointer for %s: %s\n", argv[i],
              strerror(errno));
      close(fd);
      continue;
    }
    int c;
    while ((c = getc(fptr)) != EOF)
      putchar(c);
    fclose(fptr);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11fc <main+0x4c>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xe2e(%rip),%rdi
call   1090 <serv_connect@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    122e <main+0x7e>
lea    0xe28(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1363 <main+0x1b3>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
call   1050 <serv_open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12b2 <main+0x102>
mov    0x2d5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xdbe(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1355 <main+0x1a5>
mov    -0x1c(%rbp),%edi
lea    0xdbe(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    131f <main+0x16f>
mov    0x2cf9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x48(%rbp)
mov    0x2cd5(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xd71(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
jmp    1355 <main+0x1a5>
jmp    1324 <main+0x174>
mov    -0x28(%rbp),%rdi
call   10d0 <getc@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2ca9(%rip),%rcx
cmp    (%rcx),%eax
je     134c <main+0x19c>
mov    -0x2c(%rbp),%edi
call   1030 <putchar@plt>
jmp    1324 <main+0x174>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1235 <main+0x85>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
cmp    $0x1,%edi
jle    12fe <main+0x14e>
mov    %edi,%ebp
lea    0xe5e(%rip),%rdi
call   1090 <serv_connect@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
js     131a <main+0x16a>
mov    %ebp,%eax
mov    %rax,0x10(%rsp)
mov    $0x1,%r13d
mov    0x2de8(%rip),%r15
jmp    1217 <main+0x67>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1040 <fclose@plt>
add    $0x1,%r13
cmp    0x10(%rsp),%r13
je     12ed <main+0x13d>
mov    (%r14,%r13,8),%rsi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
mov    0xc(%rsp),%edi
call   1050 <serv_open@plt>
test   %eax,%eax
js     1270 <main+0xc0>
mov    %eax,%ebp
mov    %eax,%edi
lea    0xe3e(%rip),%rsi
call   1080 <fdopen@plt>
test   %rax,%rax
je     12b0 <main+0x100>
mov    %rax,%r12
nopl   0x0(%rax)
mov    %r12,%rdi
call   10d0 <getc@plt>
cmp    (%r15),%eax
je     1200 <main+0x50>
mov    %eax,%edi
call   1030 <putchar@plt>
jmp    1250 <main+0xa0>
cs nopw 0x0(%rax,%rax,1)
mov    0x2d59(%rip),%rax
mov    (%rax),%ebp
mov    (%r14,%r13,8),%rbx
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %ebp,%edi
lea    0xdd0(%rip),%rsi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1208 <main+0x58>
cs nopw 0x0(%rax,%rax,1)
mov    0x2d19(%rip),%rax
mov    (%rax),%ebx
mov    (%r14,%r13,8),%r12
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %ebx,%edi
lea    0xda8(%rip),%rsi
mov    %r12,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1208 <main+0x58>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xcef(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1326 <main+0x176>
lea    0xd22(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c93(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [49 x i8] c"Usage: %s filename1 [filename2] [filename3] ...\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [18 x i8] c"/tmp/opend.socket\00"
@.str.2 = [33 x i8] c"Couldn't connect to opend socket\00"
@O_RDONLY = global i32 0
@.str.3 = [22 x i8] c"Couldn't open %s: %s\0A\00"
@errno = global i32 0
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [38 x i8] c"Couldn't get file pointer for %s: %s\0A\00"
@EOF = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %21
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = call i32 @serv_connect(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 %22, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %21
store i32 1, i32* %7
br label %29
29: ; preds = %83, %28
%30 = load i32, i32* %7
%31 = load i32, i32* %4
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %86
33: ; preds = %29
%34 = load i32, i32* %6
%35 = load i8**, i8*** %5
%36 = load i32, i32* %7
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8*, i8** %35, i64 %37
%39 = load i8*, i8** %38
%40 = load i32, i32* @O_RDONLY
%41 = call i32 @serv_open(i32 %34, i8* %39, i32 %40)
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %54
44: ; preds = %33
%45 = load i32, i32* @stderr
%46 = load i8**, i8*** %5
%47 = load i32, i32* %7
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8*, i8** %46, i64 %48
%50 = load i8*, i8** %49
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %50, i8* %52)
br label %83
54: ; preds = %33
%55 = load i32, i32* %8
%56 = call i32* @fdopen(i32 %55, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %56, i32** %9
%57 = load i32*, i32** %9
%58 = icmp eq i32* %57, null
br i1 %58, label %59, label %71
59: ; preds = %54
%60 = load i32, i32* @stderr
%61 = load i8**, i8*** %5
%62 = load i32, i32* %7
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8*, i8** %61, i64 %63
%65 = load i8*, i8** %64
%66 = load i32, i32* @errno
%67 = call i8* @strerror(i32 %66)
%68 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i8* %65, i8* %67)
%69 = load i32, i32* %8
%70 = call i32 @close(i32 %69)
br label %83
71: ; preds = %54
br label %72
72: ; preds = %77, %71
%73 = load i32*, i32** %9
%74 = call i32 @getc(i32* %73)
store i32 %74, i32* %10
%75 = load i32, i32* @EOF
%76 = icmp ne i32 %74, %75
br i1 %76, label %77, label %80
77: ; preds = %72
%78 = load i32, i32* %10
%79 = call i32 @putchar(i32 %78)
br label %72
80: ; preds = %72
%81 = load i32*, i32** %9
%82 = call i32 @fclose(i32* %81)
br label %83
83: ; preds = %80, %59, %44
%84 = load i32, i32* %7
%85 = add nsw i32 %84, 1
store i32 %85, i32* %7
br label %29
86: ; preds = %29
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [49 x i8] c"Usage: %s filename1 [filename2] [filename3] ...\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [18 x i8] c"/tmp/opend.socket\00"
@.str.2 = [33 x i8] c"Couldn't connect to opend socket\00"
@O_RDONLY = global i32 0
@.str.3 = [22 x i8] c"Couldn't open %s: %s\0A\00"
@errno = global i32 0
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [38 x i8] c"Couldn't get file pointer for %s: %s\0A\00"
@EOF = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %10
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = tail call i32 @serv_connect(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp slt i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = zext i32 %0 to i64
br label %19
15: ; preds = %10
%16 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %13, %54
%20 = phi i64 [ 1, %13 ], [ %55, %54 ]
%21 = getelementptr inbounds i8*, i8** %1, i64 %20
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDONLY
%24 = tail call i32 @serv_open(i32 %11, i8* %22, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %21
%29 = load i32, i32* @errno
%30 = tail call i8* @strerror(i32 %29)
%31 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %28, i8* %30)
br label %54
32: ; preds = %19
%33 = tail call i32* @fdopen(i32 %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%34 = icmp eq i32* %33, null
br i1 %34, label %39, label %35
35: ; preds = %32
%36 = tail call i32 @getc(i32* %33)
%37 = load i32, i32* @EOF
%38 = icmp eq i32 %36, %37
br i1 %38, label %52, label %46
39: ; preds = %32
%40 = load i32, i32* @stderr
%41 = load i8*, i8** %21
%42 = load i32, i32* @errno
%43 = tail call i8* @strerror(i32 %42)
%44 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i8* %41, i8* %43)
%45 = tail call i32 @close(i32 %24)
br label %54
46: ; preds = %35, %46
%47 = phi i32 [ %49, %46 ], [ %36, %35 ]
%48 = tail call i32 @putchar(i32 %47)
%49 = tail call i32 @getc(i32* %33)
%50 = load i32, i32* @EOF
%51 = icmp eq i32 %49, %50
br i1 %51, label %52, label %46
52: ; preds = %46, %35
%53 = tail call i32 @fclose(i32* %33)
br label %54
54: ; preds = %39, %52, %26
%55 = add nuw nsw i64 %20, 1
%56 = icmp eq i64 %55, %14
br i1 %56, label %57, label %19
57: ; preds = %54
ret i32 0
}