#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 pid_t;

/* Variables and functions */
scalar_t__ ceil(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
char *getPidDir(char const *, int);
int /*<<< orphan*/ log10(int);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int writeToFile(char *, char *, char *);
int addToTasksFile(const char *cg, pid_t pid) {
  char *pid_dir = getPidDir(cg, pid);
  char *buf = malloc((int)ceil(log10(pid)) + 1);
  sprintf(buf, "%d", pid);
  if (writeToFile(pid_dir, "tasks", buf) == -1) {
    do {
      if (1) {
        fprintf(stderr, "%s: %d: ",
                "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                "outputs/2022-01-22/02-19-57/repos/CRUx-BPHC/crux-judge/refs/"
                "heads/development/src/resource_limits.c",
                130);
        fprintf(stderr, "writeToFile failed");
        fprintf(stderr, "\n");
      }
    } while (0);
    free(pid_dir);
    free(buf);
    return -1;
  }
  free(pid_dir);
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<addToTasksFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1080 <getPidDir@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1090 <log10@plt>
mov    %eax,%edi
call   1050 <ceil@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe36(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xe25(%rip),%rsi
call   1040 <writeToFile@plt>
cmp    $0xffffffff,%eax
jne    1260 <addToTasksFile+0xe0>
jmp    11f1 <addToTasksFile+0x71>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe08(%rip),%rsi
lea    0xe0a(%rip),%rdx
mov    $0x82,%ecx
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
lea    0xe8a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    0xe86(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <addToTasksFile+0xf9>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addToTasksFile>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
call   1080 <getPidDir@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1090 <log10@plt>
mov    %eax,%edi
call   1050 <ceil@plt>
lea    0x1(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
lea    0xe52(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   10a0 <sprintf@plt>
lea    0xe44(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1040 <writeToFile@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
jne    121c <addToTasksFile+0x9c>
mov    0x2e08(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xe27(%rip),%rsi
lea    0xe29(%rip),%rdx
mov    $0x82,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x0(%rbp),%edi
lea    0xeaf(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x0(%rbp),%edi
lea    0xeb1(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [6 x i8] c"tasks\00"
@stderr = global i32 0
@.str.2 = [9 x i8] c"%s: %d: \00"
@.str.3 = [156 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/CRUx-BPHC/crux-judge/refs/heads/development/src/resource_limits.c\00"
@.str.4 = [19 x i8] c"writeToFile failed\00"
@.str.5 = [2 x i8] c"\0A\00"
define i32 @addToTasksFile(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = load i32, i32* %5
%10 = call i8* @getPidDir(i8* %8, i32 %9)
store i8* %10, i8** %6
%11 = load i32, i32* %5
%12 = call i32 @log10(i32 %11)
%13 = call i64 @ceil(i32 %12)
%14 = trunc i64 %13 to i32
%15 = add nsw i32 %14, 1
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %7
%17 = load i8*, i8** %7
%18 = load i32, i32* %5
%19 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = load i8*, i8** %6
%21 = load i8*, i8** %7
%22 = call i32 @writeToFile(i8* %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %37
24: ; preds = %2
br label %25
25: ; preds = %24
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([156 x i8], [156 x i8]* @.str.3, i64 0, i64 0), i32 130)
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %32
32: ; preds = %25
%33 = load i8*, i8** %6
%34 = call i32 @free(i8* %33)
%35 = load i8*, i8** %7
%36 = call i32 @free(i8* %35)
store i32 -1, i32* %3
br label %42
37: ; preds = %2
%38 = load i8*, i8** %6
%39 = call i32 @free(i8* %38)
%40 = load i8*, i8** %7
%41 = call i32 @free(i8* %40)
store i32 0, i32* %3
br label %42
42: ; preds = %37, %32
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [6 x i8] c"tasks\00"
@stderr = global i32 0
@.str.2 = [9 x i8] c"%s: %d: \00"
@.str.3 = [156 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/CRUx-BPHC/crux-judge/refs/heads/development/src/resource_limits.c\00"
@.str.4 = [19 x i8] c"writeToFile failed\00"
@.str.5 = [2 x i8] c"\0A\00"
define i32 @addToTasksFile(i8* %0, i32 %1) {
%3 = tail call i8* @getPidDir(i8* %0, i32 %1)
%4 = tail call i32 @log10(i32 %1)
%5 = tail call i64 @ceil(i32 %4)
%6 = trunc i64 %5 to i32
%7 = add nsw i32 %6, 1
%8 = tail call i8* @malloc(i32 %7)
%9 = tail call i32 @sprintf(i8* dereferenceable(1) %8, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%10 = tail call i32 @writeToFile(i8* %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([156 x i8], [156 x i8]* @.str.3, i64 0, i64 0), i32 130)
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %19
19: ; preds = %2, %12
%20 = phi i32 [ -1, %12 ], [ 0, %2 ]
%21 = tail call i32 @free(i8* %3)
%22 = tail call i32 @free(i8* %8)
ret i32 %20
}