#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 /*<<< orphan*/ d_close(int);
int d_open(char *, char *, char *);
int d_read(int, char *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *getenv(char *);
int /*<<< orphan*/ printf(char *, char *);
int sscanf(char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd, rc;
  char buffer[1024] = "", maquina_y_puerto[255] = "", ruta[255] = "", *entorno;
  if (argc != 2) {
    printf("%s", "Uso: dcat ruta\n");
    return -1;
  }
  if ((entorno = getenv("DSERVER")) == NULL)
    fprintf(stderr, "Variable de entorno DSERVER no definida\n");
  strncpy(maquina_y_puerto, entorno, strlen(entorno));
  if (sscanf(argv[1], "%s", ruta) != 1) {
    printf("%s", "Uso: dcat ruta\n");
    return -1;
  }
  fd = d_open(maquina_y_puerto, ruta, "r");
  if (fd == -1)
    return -1;
  while ((rc = d_read(fd, buffer, 1024 - 1)) > 0) {
    buffer[rc] = 0;
    printf("%s", buffer);
  }
  d_close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x640,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    -0x520(%rbp),%rdi
xor    %esi,%esi
mov    $0xff,%edx
call   1070 <memset@plt>
lea    -0x620(%rbp),%rdi
xor    %esi,%esi
mov    $0xff,%edx
call   1070 <memset@plt>
cmpl   $0x2,-0x8(%rbp)
je     121b <main+0x7b>
lea    0xdfd(%rip),%rdi
lea    0xdf9(%rip),%rsi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1358 <main+0x1b8>
lea    0xdf1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x628(%rbp)
cmp    $0x0,%rax
jne    124d <main+0xad>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
lea    0xdd3(%rip),%rsi
call   1080 <fprintf@plt>
lea    -0x520(%rbp),%rax
mov    %rax,-0x638(%rbp)
mov    -0x628(%rbp),%rax
mov    %rax,-0x630(%rbp)
mov    -0x628(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x638(%rbp),%rdi
mov    -0x630(%rbp),%rsi
mov    %eax,%edx
call   1040 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x620(%rbp),%rdx
lea    0xd60(%rip),%rsi
call   1090 <sscanf@plt>
cmp    $0x1,%eax
je     12cd <main+0x12d>
lea    0xd4b(%rip),%rdi
lea    0xd47(%rip),%rsi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1358 <main+0x1b8>
lea    -0x520(%rbp),%rdi
lea    -0x620(%rbp),%rsi
lea    0xd62(%rip),%rdx
call   10c0 <d_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1300 <main+0x160>
movl   $0xffffffff,-0x4(%rbp)
jmp    1358 <main+0x1b8>
jmp    1305 <main+0x165>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x3ff,%edx
call   10a0 <d_read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1349 <main+0x1a9>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rsi
lea    0xcc1(%rip),%rdi
call   1060 <printf@plt>
jmp    1305 <main+0x165>
mov    -0x14(%rbp),%edi
call   10b0 <d_close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x640,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x600,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x200(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1070 <memset@plt>
xorps  %xmm0,%xmm0
movups %xmm0,0x1ef(%rsp)
movaps %xmm0,0x1e0(%rsp)
movaps %xmm0,0x1d0(%rsp)
movaps %xmm0,0x1c0(%rsp)
movaps %xmm0,0x1b0(%rsp)
movaps %xmm0,0x1a0(%rsp)
movaps %xmm0,0x190(%rsp)
movaps %xmm0,0x180(%rsp)
movaps %xmm0,0x170(%rsp)
movaps %xmm0,0x160(%rsp)
movaps %xmm0,0x150(%rsp)
movaps %xmm0,0x140(%rsp)
movaps %xmm0,0x130(%rsp)
movaps %xmm0,0x120(%rsp)
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
movups %xmm0,0xef(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
cmp    $0x2,%ebp
jne    1393 <main+0x1f3>
lea    0xd55(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    12e0 <main+0x140>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
lea    0xd40(%rip),%rsi
call   1080 <fprintf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x100(%rsp),%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <strncpy@plt>
mov    0x8(%rbx),%rdi
lea    0xcfb(%rip),%rsi
mov    %rsp,%rdx
call   1090 <sscanf@plt>
cmp    $0x1,%eax
jne    1393 <main+0x1f3>
lea    0xd27(%rip),%rdx
lea    0x100(%rsp),%rdi
mov    %rsp,%rsi
call   10c0 <d_open@plt>
cmp    $0xffffffff,%eax
je     13a6 <main+0x206>
mov    %eax,%ebx
lea    0x200(%rsp),%rsi
mov    %eax,%edi
mov    $0x3ff,%edx
call   10a0 <d_read@plt>
test   %eax,%eax
jle    1388 <main+0x1e8>
lea    0xcad(%rip),%r14
lea    0x200(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    %eax,%eax
movb   $0x0,0x200(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbp,%rsi
call   1060 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x3ff,%edx
call   10a0 <d_read@plt>
test   %eax,%eax
jg     1360 <main+0x1c0>
mov    %ebx,%edi
call   10b0 <d_close@plt>
xor    %eax,%eax
jmp    13ab <main+0x20b>
lea    0xc66(%rip),%rdi
lea    0xc62(%rip),%rsi
call   1060 <printf@plt>
mov    $0xffffffff,%eax
add    $0x600,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [16 x i8] c"Uso: dcat ruta\0A\00"
@.str.2 = [8 x i8] c"DSERVER\00"
@stderr = global i32 0
@.str.3 = [41 x i8] c"Variable de entorno DSERVER no definida\0A\00"
@.str.4 = [2 x i8] c"r\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [1024 x i8]
%9 = alloca [255 x i8]
%10 = alloca [255 x i8]
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [1024 x i8]* %8 to i8*
%13 = bitcast [255 x i8]* %9 to i8*
%14 = bitcast [255 x i8]* %10 to i8*
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 2
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %61
19: ; preds = %2
%20 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i8* %20, i8** %11
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %22, %19
%26 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%27 = load i8*, i8** %11
%28 = load i8*, i8** %11
%29 = call i32 @strlen(i8* %28)
%30 = call i32 @strncpy(i8* %26, i8* %27, i32 %29)
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%35 = call i32 @sscanf(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %34)
%36 = icmp ne i32 %35, 1
br i1 %36, label %37, label %39
37: ; preds = %25
%38 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %61
39: ; preds = %25
%40 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%41 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%42 = call i32 @d_open(i8* %40, i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %46
45: ; preds = %39
store i32 -1, i32* %3
br label %61
46: ; preds = %39
br label %47
47: ; preds = %52, %46
%48 = load i32, i32* %6
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%50 = call i32 @d_read(i32 %48, i8* %49, i32 1023)
store i32 %50, i32* %7
%51 = icmp sgt i32 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %47
%53 = load i32, i32* %7
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %54
store i8 0, i8* %55
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%57 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %56)
br label %47
58: ; preds = %47
%59 = load i32, i32* %6
%60 = call i32 @d_close(i32 %59)
store i32 0, i32* %3
br label %61
61: ; preds = %58, %45, %37, %17
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [16 x i8] c"Uso: dcat ruta\0A\00"
@.str.2 = [8 x i8] c"DSERVER\00"
@stderr = global i32 0
@.str.3 = [41 x i8] c"Variable de entorno DSERVER no definida\0A\00"
@.str.4 = [2 x i8] c"r\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca [255 x i8]
%5 = alloca [255 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%9 = icmp eq i32 %0, 2
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %42
12: ; preds = %2
%13 = tail call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %15, %12
%19 = tail call i32 @strlen(i8* %13)
%20 = call i32 @strncpy(i8* %7, i8* %13, i32 %19)
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @sscanf(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %8)
%24 = icmp eq i32 %23, 1
br i1 %24, label %27, label %25
25: ; preds = %18
%26 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %42
27: ; preds = %18
%28 = call i32 @d_open(i8* %7, i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%29 = icmp eq i32 %28, -1
br i1 %29, label %42, label %30
30: ; preds = %27
%31 = call i32 @d_read(i32 %28, i8* %6, i32 1023)
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %30, %33
%34 = phi i32 [ %38, %33 ], [ %31, %30 ]
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %35
store i8 0, i8* %36
%37 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %6)
%38 = call i32 @d_read(i32 %28, i8* %6, i32 1023)
%39 = icmp sgt i32 %38, 0
br i1 %39, label %33, label %40
40: ; preds = %33, %30
%41 = call i32 @d_close(i32 %28)
br label %42
42: ; preds = %27, %40, %25, %10
%43 = phi i32 [ -1, %10 ], [ -1, %25 ], [ 0, %40 ], [ -1, %27 ]
ret i32 %43
}