#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 /*<<< orphan*/ STDOUT_FILENO;
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, ...);
char *strerror(int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int /*<<< orphan*/);
void handle_error(long return_code, const char *msg, int in_thread) {
  if (return_code < 0) {
    char extra_txt[16384];
    char error_msg[16384];
    char *extra_msg = extra_txt;
    int myerrno = errno;
    const char *error_str = strerror(myerrno);
    if (msg != NULL) {
      sprintf(extra_msg, "%s\n", msg);
    } else {
      extra_msg = "";
    }
    sprintf(error_msg, "%sreturn_code=%ld\nerrno=%d\nmessage=%s\n", extra_msg,
            return_code, myerrno, error_str);
    write(STDOUT_FILENO, error_msg, strlen(error_msg));
    if (in_thread) {
      pthread_exit(NULL);
    } else {
      exit(1);
    }
  }
}


x86 assembly (clang, -O0):
<handle_error>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8050,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    1279 <handle_error+0x119>
lea    -0x4020(%rbp),%rax
mov    %rax,-0x8028(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x802c(%rbp)
mov    -0x802c(%rbp),%edi
call   1080 <strerror@plt>
mov    %rax,-0x8038(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11d9 <handle_error+0x79>
mov    -0x8028(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe56(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
jmp    11e7 <handle_error+0x87>
lea    0xe46(%rip),%rax
mov    %rax,-0x8028(%rbp)
lea    -0x8020(%rbp),%rdi
mov    -0x8028(%rbp),%rdx
mov    -0x8(%rbp),%rcx
mov    -0x802c(%rbp),%r8d
mov    -0x8038(%rbp),%r9
lea    0xdf2(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8044(%rbp)
lea    -0x8020(%rbp),%rax
mov    %rax,-0x8040(%rbp)
lea    -0x8020(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x8044(%rbp),%edi
mov    -0x8040(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmpl   $0x0,-0x14(%rbp)
je     126a <handle_error+0x10a>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <pthread_exit@plt>
jmp    1274 <handle_error+0x114>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1279 <handle_error+0x119>
add    $0x8050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_error>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x8008,%rsp
test   %rdi,%rdi
jns    120b <handle_error+0xab>
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    0x2e4e(%rip),%rax
mov    (%rax),%r12d
mov    %r12d,%edi
call   1080 <strerror@plt>
mov    %rax,%r15
test   %rbp,%rbp
je     11bb <handle_error+0x5b>
lea    0x4000(%rsp),%r13
lea    0xe77(%rip),%rsi
mov    %r13,%rdi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <sprintf@plt>
jmp    11c2 <handle_error+0x62>
lea    0xe64(%rip),%r13
lea    0xe37(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %r13,%rdx
mov    %rbx,%rcx
mov    %r12d,%r8d
mov    %r15,%r9
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%ebx
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %r14d,%r14d
je     121d <handle_error+0xbd>
xor    %edi,%edi
call   1050 <pthread_exit@plt>
add    $0x8008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@errno = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [39 x i8] c"%sreturn_code=%ld\0Aerrno=%d\0Amessage=%s\0A\00"
@STDOUT_FILENO = global i32 0
define void @handle_error(i64 %0, i8* %1, i32 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [16384 x i8]
%8 = alloca [16384 x i8]
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
store i64 %0, i64* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%12 = load i64, i64* %4
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %45
14: ; preds = %3
%15 = getelementptr inbounds [16384 x i8], [16384 x i8]* %7, i64 0, i64 0
store i8* %15, i8** %9
%16 = load i32, i32* @errno
store i32 %16, i32* %10
%17 = load i32, i32* %10
%18 = call i8* @strerror(i32 %17)
store i8* %18, i8** %11
%19 = load i8*, i8** %5
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %25
21: ; preds = %14
%22 = load i8*, i8** %9
%23 = load i8*, i8** %5
%24 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %26
25: ; preds = %14
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** %9
br label %26
26: ; preds = %25, %21
%27 = getelementptr inbounds [16384 x i8], [16384 x i8]* %8, i64 0, i64 0
%28 = load i8*, i8** %9
%29 = load i64, i64* %4
%30 = load i32, i32* %10
%31 = load i8*, i8** %11
%32 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %27, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %28, i64 %29, i32 %30, i8* %31)
%33 = load i32, i32* @STDOUT_FILENO
%34 = getelementptr inbounds [16384 x i8], [16384 x i8]* %8, i64 0, i64 0
%35 = getelementptr inbounds [16384 x i8], [16384 x i8]* %8, i64 0, i64 0
%36 = call i32 @strlen(i8* %35)
%37 = call i32 @write(i32 %33, i8* %34, i32 %36)
%38 = load i32, i32* %6
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %26
%41 = call i32 @pthread_exit(i32* null)
br label %44
42: ; preds = %26
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %40
br label %45
45: ; preds = %44, %3
ret void
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [39 x i8] c"%sreturn_code=%ld\0Aerrno=%d\0Amessage=%s\0A\00"
@STDOUT_FILENO = global i32 0
define void @handle_error(i64 %0, i8* %1, i32 %2) {
%4 = alloca [16384 x i8]
%5 = alloca [16384 x i8]
%6 = icmp slt i64 %0, 0
br i1 %6, label %7, label %26
7: ; preds = %3
%8 = getelementptr inbounds [16384 x i8], [16384 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [16384 x i8], [16384 x i8]* %5, i64 0, i64 0
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = icmp eq i8* %1, null
br i1 %12, label %15, label %13
13: ; preds = %7
%14 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %15
15: ; preds = %7, %13
%16 = phi i8* [ %8, %13 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %7 ]
%17 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %16, i64 %0, i32 %10, i8* %11)
%18 = load i32, i32* @STDOUT_FILENO
%19 = call i32 @strlen(i8* %9)
%20 = call i32 @write(i32 %18, i8* %9, i32 %19)
%21 = icmp eq i32 %2, 0
br i1 %21, label %24, label %22
22: ; preds = %15
%23 = call i32 @pthread_exit(i32* null)
br label %26
24: ; preds = %15
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %22, %3
ret void
}