#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*/ ignore_return_value(int /*<<< orphan*/);
size_t strnlen(char const *, int);
scalar_t__ strstr(char const *, char *);
int /*<<< orphan*/ write(int const, char const *, int);
int compute_exit_code(const char *status_msg) {
  if (!status_msg) {
    return 0;
  }
  size_t n = strnlen(status_msg, 2047);
  if (n >= 2047) {
    status_msg = "main: internal error: error message is too long";
    n = strnlen(status_msg, 2047);
  }
  const int stderr_fd = 2;
  ignore_return_value(write(stderr_fd, status_msg, n));
  ignore_return_value(write(stderr_fd, "\n", 1));
  return strstr(status_msg, "internal error:") ? 2 : 1;
}


x86 assembly (clang, -O0):
<compute_exit_code>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1163 <compute_exit_code+0x23>
movl   $0x0,-0x4(%rbp)
jmp    1206 <compute_exit_code+0xc6>
mov    -0x10(%rbp),%rdi
mov    $0x7ff,%esi
call   1040 <strnlen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x7ff,-0x18(%rbp)
jb     11a0 <compute_exit_code+0x60>
lea    0xe76(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x7ff,%esi
call   1040 <strnlen@plt>
mov    %rax,-0x18(%rbp)
movl   $0x2,-0x1c(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
mov    $0x2,%edi
call   1030 <write@plt>
mov    %eax,%edi
call   1050 <ignore_return_value@plt>
mov    $0x2,%edi
lea    0xe62(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,%edi
call   1050 <ignore_return_value@plt>
mov    -0x10(%rbp),%rdi
lea    0xe48(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,%rdx
mov    $0x1,%eax
mov    $0x2,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compute_exit_code>:
push   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
je     11c1 <compute_exit_code+0x81>
mov    %rdi,%rbx
mov    $0x7ff,%esi
call   1040 <strnlen@plt>
cmp    $0x7ff,%rax
jb     1171 <compute_exit_code+0x31>
lea    0xe9c(%rip),%rbx
mov    %rbx,%rdi
mov    $0x7ff,%esi
call   1040 <strnlen@plt>
mov    $0x2,%ebp
mov    $0x2,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%edi
call   1050 <ignore_return_value@plt>
lea    0xe9d(%rip),%rsi
mov    $0x2,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,%edi
call   1050 <ignore_return_value@plt>
lea    0xe82(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strstr@plt>
cmp    $0x1,%rax
sbb    $0x0,%ebp
jmp    11c3 <compute_exit_code+0x83>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [48 x i8] c"main: internal error: error message is too long\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [16 x i8] c"internal error:\00"
define i32 @compute_exit_code(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %9, label %8
8: ; preds = %1
store i32 0, i32* %2
br label %30
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i64 @strnlen(i8* %10, i32 2047)
store i64 %11, i64* %4
%12 = load i64, i64* %4
%13 = icmp uge i64 %12, 2047
br i1 %13, label %14, label %17
14: ; preds = %9
store i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8** %3
%15 = load i8*, i8** %3
%16 = call i64 @strnlen(i8* %15, i32 2047)
store i64 %16, i64* %4
br label %17
17: ; preds = %14, %9
store i32 2, i32* %5
%18 = load i8*, i8** %3
%19 = load i64, i64* %4
%20 = trunc i64 %19 to i32
%21 = call i32 @write(i32 2, i8* %18, i32 %20)
%22 = call i32 @ignore_return_value(i32 %21)
%23 = call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%24 = call i32 @ignore_return_value(i32 %23)
%25 = load i8*, i8** %3
%26 = call i64 @strstr(i8* %25, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp ne i64 %26, 0
%28 = zext i1 %27 to i64
%29 = select i1 %27, i32 2, i32 1
store i32 %29, i32* %2
br label %30
30: ; preds = %17, %8
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@.str = [48 x i8] c"main: internal error: error message is too long\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [16 x i8] c"internal error:\00"
define i32 @compute_exit_code(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %19, label %3
3: ; preds = %1
%4 = tail call i64 @strnlen(i8* %0, i32 2047)
%5 = icmp ugt i64 %4, 2046
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i64 @strnlen(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 2047)
br label %8
8: ; preds = %6, %3
%9 = phi i8* [ getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), %6 ], [ %0, %3 ]
%10 = phi i64 [ %7, %6 ], [ %4, %3 ]
%11 = trunc i64 %10 to i32
%12 = tail call i32 @write(i32 2, i8* %9, i32 %11)
%13 = tail call i32 @ignore_return_value(i32 %12)
%14 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%15 = tail call i32 @ignore_return_value(i32 %14)
%16 = tail call i64 @strstr(i8* %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
%18 = select i1 %17, i32 1, i32 2
br label %19
19: ; preds = %1, %8
%20 = phi i32 [ %18, %8 ], [ 0, %1 ]
ret i32 %20
}