#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 */
struct timeval {
  char const *tv_sec;
  int /*<<< orphan*/ tv_usec;
};
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
char *log_dir();
char *log_level_desc(int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
void log_msg(int level, const char *s, const char *file, const int line) {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  char timestamp[128];
  sprintf(timestamp, "%ld.%06ld", tv.tv_sec, tv.tv_usec);
  const char *log_path = log_dir();
  if (!log_path) {
    fprintf(stderr, "Error: no log dir, logging is disabled.\n");
    return;
  }
  pid_t pid = getpid();
  char fname[128];
  sprintf(fname, "%s%d.log", log_path, pid);
  FILE *fs = fopen(fname, "a");
  if (!fs) {
    fprintf(stderr, "%s:%d: fopen() failed\n",
            "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
            "2022-01-22/02-19-57/repos/mmmmhack/gstep/refs/heads/master/util.c",
            92);
    exit(1);
  }
  char *err_pre = "";
  char *err_detail = "";
  if (level >= 50 && errno) {
    err_pre = " error detail: ";
    err_detail = strerror(errno);
  }
  fprintf(fs, "[%s] %s: %s:%d: %s%s%s\n", timestamp, log_level_desc(level),
          file, line, s, err_pre, err_detail);
  fflush(fs);
  fclose(fs);
}


x86 assembly (clang, -O0):
<log_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x180,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
lea    -0xb0(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%ecx
lea    0xe15(%rip),%rsi
call   10b0 <sprintf@plt>
mov    $0x0,%al
call   1070 <log_dir@plt>
mov    %rax,-0xb8(%rbp)
cmpq   $0x0,-0xb8(%rbp)
jne    1229 <log_msg+0x79>
mov    0x2dcd(%rip),%rax
movslq (%rax),%rdi
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    138a <log_msg+0x1da>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0xbc(%rbp)
lea    -0x140(%rbp),%rdi
mov    -0xb8(%rbp),%rdx
mov    -0xbc(%rbp),%ecx
lea    0xde2(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x140(%rbp),%rdi
lea    0xdd8(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x148(%rbp)
cmpq   $0x0,-0x148(%rbp)
jne    12ac <log_msg+0xfc>
mov    0x2d5b(%rip),%rax
movslq (%rax),%rdi
lea    0xdaf(%rip),%rsi
lea    0xdbf(%rip),%rdx
mov    $0x5c,%ecx
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd7f(%rip),%rax
mov    %rax,-0x150(%rbp)
lea    0xd71(%rip),%rax
mov    %rax,-0x158(%rbp)
cmpl   $0x32,-0x4(%rbp)
jl     1307 <log_msg+0x157>
mov    0x2cff(%rip),%rax
cmpq   $0x0,(%rax)
je     1307 <log_msg+0x157>
lea    0xded(%rip),%rax
mov    %rax,-0x150(%rbp)
mov    0x2ce0(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    %rax,-0x158(%rbp)
mov    -0x148(%rbp),%rax
mov    %rax,-0x168(%rbp)
lea    -0xb0(%rbp),%rax
mov    %rax,-0x160(%rbp)
mov    -0x4(%rbp),%edi
call   1080 <log_level_desc@plt>
mov    -0x168(%rbp),%rdi
mov    -0x160(%rbp),%rdx
mov    %rax,%rcx
mov    -0x18(%rbp),%r8
mov    -0x1c(%rbp),%r9d
mov    -0x10(%rbp),%r11
mov    -0x150(%rbp),%r10
mov    -0x158(%rbp),%rax
lea    0xd8a(%rip),%rsi
mov    %r11,(%rsp)
mov    %r10,0x8(%rsp)
mov    %rax,0x10(%rsp)
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x148(%rbp),%rdi
call   1090 <fflush@plt>
mov    -0x148(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x180,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_msg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
mov    %ecx,%r12d
mov    %rdx,%rbp
mov    %rsi,%r15
mov    %edi,%r13d
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    0x10(%rsp),%rdx
mov    0x18(%rsp),%ecx
lea    0xe07(%rip),%rsi
lea    0x20(%rsp),%rdi
call   10b0 <sprintf@plt>
xor    %eax,%eax
call   1070 <log_dir@plt>
test   %rax,%rax
je     1285 <log_msg+0xc5>
mov    %rax,%rbx
mov    %rbp,0x8(%rsp)
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe0e(%rip),%rsi
lea    0xa0(%rsp),%rbp
mov    %rbp,%rdi
mov    %rbx,%rdx
mov    %eax,%ecx
call   10b0 <sprintf@plt>
lea    0xdfb(%rip),%rsi
mov    %rbp,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1311 <log_msg+0x151>
mov    %rax,%rbx
lea    0xdd6(%rip),%rbp
cmp    $0x32,%r13d
jl     12b6 <log_msg+0xf6>
mov    0x2d6f(%rip),%rax
mov    (%rax),%rdi
mov    %rbp,%r14
test   %rdi,%rdi
je     12b9 <log_msg+0xf9>
call   10e0 <strerror@plt>
mov    %rax,%r14
lea    0xe54(%rip),%rbp
jmp    12b9 <log_msg+0xf9>
mov    0x2d54(%rip),%rax
movslq (%rax),%rcx
lea    0xd74(%rip),%rdi
mov    $0x28,%esi
mov    $0x1,%edx
add    $0x128,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10d0 <fwrite@plt>
mov    %rbp,%r14
mov    %r13d,%edi
call   1080 <log_level_desc@plt>
sub    $0x8,%rsp
lea    0xe1b(%rip),%rsi
lea    0x28(%rsp),%rdx
mov    %rbx,%rdi
mov    %rax,%rcx
mov    0x10(%rsp),%r8
mov    %r12d,%r9d
xor    %eax,%eax
push   %r14
push   %rbp
push   %r15
call   1060 <fprintf@plt>
add    $0x20,%rsp
mov    %rbx,%rdi
call   1090 <fflush@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x128,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cc8(%rip),%rax
movslq (%rax),%rdi
lea    0xd1c(%rip),%rsi
lea    0xd2c(%rip),%rdx
mov    $0x5c,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i8*, i32 }
@.str = [10 x i8] c"%ld.%06ld\00"
@stderr = global i32 0
@.str.1 = [41 x i8] c"Error: no log dir, logging is disabled.\0A\00"
@.str.2 = [9 x i8] c"%s%d.log\00"
@.str.3 = [2 x i8] c"a\00"
@.str.4 = [23 x i8] c"%s:%d: fopen() failed\0A\00"
@.str.5 = [130 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/mmmmhack/gstep/refs/heads/master/util.c\00"
@.str.6 = [1 x i8] zeroinitializer
@errno = global i64 0
@.str.7 = [16 x i8] c" error detail: \00"
@.str.8 = [24 x i8] c"[%s] %s: %s:%d: %s%s%s\0A\00"
define void @log_msg(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca [128 x i8]
%11 = alloca i8*
%12 = alloca i32
%13 = alloca [128 x i8]
%14 = alloca i32*
%15 = alloca i8*
%16 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%17 = call i32 @gettimeofday(%struct.timeval* %9, i32* null)
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%20 = load i8*, i8** %19
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%22 = load i32, i32* %21
%23 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %22)
%24 = call i8* (...) @log_dir()
store i8* %24, i8** %11
%25 = load i8*, i8** %11
%26 = icmp ne i8* %25, null
br i1 %26, label %32, label %27
27: ; preds = %4
%28 = load i32, i32* @stderr
%29 = sext i32 %28 to i64
%30 = inttoptr i64 %29 to i32*
%31 = call i32 (i32*, i8*, ...) @fprintf(i32* %30, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %72
32: ; preds = %4
%33 = call i32 (...) @getpid()
store i32 %33, i32* %12
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%35 = load i8*, i8** %11
%36 = load i32, i32* %12
%37 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %35, i32 %36)
%38 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%39 = call i32* @fopen(i8* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %39, i32** %14
%40 = load i32*, i32** %14
%41 = icmp ne i32* %40, null
br i1 %41, label %48, label %42
42: ; preds = %32
%43 = load i32, i32* @stderr
%44 = sext i32 %43 to i64
%45 = inttoptr i64 %44 to i32*
%46 = call i32 (i32*, i8*, ...) @fprintf(i32* %45, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([130 x i8], [130 x i8]* @.str.5, i64 0, i64 0), i32 92)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %32
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0), i8** %15
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0), i8** %16
%49 = load i32, i32* %5
%50 = icmp sge i32 %49, 50
br i1 %50, label %51, label %57
51: ; preds = %48
%52 = load i64, i64* @errno
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i8** %15
%55 = load i64, i64* @errno
%56 = call i8* @strerror(i64 %55)
store i8* %56, i8** %16
br label %57
57: ; preds = %54, %51, %48
%58 = load i32*, i32** %14
%59 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%60 = load i32, i32* %5
%61 = call i8* @log_level_desc(i32 %60)
%62 = load i8*, i8** %7
%63 = load i32, i32* %8
%64 = load i8*, i8** %6
%65 = load i8*, i8** %15
%66 = load i8*, i8** %16
%67 = call i32 (i32*, i8*, ...) @fprintf(i32* %58, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0), i8* %59, i8* %61, i8* %62, i32 %63, i8* %64, i8* %65, i8* %66)
%68 = load i32*, i32** %14
%69 = call i32 @fflush(i32* %68)
%70 = load i32*, i32** %14
%71 = call i32 @fclose(i32* %70)
br label %72
72: ; preds = %57, %27
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i8*, i32 }
@.str = [10 x i8] c"%ld.%06ld\00"
@stderr = global i32 0
@.str.1 = [41 x i8] c"Error: no log dir, logging is disabled.\0A\00"
@.str.2 = [9 x i8] c"%s%d.log\00"
@.str.3 = [2 x i8] c"a\00"
@.str.4 = [23 x i8] c"%s:%d: fopen() failed\0A\00"
@.str.5 = [130 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/mmmmhack/gstep/refs/heads/master/util.c\00"
@.str.6 = [1 x i8] zeroinitializer
@errno = global i64 0
@.str.7 = [16 x i8] c" error detail: \00"
@.str.8 = [24 x i8] c"[%s] %s: %s:%d: %s%s%s\0A\00"
define void @log_msg(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca %struct.timeval
%6 = alloca [128 x i8]
%7 = alloca [128 x i8]
%8 = bitcast %struct.timeval* %5 to i8*
%9 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%12 = load i8*, i8** %11
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%14 = load i32, i32* %13
%15 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %12, i32 %14)
%16 = tail call i8* (...) @log_dir()
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %23
18: ; preds = %4
%19 = load i32, i32* @stderr
%20 = sext i32 %19 to i64
%21 = inttoptr i64 %20 to i32*
%22 = tail call i64 @fwrite(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i64 40, i64 1, i32* %21)
br label %49
23: ; preds = %4
%24 = tail call i32 (...) @getpid()
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%26 = call i32 @sprintf(i8* %25, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %16, i32 %24)
%27 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %35
29: ; preds = %23
%30 = load i32, i32* @stderr
%31 = sext i32 %30 to i64
%32 = inttoptr i64 %31 to i32*
%33 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %32, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([130 x i8], [130 x i8]* @.str.5, i64 0, i64 0), i32 92)
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %23
%36 = icmp sgt i32 %0, 49
%37 = load i64, i64* @errno
%38 = icmp ne i64 %37, 0
%39 = select i1 %36, i1 %38, i1 false
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = tail call i8* @strerror(i64 %37)
br label %42
42: ; preds = %40, %35
%43 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), %40 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0), %35 ]
%44 = phi i8* [ %41, %40 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0), %35 ]
%45 = tail call i8* @log_level_desc(i32 %0)
%46 = call i32 (i32*, i8*, ...) @fprintf(i32* %27, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0), i8* %10, i8* %45, i8* %2, i32 %3, i8* %1, i8* %43, i8* %44)
%47 = call i32 @fflush(i32* %27)
%48 = call i32 @fclose(i32* %27)
br label %49
49: ; preds = %42, %18
ret void
}