#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 getpid();
int mi_creat(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int write_randomly(char *);
int writer(int id, char *dir) {
  char filepath[100];
  sprintf(filepath, "%sproceso_%d/", dir, getpid());
  int resultado;
  resultado = mi_creat(filepath, 7);
  if (resultado < 0) {
    return resultado;
  }
  strcat(filepath, "prueba.dat");
  resultado = mi_creat(filepath, 7);
  if (resultado < 0) {
    return resultado;
  }
  resultado = write_randomly(filepath);
  if (resultado < 0) {
    return resultado;
  }
  return 0;
}


x86 assembly (clang, -O0):
<writer>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x98(%rbp),%rdi
mov    -0x90(%rbp),%rdx
mov    %eax,%ecx
lea    0xe6a(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x80(%rbp),%rdi
mov    $0x7,%esi
call   1040 <mi_creat@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jge    11ca <writer+0x7a>
mov    -0x84(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    123a <writer+0xea>
lea    -0x80(%rbp),%rdi
lea    0xe39(%rip),%rsi
call   1060 <strcat@plt>
lea    -0x80(%rbp),%rdi
mov    $0x7,%esi
call   1040 <mi_creat@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jge    1209 <writer+0xb9>
mov    -0x84(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    123a <writer+0xea>
lea    -0x80(%rbp),%rdi
call   1050 <write_randomly@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jge    1233 <writer+0xe3>
mov    -0x84(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    123a <writer+0xea>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writer>:
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %rsi,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe98(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1070 <sprintf@plt>
mov    %r14,%rdi
mov    $0x7,%esi
call   1040 <mi_creat@plt>
test   %eax,%eax
js     11bb <writer+0x6b>
lea    0xe7e(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1060 <strcat@plt>
mov    %rbx,%rdi
mov    $0x7,%esi
call   1040 <mi_creat@plt>
test   %eax,%eax
js     11bb <writer+0x6b>
mov    %rsp,%rdi
call   1050 <write_randomly@plt>
mov    %eax,%ecx
sar    $0x1f,%eax
and    %ecx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [14 x i8] c"%sproceso_%d/\00"
@.str.1 = [11 x i8] c"prueba.dat\00"
define i32 @writer(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [100 x i8]
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%9 = load i8*, i8** %5
%10 = call i32 (...) @getpid()
%11 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %9, i32 %10)
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%13 = call i32 @mi_creat(i8* %12, i32 7)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = load i32, i32* %7
store i32 %17, i32* %3
br label %35
18: ; preds = %2
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%20 = call i32 @strcat(i8* %19, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%22 = call i32 @mi_creat(i8* %21, i32 7)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = load i32, i32* %7
store i32 %26, i32* %3
br label %35
27: ; preds = %18
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%29 = call i32 @write_randomly(i8* %28)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = load i32, i32* %7
store i32 %33, i32* %3
br label %35
34: ; preds = %27
store i32 0, i32* %3
br label %35
35: ; preds = %34, %32, %25, %16
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@.str = [14 x i8] c"%sproceso_%d/\00"
@.str.1 = [11 x i8] c"prueba.dat\00"
define i32 @writer(i32 %0, i8* %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (...) @getpid()
%6 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %5)
%7 = call i32 @mi_creat(i8* %4, i32 7)
%8 = icmp slt i32 %7, 0
br i1 %8, label %17, label %9
9: ; preds = %2
%10 = call i32 @strcat(i8* %4, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @mi_creat(i8* %4, i32 7)
%12 = icmp slt i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %9
%14 = call i32 @write_randomly(i8* %4)
%15 = icmp slt i32 %14, 0
%16 = select i1 %15, i32 %14, i32 0
br label %17
17: ; preds = %13, %9, %2
%18 = phi i32 [ %7, %2 ], [ %11, %9 ], [ %16, %13 ]
ret i32 %18
}