#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*/ close(int);
int envia(int, char *, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int pollIn(int, int);
int recibe(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int r_close(int fd) {
  int rc, r_errno;
  char cadena[300] = "";
  sprintf(cadena, "r_close\n");
  if (envia(fd, cadena, strlen(cadena)) == -1) {
    perror("r_close(): envia()");
    return -1;
  }
  rc = pollIn(fd, 3000);
  if (rc == 1) {
    fprintf(stderr, "r_seek(): pollIn(): timeout\n");
    return -1;
  }
  rc = 0;
  while (rc == 0) {
    rc = recibe(fd, cadena, 300);
    if (rc == -1) {
      perror("r_seek(): recibe()");
      return -1;
    }
  }
  sscanf(cadena, "%d", &r_errno);
  if (r_errno != 0) {
    errno = r_errno;
    close(fd);
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<r_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x140(%rbp),%rdi
xor    %esi,%esi
mov    $0x12c,%edx
call   1060 <memset@plt>
lea    -0x140(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14c(%rbp)
lea    -0x140(%rbp),%rax
mov    %rax,-0x148(%rbp)
lea    -0x140(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x14c(%rbp),%edi
mov    -0x148(%rbp),%rsi
mov    %eax,%edx
call   1040 <envia@plt>
cmp    $0xffffffff,%eax
jne    122c <r_close+0x8c>
lea    0xdee(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1306 <r_close+0x166>
mov    -0x8(%rbp),%edi
mov    $0xbb8,%esi
call   1070 <pollIn@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    1267 <r_close+0xc7>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xdc6(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1306 <r_close+0x166>
movl   $0x0,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    12b6 <r_close+0x116>
mov    -0x8(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0x12c,%edx
call   1030 <recibe@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    12b1 <r_close+0x111>
lea    0xd99(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1306 <r_close+0x166>
jmp    126e <r_close+0xce>
lea    -0x140(%rbp),%rdi
lea    0xd88(%rip),%rsi
lea    -0x10(%rbp),%rdx
call   10a0 <sscanf@plt>
cmpl   $0x0,-0x10(%rbp)
je     12f7 <r_close+0x157>
mov    -0x10(%rbp),%ecx
mov    0x2cf7(%rip),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1306 <r_close+0x166>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<r_close>:
push   %r14
push   %rbx
sub    $0x148,%rsp
mov    %edi,%ebx
lea    0x19(%rsp),%rdi
mov    $0x123,%edx
xor    %esi,%esi
call   1060 <memset@plt>
movabs $0xa65736f6c635f72,%rax
73 65 0a
mov    %rax,0x10(%rsp)
movb   $0x0,0x18(%rsp)
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <envia@plt>
cmp    $0xffffffff,%eax
je     127c <r_close+0xec>
mov    %ebx,%edi
mov    $0xbb8,%esi
call   1070 <pollIn@plt>
cmp    $0x1,%eax
jne    120b <r_close+0x7b>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
call   1090 <fprintf@plt>
jmp    1288 <r_close+0xf8>
xor    %eax,%eax
lea    0x10(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %eax,%eax
jne    1241 <r_close+0xb1>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x12c,%edx
call   1030 <recibe@plt>
cmp    $0xffffffff,%eax
jne    1220 <r_close+0x90>
lea    0xdfa(%rip),%rdi
jmp    1283 <r_close+0xf3>
lea    0xe04(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rdx
call   10a0 <sscanf@plt>
mov    0xc(%rsp),%eax
test   %eax,%eax
je     1271 <r_close+0xe1>
mov    0x2d72(%rip),%rcx
mov    %eax,(%rcx)
mov    %ebx,%edi
call   1080 <close@plt>
jmp    1288 <r_close+0xf8>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
jmp    128d <r_close+0xfd>
lea    0xd86(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%eax
add    $0x148,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [9 x i8] c"r_close\0A\00"
@.str.1 = [19 x i8] c"r_close(): envia()\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"r_seek(): pollIn(): timeout\0A\00"
@.str.3 = [19 x i8] c"r_seek(): recibe()\00"
@.str.4 = [3 x i8] c"%d\00"
@errno = global i32 0
define i32 @r_close(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [300 x i8]
store i32 %0, i32* %3
%7 = bitcast [300 x i8]* %6 to i8*
%8 = getelementptr inbounds [300 x i8], [300 x i8]* %6, i64 0, i64 0
%9 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* %3
%11 = getelementptr inbounds [300 x i8], [300 x i8]* %6, i64 0, i64 0
%12 = getelementptr inbounds [300 x i8], [300 x i8]* %6, i64 0, i64 0
%13 = call i32 @strlen(i8* %12)
%14 = call i32 @envia(i32 %10, i8* %11, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %51
18: ; preds = %1
%19 = load i32, i32* %3
%20 = call i32 @pollIn(i32 %19, i32 3000)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %51
26: ; preds = %18
store i32 0, i32* %4
br label %27
27: ; preds = %38, %26
%28 = load i32, i32* %4
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %39
30: ; preds = %27
%31 = load i32, i32* %3
%32 = getelementptr inbounds [300 x i8], [300 x i8]* %6, i64 0, i64 0
%33 = call i32 @recibe(i32 %31, i8* %32, i32 300)
store i32 %33, i32* %4
%34 = load i32, i32* %4
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %51
38: ; preds = %30
br label %27
39: ; preds = %27
%40 = getelementptr inbounds [300 x i8], [300 x i8]* %6, i64 0, i64 0
%41 = call i32 @sscanf(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %5)
%42 = load i32, i32* %5
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %39
%45 = load i32, i32* %5
store i32 %45, i32* @errno
%46 = load i32, i32* %3
%47 = call i32 @close(i32 %46)
store i32 -1, i32* %2
br label %51
48: ; preds = %39
%49 = load i32, i32* %3
%50 = call i32 @close(i32 %49)
store i32 0, i32* %2
br label %51
51: ; preds = %48, %44, %36, %23, %16
%52 = load i32, i32* %2
ret i32 %52
}

LLVM IR (-O2):
@.str = [9 x i8] c"r_close\0A\00"
@.str.1 = [19 x i8] c"r_close(): envia()\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"r_seek(): pollIn(): timeout\0A\00"
@.str.3 = [19 x i8] c"r_seek(): recibe()\00"
@.str.4 = [3 x i8] c"%d\00"
@errno = global i32 0
define i32 @r_close(i32 %0) {
%2 = alloca i32
%3 = alloca [300 x i8]
%4 = bitcast i32* %2 to i8*
%5 = getelementptr inbounds [300 x i8], [300 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [300 x i8], [300 x i8]* %3, i64 0, i64 9
%7 = call i32 @strlen(i8* %5)
%8 = call i32 @envia(i32 %0, i8* %5, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %34
12: ; preds = %1
%13 = call i32 @pollIn(i32 %0, i32 3000)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %34
18: ; preds = %12, %21
%19 = phi i32 [ %22, %21 ], [ 0, %12 ]
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = call i32 @recibe(i32 %0, i8* %5, i32 300)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %18
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %34
26: ; preds = %18
%27 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %2)
%28 = load i32, i32* %2
%29 = icmp eq i32 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %26
store i32 %28, i32* @errno
%31 = call i32 @close(i32 %0)
br label %34
32: ; preds = %26
%33 = call i32 @close(i32 %0)
br label %34
34: ; preds = %32, %30, %24, %15, %10
%35 = phi i32 [ -1, %10 ], [ -1, %15 ], [ -1, %24 ], [ -1, %30 ], [ 0, %32 ]
ret i32 %35
}