#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 EINVAL;
int EPERM;
int /*<<< orphan*/ envia(int, char *, int /*<<< orphan*/);
int errno;
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ fwrite(void *, unsigned long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, unsigned long *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int petic_rwrite(int sockCliente, FILE *pFich, char *argLong, void *datos,
                 int modoAcceso) {
  int error;
  char cadena[300];
  unsigned long longitud;
  if (fileno(pFich) == -1) {
    fprintf(stderr, "petic_rwrite(): no hay fichero abierto!\n");
    return -1;
  } else if ((argLong == NULL) || (datos == NULL) ||
             (!sscanf(argLong, "%lu", &longitud))) {
    error = EINVAL;
    fprintf(
        stderr,
        "petic_rwrite(): esperaba 2 argumentos: bytes que escribir y datos\n");
    sprintf(cadena, "%d\n", error);
    envia(sockCliente, cadena, strlen(cadena));
    return -1;
  }
  if (modoAcceso != 1) {
    error = EPERM;
    fprintf(stderr,
            "petic_rwrite(): el fichero no esta abierto en modo escritura\n");
    sprintf(cadena, "%d\n", error);
    envia(sockCliente, cadena, strlen(cadena));
    return -1;
  }
  if (fwrite(datos, longitud, 1, pFich) == 0) {
    error = errno;
    perror("petic_rwrite(): fwrite()");
    sprintf(cadena, "%d\n", error);
    envia(sockCliente, cadena, strlen(cadena));
    return -1;
  }
  error = 0;
  sprintf(cadena, "%d\n", error);
  envia(sockCliente, cadena, strlen(cadena));
  return 0;
}


x86 assembly (clang, -O0):
<petic_rwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1b0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
cmp    $0xffffffff,%eax
jne    11d1 <petic_rwrite+0x51>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f9 <petic_rwrite+0x279>
cmpq   $0x0,-0x18(%rbp)
je     1207 <petic_rwrite+0x87>
cmpq   $0x0,-0x20(%rbp)
je     1207 <petic_rwrite+0x87>
mov    -0x18(%rbp),%rdi
lea    0xe37(%rip),%rsi
lea    -0x168(%rbp),%rdx
call   1070 <sscanf@plt>
cmp    $0x0,%eax
jne    1281 <petic_rwrite+0x101>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
call   1050 <fprintf@plt>
lea    -0x160(%rbp),%rdi
mov    -0x28(%rbp),%edx
lea    0xe37(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x174(%rbp)
lea    -0x160(%rbp),%rax
mov    %rax,-0x170(%rbp)
lea    -0x160(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x174(%rbp),%edi
mov    -0x170(%rbp),%rsi
mov    %eax,%edx
call   1030 <envia@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f9 <petic_rwrite+0x279>
jmp    1286 <petic_rwrite+0x106>
cmpl   $0x1,-0x24(%rbp)
je     130a <petic_rwrite+0x18a>
mov    0x2d29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
lea    0xdc8(%rip),%rsi
call   1050 <fprintf@plt>
lea    -0x160(%rbp),%rdi
mov    -0x28(%rbp),%edx
lea    0xdae(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x184(%rbp)
lea    -0x160(%rbp),%rax
mov    %rax,-0x180(%rbp)
lea    -0x160(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x184(%rbp),%edi
mov    -0x180(%rbp),%rsi
mov    %eax,%edx
call   1030 <envia@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f9 <petic_rwrite+0x279>
mov    -0x20(%rbp),%rdi
mov    -0x168(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   10a0 <fwrite@plt>
cmp    $0x0,%rax
jne    139e <petic_rwrite+0x21e>
mov    0x2ca4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
lea    0xd72(%rip),%rdi
call   1080 <perror@plt>
lea    -0x160(%rbp),%rdi
mov    -0x28(%rbp),%edx
lea    0xd1a(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x194(%rbp)
lea    -0x160(%rbp),%rax
mov    %rax,-0x190(%rbp)
lea    -0x160(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x194(%rbp),%edi
mov    -0x190(%rbp),%rsi
mov    %eax,%edx
call   1030 <envia@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f9 <petic_rwrite+0x279>
movl   $0x0,-0x28(%rbp)
lea    -0x160(%rbp),%rdi
mov    -0x28(%rbp),%edx
lea    0xcba(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1a4(%rbp)
lea    -0x160(%rbp),%rax
mov    %rax,-0x1a0(%rbp)
lea    -0x160(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x1a4(%rbp),%edi
mov    -0x1a0(%rbp),%rsi
mov    %eax,%edx
call   1030 <envia@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<petic_rwrite>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x140,%rsp
mov    %r8d,%r15d
mov    %rcx,%r12
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %rsi,%rdi
call   1060 <fileno@plt>
cmp    $0xffffffff,%eax
je     1273 <petic_rwrite+0xf3>
test   %rbp,%rbp
je     1228 <petic_rwrite+0xa8>
test   %r12,%r12
je     1228 <petic_rwrite+0xa8>
lea    0xe69(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rbp,%rdi
call   1070 <sscanf@plt>
test   %eax,%eax
je     1228 <petic_rwrite+0xa8>
cmp    $0x1,%r15d
jne    129f <petic_rwrite+0x11f>
mov    0x8(%rsp),%rsi
mov    %r12,%rdi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10a0 <fwrite@plt>
test   %rax,%rax
je     12ba <petic_rwrite+0x13a>
lea    0xe70(%rip),%rsi
lea    0x10(%rsp),%rbp
xor    %ebx,%ebx
mov    %rbp,%rdi
xor    %edx,%edx
call   1090 <sprintf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <envia@plt>
jmp    128d <petic_rwrite+0x10d>
mov    0x2da1(%rip),%rax
mov    (%rax),%ebx
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe23(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %ebx,%edx
call   1090 <sprintf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <envia@plt>
jmp    1288 <petic_rwrite+0x108>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
lea    0xd7d(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x140,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d1a(%rip),%rax
mov    (%rax),%ebx
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
jmp    1241 <petic_rwrite+0xc1>
mov    0x2d17(%rip),%rax
mov    (%rax),%ebx
lea    0xde8(%rip),%rdi
call   1080 <perror@plt>
jmp    1246 <petic_rwrite+0xc6>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [41 x i8] c"petic_rwrite(): no hay fichero abierto!\0A\00"
@.str.1 = [4 x i8] c"%lu\00"
@EINVAL = global i32 0
@.str.2 = [67 x i8] c"petic_rwrite(): esperaba 2 argumentos: bytes que escribir y datos\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@EPERM = global i32 0
@.str.4 = [62 x i8] c"petic_rwrite(): el fichero no esta abierto en modo escritura\0A\00"
@errno = global i32 0
@.str.5 = [25 x i8] c"petic_rwrite(): fwrite()\00"
define i32 @petic_rwrite(i32 %0, i32* %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca [300 x i8]
%14 = alloca i64
store i32 %0, i32* %7
store i32* %1, i32** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%15 = load i32*, i32** %8
%16 = call i32 @fileno(i32* %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %5
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %85
21: ; preds = %5
%22 = load i8*, i8** %9
%23 = icmp eq i8* %22, null
br i1 %23, label %31, label %24
24: ; preds = %21
%25 = load i8*, i8** %10
%26 = icmp eq i8* %25, null
br i1 %26, label %31, label %27
27: ; preds = %24
%28 = load i8*, i8** %9
%29 = call i32 @sscanf(i8* %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %14)
%30 = icmp ne i32 %29, 0
br i1 %30, label %43, label %31
31: ; preds = %27, %24, %21
%32 = load i32, i32* @EINVAL
store i32 %32, i32* %12
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.2, i64 0, i64 0))
%35 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%36 = load i32, i32* %12
%37 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = load i32, i32* %7
%39 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%40 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%41 = call i32 @strlen(i8* %40)
%42 = call i32 @envia(i32 %38, i8* %39, i32 %41)
store i32 -1, i32* %6
br label %85
43: ; preds = %27
br label %44
44: ; preds = %43
%45 = load i32, i32* %11
%46 = icmp ne i32 %45, 1
br i1 %46, label %47, label %59
47: ; preds = %44
%48 = load i32, i32* @EPERM
store i32 %48, i32* %12
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.4, i64 0, i64 0))
%51 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%52 = load i32, i32* %12
%53 = call i32 @sprintf(i8* %51, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %52)
%54 = load i32, i32* %7
%55 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%56 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%57 = call i32 @strlen(i8* %56)
%58 = call i32 @envia(i32 %54, i8* %55, i32 %57)
store i32 -1, i32* %6
br label %85
59: ; preds = %44
%60 = load i8*, i8** %10
%61 = load i64, i64* %14
%62 = load i32*, i32** %8
%63 = call i64 @fwrite(i8* %60, i64 %61, i32 1, i32* %62)
%64 = icmp eq i64 %63, 0
br i1 %64, label %65, label %76
65: ; preds = %59
%66 = load i32, i32* @errno
store i32 %66, i32* %12
%67 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%68 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%69 = load i32, i32* %12
%70 = call i32 @sprintf(i8* %68, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %69)
%71 = load i32, i32* %7
%72 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%73 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%74 = call i32 @strlen(i8* %73)
%75 = call i32 @envia(i32 %71, i8* %72, i32 %74)
store i32 -1, i32* %6
br label %85
76: ; preds = %59
store i32 0, i32* %12
%77 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%78 = load i32, i32* %12
%79 = call i32 @sprintf(i8* %77, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %78)
%80 = load i32, i32* %7
%81 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%82 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%83 = call i32 @strlen(i8* %82)
%84 = call i32 @envia(i32 %80, i8* %81, i32 %83)
store i32 0, i32* %6
br label %85
85: ; preds = %76, %65, %47, %31, %18
%86 = load i32, i32* %6
ret i32 %86
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [41 x i8] c"petic_rwrite(): no hay fichero abierto!\0A\00"
@.str.1 = [4 x i8] c"%lu\00"
@EINVAL = global i32 0
@.str.2 = [67 x i8] c"petic_rwrite(): esperaba 2 argumentos: bytes que escribir y datos\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@EPERM = global i32 0
@.str.4 = [62 x i8] c"petic_rwrite(): el fichero no esta abierto en modo escritura\0A\00"
@errno = global i32 0
@.str.5 = [25 x i8] c"petic_rwrite(): fwrite()\00"
define i32 @petic_rwrite(i32 %0, i32* nocapture %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca [300 x i8]
%7 = alloca i64
%8 = getelementptr inbounds [300 x i8], [300 x i8]* %6, i64 0, i64 0
%9 = bitcast i64* %7 to i8*
%10 = tail call i32 @fileno(i32* %1)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %5
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %52
15: ; preds = %5
%16 = icmp eq i8* %2, null
%17 = icmp eq i8* %3, null
%18 = or i1 %16, %17
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = call i32 @sscanf(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %7)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %19, %15
%23 = load i32, i32* @EINVAL
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%27 = call i32 @strlen(i8* %8)
%28 = call i32 @envia(i32 %0, i8* %8, i32 %27)
br label %52
29: ; preds = %19
%30 = icmp eq i32 %4, 1
br i1 %30, label %38, label %31
31: ; preds = %29
%32 = load i32, i32* @EPERM
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.4, i64 0, i64 0))
%35 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%36 = call i32 @strlen(i8* %8)
%37 = call i32 @envia(i32 %0, i8* %8, i32 %36)
br label %52
38: ; preds = %29
%39 = load i64, i64* %7
%40 = call i64 @fwrite(i8* %3, i64 %39, i32 1, i32* %1)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %38
%43 = load i32, i32* @errno
%44 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%45 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %43)
%46 = call i32 @strlen(i8* %8)
%47 = call i32 @envia(i32 %0, i8* %8, i32 %46)
br label %52
48: ; preds = %38
%49 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 0)
%50 = call i32 @strlen(i8* %8)
%51 = call i32 @envia(i32 %0, i8* %8, i32 %50)
br label %52
52: ; preds = %48, %42, %31, %22, %12
%53 = phi i32 [ -1, %12 ], [ -1, %22 ], [ -1, %31 ], [ -1, %42 ], [ 0, %48 ]
ret i32 %53
}