#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 scalar_t__ uint8_t;
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ core_path;
typedef int /*<<< orphan*/ argv_zero;

/* Variables and functions */
int PATH_MAX;
scalar_t__ WCOREDUMP(int /*<<< orphan*/);
scalar_t__ WEXITSTATUS(int /*<<< orphan*/);
scalar_t__ WIFEXITED(int /*<<< orphan*/);
scalar_t__ WIFSIGNALED(int /*<<< orphan*/);
int WTERMSIG(int /*<<< orphan*/);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
char *basename(char *);
int /*<<< orphan*/ cp_file(char *, char *);
int /*<<< orphan*/ logprintf(char *, char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
char *strchr(char *, char);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ system(char *);
void system_logged(char *cmd) {
  pid_t code;
  uint8_t exit_status;
  int sig_num;
  char argv_zero[PATH_MAX];
  char core_path[PATH_MAX];
  char *exe_name;
  char *space;
  assert(cmd != NULL);
  strncpy(argv_zero, cmd, sizeof(argv_zero));
  argv_zero[PATH_MAX - 1] = '\0';
  space = strchr(argv_zero, ' ');
  if (space != NULL)
    *space = '\0';
  exe_name = basename(argv_zero);
  code = system(cmd);
  if (WIFEXITED(code)) {
    exit_status = WEXITSTATUS(code);
    if (exit_status) {
      logprintf("2command %s died with code %d\n", exe_name, exit_status);
      return;
    }
  } else if (WIFSIGNALED(code)) {
    sig_num = WTERMSIG(code);
    if (WCOREDUMP(code)) {
      logprintf("2command %s died from signal %d and dumped core\n", exe_name,
                sig_num);
      snprintf(core_path, sizeof(core_path), "/var/core-%s", exe_name);
      cp_file(core_path, "/mnt/media/nsboot/logs");
      logprintf("0the core dump was copied to /mnt/media/nsboot/logs/core-%s",
                exe_name);
      return;
    } else {
      logprintf("2command %s died from signal %d (but no core was produced)",
                exe_name, sig_num);
      return;
    }
  } else {
    logprintf("2something evil happened to command %s", exe_name);
  }
}


x86 assembly (clang, -O0):
<system_logged>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%ecx
mov    %rcx,-0x60(%rbp)
mov    %rsp,%rdx
mov    %rdx,-0x28(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x58(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x30(%rbp)
mov    (%rax),%eax
mov    %rax,-0x70(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x58(%rbp),%rdi
mov    -0x60(%rbp),%rax
mov    -0x8(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x58(%rbp),%rdi
mov    0x2d92(%rip),%rax
mov    (%rax),%eax
sub    $0x1,%eax
cltq
movb   $0x0,(%rdi,%rax,1)
mov    $0x20,%esi
call   1050 <strchr@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1289 <system_logged+0xb9>
mov    -0x48(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x58(%rbp),%rdi
call   10a0 <basename@plt>
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <system@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   10e0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12f2 <system_logged+0x122>
mov    -0xc(%rbp),%edi
call   10d0 <WEXITSTATUS@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12ed <system_logged+0x11d>
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xd26(%rip),%rdi
mov    $0x0,%al
call   10f0 <logprintf@plt>
movl   $0x1,-0x4c(%rbp)
jmp    13bd <system_logged+0x1ed>
jmp    13b6 <system_logged+0x1e6>
mov    -0xc(%rbp),%edi
call   1080 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     139f <system_logged+0x1cf>
mov    -0xc(%rbp),%edi
call   10b0 <WTERMSIG@plt>
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <WCOREDUMP@plt>
cmp    $0x0,%rax
je     137e <system_logged+0x1ae>
mov    -0x40(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0xcf0(%rip),%rdi
mov    $0x0,%al
call   10f0 <logprintf@plt>
mov    -0x68(%rbp),%rdi
mov    -0x70(%rbp),%rax
mov    %eax,%esi
mov    -0x40(%rbp),%rcx
lea    0xd05(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x68(%rbp),%rdi
lea    0xd02(%rip),%rsi
call   10c0 <cp_file@plt>
mov    -0x40(%rbp),%rsi
lea    0xd09(%rip),%rdi
mov    $0x0,%al
call   10f0 <logprintf@plt>
movl   $0x1,-0x4c(%rbp)
jmp    13bd <system_logged+0x1ed>
mov    -0x40(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0xd24(%rip),%rdi
mov    $0x0,%al
call   10f0 <logprintf@plt>
movl   $0x1,-0x4c(%rbp)
jmp    13bd <system_logged+0x1ed>
mov    -0x40(%rbp),%rsi
lea    0xd41(%rip),%rdi
mov    $0x0,%al
call   10f0 <logprintf@plt>
jmp    13b6 <system_logged+0x1e6>
movl   $0x0,-0x4c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<system_logged>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r13
mov    0x2e08(%rip),%r15
mov    (%r15),%r14d
mov    %rsp,%rbx
mov    %r14,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rsp,%r12
sub    %rax,%r12
mov    %r12,%rsp
xor    %edi,%edi
test   %r13,%r13
setne  %dil
call   1090 <assert@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
movslq (%r15),%rax
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbx,%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1241 <system_logged+0x71>
movb   $0x0,(%rax)
mov    %rbx,%rdi
call   10a0 <basename@plt>
mov    %rax,%r15
mov    %r13,%rdi
call   1040 <system@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   10e0 <WIFEXITED@plt>
test   %rax,%rax
je     128b <system_logged+0xbb>
mov    %ebx,%edi
call   10d0 <WEXITSTATUS@plt>
test   %rax,%rax
je     1315 <system_logged+0x145>
lea    0xd87(%rip),%rdi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   10f0 <logprintf@plt>
jmp    1315 <system_logged+0x145>
mov    %ebx,%edi
call   1080 <WIFSIGNALED@plt>
test   %rax,%rax
je     12ee <system_logged+0x11e>
mov    %ebx,%edi
call   10b0 <WTERMSIG@plt>
mov    %eax,%r13d
mov    %ebx,%edi
call   1060 <WCOREDUMP@plt>
test   %rax,%rax
je     1301 <system_logged+0x131>
lea    0xd6b(%rip),%rdi
mov    %r15,%rsi
mov    %r13d,%edx
xor    %eax,%eax
call   10f0 <logprintf@plt>
lea    0xd88(%rip),%rdx
mov    %r12,%rdi
mov    %r14d,%esi
mov    %r15,%rcx
call   1070 <snprintf@plt>
lea    0xd80(%rip),%rsi
mov    %r12,%rdi
call   10c0 <cp_file@plt>
lea    0xd88(%rip),%rdi
jmp    12f5 <system_logged+0x125>
lea    0xdf6(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   10f0 <logprintf@plt>
jmp    1315 <system_logged+0x145>
lea    0xda8(%rip),%rdi
mov    %r15,%rsi
mov    %r13d,%edx
xor    %eax,%eax
call   10f0 <logprintf@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [31 x i8] c"2command %s died with code %d\0A\00"
@.str.1 = [49 x i8] c"2command %s died from signal %d and dumped core\0A\00"
@.str.2 = [13 x i8] c"/var/core-%s\00"
@.str.3 = [23 x i8] c"/mnt/media/nsboot/logs\00"
@.str.4 = [60 x i8] c"0the core dump was copied to /mnt/media/nsboot/logs/core-%s\00"
@.str.5 = [59 x i8] c"2command %s died from signal %d (but no core was produced)\00"
@.str.6 = [39 x i8] c"2something evil happened to command %s\00"
define void @system_logged(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %2
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %6
%15 = alloca i8, i64 %13
store i64 %13, i64* %7
%16 = load i32, i32* @PATH_MAX
%17 = zext i32 %16 to i64
%18 = alloca i8, i64 %17
store i64 %17, i64* %8
%19 = load i8*, i8** %2
%20 = icmp ne i8* %19, null
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i8*, i8** %2
%24 = trunc i64 %13 to i32
%25 = call i32 @strncpy(i8* %15, i8* %23, i32 %24)
%26 = load i32, i32* @PATH_MAX
%27 = sub nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %15, i64 %28
store i8 0, i8* %29
%30 = call i8* @strchr(i8* %15, i8 signext 32)
store i8* %30, i8** %10
%31 = load i8*, i8** %10
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %35
33: ; preds = %1
%34 = load i8*, i8** %10
store i8 0, i8* %34
br label %35
35: ; preds = %33, %1
%36 = call i8* @basename(i8* %15)
store i8* %36, i8** %9
%37 = load i8*, i8** %2
%38 = call i32 @system(i8* %37)
store i32 %38, i32* %3
%39 = load i32, i32* %3
%40 = call i64 @WIFEXITED(i32 %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %52
42: ; preds = %35
%43 = load i32, i32* %3
%44 = call i64 @WEXITSTATUS(i32 %43)
store i64 %44, i64* %4
%45 = load i64, i64* %4
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %42
%48 = load i8*, i8** %9
%49 = load i64, i64* %4
%50 = call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %48, i64 %49)
store i32 1, i32* %11
br label %81
51: ; preds = %42
br label %80
52: ; preds = %35
%53 = load i32, i32* %3
%54 = call i64 @WIFSIGNALED(i32 %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %76
56: ; preds = %52
%57 = load i32, i32* %3
%58 = call i32 @WTERMSIG(i32 %57)
store i32 %58, i32* %5
%59 = load i32, i32* %3
%60 = call i64 @WCOREDUMP(i32 %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %72
62: ; preds = %56
%63 = load i8*, i8** %9
%64 = load i32, i32* %5
%65 = call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %63, i32 %64)
%66 = trunc i64 %17 to i32
%67 = load i8*, i8** %9
%68 = call i32 @snprintf(i8* %18, i32 %66, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %67)
%69 = call i32 @cp_file(i8* %18, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%70 = load i8*, i8** %9
%71 = call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.4, i64 0, i64 0), i8* %70)
store i32 1, i32* %11
br label %81
72: ; preds = %56
%73 = load i8*, i8** %9
%74 = load i32, i32* %5
%75 = call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.5, i64 0, i64 0), i8* %73, i32 %74)
store i32 1, i32* %11
br label %81
76: ; preds = %52
%77 = load i8*, i8** %9
%78 = call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i8* %77)
br label %79
79: ; preds = %76
br label %80
80: ; preds = %79, %51
store i32 0, i32* %11
br label %81
81: ; preds = %80, %72, %62, %47
%82 = load i8*, i8** %6
%83 = load i32, i32* %11
switch i32 %83, label %85 [
i32 0, label %84
i32 1, label %84
]
84: ; preds = %81, %81
ret void
85: ; preds = %81
unreachable
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [31 x i8] c"2command %s died with code %d\0A\00"
@.str.1 = [49 x i8] c"2command %s died from signal %d and dumped core\0A\00"
@.str.2 = [13 x i8] c"/var/core-%s\00"
@.str.3 = [23 x i8] c"/mnt/media/nsboot/logs\00"
@.str.4 = [60 x i8] c"0the core dump was copied to /mnt/media/nsboot/logs/core-%s\00"
@.str.5 = [59 x i8] c"2command %s died from signal %d (but no core was produced)\00"
@.str.6 = [39 x i8] c"2something evil happened to command %s\00"
define void @system_logged(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = alloca i8, i64 %3
%6 = icmp ne i8* %0, null
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = call i32 @strncpy(i8* %4, i8* %0, i32 %2)
%10 = load i32, i32* @PATH_MAX
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %4, i64 %12
store i8 0, i8* %13
%14 = call i8* @strchr(i8* %4, i8 signext 32)
%15 = icmp eq i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %1
store i8 0, i8* %14
br label %17
17: ; preds = %16, %1
%18 = call i8* @basename(i8* %4)
%19 = call i32 @system(i8* %0)
%20 = call i64 @WIFEXITED(i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %17
%23 = call i64 @WEXITSTATUS(i32 %19)
%24 = icmp eq i64 %23, 0
br i1 %24, label %43, label %25
25: ; preds = %22
%26 = call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %18, i64 %23)
br label %43
27: ; preds = %17
%28 = call i64 @WIFSIGNALED(i32 %19)
%29 = icmp eq i64 %28, 0
br i1 %29, label %41, label %30
30: ; preds = %27
%31 = call i32 @WTERMSIG(i32 %19)
%32 = call i64 @WCOREDUMP(i32 %19)
%33 = icmp eq i64 %32, 0
br i1 %33, label %39, label %34
34: ; preds = %30
%35 = call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %18, i32 %31)
%36 = call i32 @snprintf(i8* %5, i32 %2, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%37 = call i32 @cp_file(i8* %5, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%38 = call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.4, i64 0, i64 0), i8* %18)
br label %43
39: ; preds = %30
%40 = call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.5, i64 0, i64 0), i8* %18, i32 %31)
br label %43
41: ; preds = %27
%42 = call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i8* %18)
br label %43
43: ; preds = %41, %22, %39, %34, %25
ret void
}