#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*/ pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
char *strchr(char *, char);
void write_at_log(char *name, int i, pid_t cpid) {
  char folder[255];
  char *tmp = strchr(name, '/');
  while (tmp != NULL) {
    name = tmp + 1;
    tmp = strchr(name, '/');
  }
  sprintf(folder, "/tmp/%s%d.pid", name, i);
  FILE *fp = fopen(folder, "w");
  if (fp != NULL) {
    fwrite(&cpid, sizeof(pid_t), 1, fp);
    fclose(fp);
  }
}


x86 assembly (clang, -O0):
<write_at_log>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
je     11b1 <write_at_log+0x61>
mov    -0x118(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,-0x118(%rbp)
jmp    117a <write_at_log+0x2a>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
lea    0xe3a(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x110(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x120(%rbp)
cmpq   $0x0,-0x120(%rbp)
je     1219 <write_at_log+0xc9>
mov    -0x120(%rbp),%rcx
lea    -0x10(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    -0x120(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_at_log>:
push   %rbp
push   %r14
push   %rbx
sub    $0x110,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    %edx,0xc(%rsp)
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x2f,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     118b <write_at_log+0x3b>
mov    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
jmp    1170 <write_at_log+0x20>
lea    0xe6e(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %rbx,%rdx
mov    %r14d,%ecx
call   1060 <sprintf@plt>
lea    0xe62(%rip),%rsi
mov    %rbp,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11e6 <write_at_log+0x96>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
add    $0x110,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <fclose@plt>
add    $0x110,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/tmp/%s%d.pid\00"
@.str.1 = [2 x i8] c"w\00"
define void @write_at_log(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [255 x i8]
%8 = alloca i8*
%9 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i8*, i8** %4
%11 = call i8* @strchr(i8* %10, i8 signext 47)
store i8* %11, i8** %8
br label %12
12: ; preds = %15, %3
%13 = load i8*, i8** %8
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i8*, i8** %8
%17 = getelementptr inbounds i8, i8* %16, i64 1
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = call i8* @strchr(i8* %18, i8 signext 47)
store i8* %19, i8** %8
br label %12
20: ; preds = %12
%21 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%22 = load i8*, i8** %4
%23 = load i32, i32* %5
%24 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %22, i32 %23)
%25 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %9
%27 = load i32*, i32** %9
%28 = icmp ne i32* %27, null
br i1 %28, label %29, label %34
29: ; preds = %20
%30 = load i32*, i32** %9
%31 = call i32 @fwrite(i32* %6, i32 4, i32 1, i32* %30)
%32 = load i32*, i32** %9
%33 = call i32 @fclose(i32* %32)
br label %34
34: ; preds = %29, %20
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"/tmp/%s%d.pid\00"
@.str.1 = [2 x i8] c"w\00"
define void @write_at_log(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca [255 x i8]
store i32 %2, i32* %4
%6 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%7 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 47)
%8 = icmp eq i8* %7, null
br i1 %8, label %14, label %9
9: ; preds = %3, %9
%10 = phi i8* [ %12, %9 ], [ %7, %3 ]
%11 = getelementptr inbounds i8, i8* %10, i64 1
%12 = tail call i8* @strchr(i8* %11, i8 signext 47)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %9
14: ; preds = %9, %3
%15 = phi i8* [ %0, %3 ], [ %11, %9 ]
%16 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %15, i32 %1)
%17 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = call i32 @fwrite(i32* %4, i32 4, i32 1, i32* %17)
%21 = tail call i32 @fclose(i32* %17)
br label %22
22: ; preds = %19, %14
ret void
}