#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*/ cmd;

/* Variables and functions */
long INT_MAX;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_INFO;
scalar_t__ WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ logmsg(int /*<<< orphan*/, char *, char *, ...);
char *malloc(int);
int /*<<< orphan*/ strcpy(char *, char *);
char *strerror(int /*<<< orphan*/);
char *strtok(char *, char *);
long sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
void song_exec(char *cmd) {
  char *song_argv[30];
  char *search = " ";
  char *buf = malloc(sizeof(cmd));
  int it = 0;
  long max_fd;
  int fd;
  int ret;
  strcpy(buf, cmd);
  if ((song_argv[0] = strtok(buf, search))) {
    do {
      it++;
    } while ((song_argv[it] = strtok(NULL, search)));
  } else {
    free(buf);
    return;
  }
  ret = fork();
  if (ret == 0) {
    max_fd = sysconf(_SC_OPEN_MAX);
    if (max_fd > INT_MAX)
      max_fd = INT_MAX;
    for (fd = 3; fd < max_fd; fd++)
      close(fd);
    execvp(song_argv[0], song_argv);
    logmsg(LOG_ERR, "Could not execute %s: %s", song_argv[0], strerror(errno));
    exit(1);
  } else if (ret == -1) {
    logmsg(LOG_ERR, "Could not fork: %s", strerror(errno));
  } else {
    waitpid(ret, &ret, 0);
    if ((WIFEXITED(ret) == 0) || (WEXITSTATUS(ret) != 0)) {
      logmsg(LOG_INFO, "%s failed", song_argv[0]);
    }
  }
  free(buf);
  return;
}


x86 assembly (clang, -O0):
<song_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe0a(%rip),%rax
mov    %rax,-0x108(%rbp)
mov    $0x8,%edi
call   1060 <malloc@plt>
mov    %rax,-0x110(%rbp)
movl   $0x0,-0x114(%rbp)
mov    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x110(%rbp),%rdi
mov    -0x108(%rbp),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x100(%rbp)
cmp    $0x0,%rax
je     128e <song_exec+0xae>
jmp    1251 <song_exec+0x71>
mov    -0x114(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x114(%rbp)
mov    -0x108(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <strtok@plt>
movslq -0x114(%rbp),%rcx
mov    %rax,-0x100(%rbp,%rcx,8)
ff
cmp    $0x0,%rax
jne    1251 <song_exec+0x71>
jmp    129f <song_exec+0xbf>
mov    -0x110(%rbp),%rdi
call   1030 <free@plt>
jmp    1451 <song_exec+0x271>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x128(%rbp)
cmpl   $0x0,-0x128(%rbp)
jne    1399 <song_exec+0x1b9>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    %rax,-0x120(%rbp)
mov    -0x120(%rbp),%rax
mov    0x2d0c(%rip),%rcx
cmp    (%rcx),%rax
jle    12f6 <song_exec+0x116>
mov    0x2cfc(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x120(%rbp)
movl   $0x3,-0x124(%rbp)
movslq -0x124(%rbp),%rax
cmp    -0x120(%rbp),%rax
jge    1333 <song_exec+0x153>
mov    -0x124(%rbp),%edi
call   1050 <close@plt>
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    1300 <song_exec+0x120>
mov    -0x100(%rbp),%rdi
lea    -0x100(%rbp),%rsi
call   10c0 <execvp@plt>
mov    0x2c6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x134(%rbp)
mov    -0x100(%rbp),%rax
mov    %rax,-0x130(%rbp)
mov    0x2c66(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    -0x134(%rbp),%edi
mov    -0x130(%rbp),%rdx
mov    %rax,%rcx
lea    0xc7a(%rip),%rsi
mov    $0x0,%al
call   1070 <logmsg@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
cmpl   $0xffffffff,-0x128(%rbp)
jne    13df <song_exec+0x1ff>
mov    0x2c0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x138(%rbp)
mov    0x2c14(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    -0x138(%rbp),%edi
mov    %rax,%rdx
lea    0xc48(%rip),%rsi
mov    $0x0,%al
call   1070 <logmsg@plt>
jmp    1440 <song_exec+0x260>
mov    -0x128(%rbp),%edi
lea    -0x128(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    -0x128(%rbp),%edi
call   10d0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     141d <song_exec+0x23d>
mov    -0x128(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
cmp    $0x0,%rax
je     143b <song_exec+0x25b>
mov    0x2ba4(%rip),%rax
mov    (%rax),%edi
mov    -0x100(%rbp),%rdx
lea    0xbfa(%rip),%rsi
mov    $0x0,%al
call   1070 <logmsg@plt>
jmp    1440 <song_exec+0x260>
jmp    1445 <song_exec+0x265>
mov    -0x110(%rbp),%rdi
call   1030 <free@plt>
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<song_exec>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%rbx
mov    $0x8,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
lea    0xdf2(%rip),%rsi
mov    %r14,%rdi
call   10a0 <strtok@plt>
mov    %rax,0x10(%rsp)
test   %rax,%rax
je     129f <song_exec+0xbf>
lea    0x18(%rsp),%rbx
lea    0xdd4(%rip),%r15
nopl   0x0(%rax)
xor    %edi,%edi
mov    %r15,%rsi
call   10a0 <strtok@plt>
mov    %rax,(%rbx)
add    $0x8,%rbx
test   %rax,%rax
jne    1230 <song_exec+0x50>
xor    %eax,%eax
call   1100 <fork@plt>
mov    %eax,0xc(%rsp)
cmp    $0xffffffff,%eax
je     12b3 <song_exec+0xd3>
test   %eax,%eax
je     12f2 <song_exec+0x112>
lea    0xc(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    0xc(%rsp),%edi
call   10d0 <WIFEXITED@plt>
test   %rax,%rax
je     1288 <song_exec+0xa8>
mov    0xc(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
test   %rax,%rax
je     12dd <song_exec+0xfd>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rsp),%rdx
lea    0xd91(%rip),%rsi
jmp    12d6 <song_exec+0xf6>
mov    %r14,%rdi
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <free@plt>
mov    0x2cfe(%rip),%rax
mov    (%rax),%ebx
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xd4a(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <logmsg@plt>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    %rax,%r14
mov    0x2cde(%rip),%rax
mov    (%rax),%rax
cmp    %rax,%r14
cmovg  %rax,%r14
cmp    $0x4,%r14
jl     132f <song_exec+0x14f>
mov    $0x3,%ebx
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    131f <song_exec+0x13f>
mov    0x10(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   10c0 <execvp@plt>
mov    0x2c73(%rip),%rax
mov    (%rax),%ebx
mov    0x10(%rsp),%r14
mov    0x2c7d(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xca1(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <logmsg@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
@_SC_OPEN_MAX = global i32 0
@INT_MAX = global i64 0
@LOG_ERR = global i32 0
@.str.1 = [25 x i8] c"Could not execute %s: %s\00"
@errno = global i32 0
@.str.2 = [19 x i8] c"Could not fork: %s\00"
@LOG_INFO = global i32 0
@.str.3 = [10 x i8] c"%s failed\00"
define void @song_exec(i8* %0) {
%2 = alloca i8*
%3 = alloca [30 x i8*]
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %2
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %4
%10 = call i8* @malloc(i32 8)
store i8* %10, i8** %5
store i32 0, i32* %6
%11 = load i8*, i8** %5
%12 = load i8*, i8** %2
%13 = call i32 @strcpy(i8* %11, i8* %12)
%14 = load i8*, i8** %5
%15 = load i8*, i8** %4
%16 = call i8* @strtok(i8* %14, i8* %15)
%17 = getelementptr inbounds [30 x i8*], [30 x i8*]* %3, i64 0, i64 0
store i8* %16, i8** %17
%18 = icmp ne i8* %16, null
br i1 %18, label %19, label %31
19: ; preds = %1
br label %20
20: ; preds = %23, %19
%21 = load i32, i32* %6
%22 = add nsw i32 %21, 1
store i32 %22, i32* %6
br label %23
23: ; preds = %20
%24 = load i8*, i8** %4
%25 = call i8* @strtok(i8* null, i8* %24)
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [30 x i8*], [30 x i8*]* %3, i64 0, i64 %27
store i8* %25, i8** %28
%29 = icmp ne i8* %25, null
br i1 %29, label %20, label %30
30: ; preds = %23
br label %34
31: ; preds = %1
%32 = load i8*, i8** %5
%33 = call i32 @free(i8* %32)
br label %98
34: ; preds = %30
%35 = call i32 (...) @fork()
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %70
38: ; preds = %34
%39 = load i32, i32* @_SC_OPEN_MAX
%40 = call i64 @sysconf(i32 %39)
store i64 %40, i64* %7
%41 = load i64, i64* %7
%42 = load i64, i64* @INT_MAX
%43 = icmp sgt i64 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %38
%45 = load i64, i64* @INT_MAX
store i64 %45, i64* %7
br label %46
46: ; preds = %44, %38
store i32 3, i32* %8
br label %47
47: ; preds = %55, %46
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = load i64, i64* %7
%51 = icmp slt i64 %49, %50
br i1 %51, label %52, label %58
52: ; preds = %47
%53 = load i32, i32* %8
%54 = call i32 @close(i32 %53)
br label %55
55: ; preds = %52
%56 = load i32, i32* %8
%57 = add nsw i32 %56, 1
store i32 %57, i32* %8
br label %47
58: ; preds = %47
%59 = getelementptr inbounds [30 x i8*], [30 x i8*]* %3, i64 0, i64 0
%60 = load i8*, i8** %59
%61 = getelementptr inbounds [30 x i8*], [30 x i8*]* %3, i64 0, i64 0
%62 = call i32 @execvp(i8* %60, i8** %61)
%63 = load i32, i32* @LOG_ERR
%64 = getelementptr inbounds [30 x i8*], [30 x i8*]* %3, i64 0, i64 0
%65 = load i8*, i8** %64
%66 = load i32, i32* @errno
%67 = call i8* @strerror(i32 %66)
%68 = call i32 (i32, i8*, i8*, ...) @logmsg(i32 %63, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %65, i8* %67)
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %34
%71 = load i32, i32* %9
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %78
73: ; preds = %70
%74 = load i32, i32* @LOG_ERR
%75 = load i32, i32* @errno
%76 = call i8* @strerror(i32 %75)
%77 = call i32 (i32, i8*, i8*, ...) @logmsg(i32 %74, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %76)
br label %94
78: ; preds = %70
%79 = load i32, i32* %9
%80 = call i32 @waitpid(i32 %79, i32* %9, i32 0)
%81 = load i32, i32* %9
%82 = call i64 @WIFEXITED(i32 %81)
%83 = icmp eq i64 %82, 0
br i1 %83, label %88, label %84
84: ; preds = %78
%85 = load i32, i32* %9
%86 = call i64 @WEXITSTATUS(i32 %85)
%87 = icmp ne i64 %86, 0
br i1 %87, label %88, label %93
88: ; preds = %84, %78
%89 = load i32, i32* @LOG_INFO
%90 = getelementptr inbounds [30 x i8*], [30 x i8*]* %3, i64 0, i64 0
%91 = load i8*, i8** %90
%92 = call i32 (i32, i8*, i8*, ...) @logmsg(i32 %89, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %91)
br label %93
93: ; preds = %88, %84
br label %94
94: ; preds = %93, %73
br label %95
95: ; preds = %94
%96 = load i8*, i8** %5
%97 = call i32 @free(i8* %96)
br label %98
98: ; preds = %95, %31
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
@_SC_OPEN_MAX = global i32 0
@INT_MAX = global i64 0
@LOG_ERR = global i32 0
@.str.1 = [25 x i8] c"Could not execute %s: %s\00"
@errno = global i32 0
@.str.2 = [19 x i8] c"Could not fork: %s\00"
@LOG_INFO = global i32 0
@.str.3 = [10 x i8] c"%s failed\00"
define void @song_exec(i8* %0) {
%2 = alloca [30 x i8*]
%3 = alloca i32
%4 = bitcast [30 x i8*]* %2 to i8*
%5 = tail call dereferenceable_or_null(8) i8* @malloc(i32 8)
%6 = bitcast i32* %3 to i8*
%7 = tail call i32 @strcpy(i8* %5, i8* %0)
%8 = tail call i8* @strtok(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds [30 x i8*], [30 x i8*]* %2, i64 0, i64 0
store i8* %8, i8** %9
%10 = icmp eq i8* %8, null
br i1 %10, label %17, label %11
11: ; preds = %1, %11
%12 = phi i64 [ %13, %11 ], [ 0, %1 ]
%13 = add nuw nsw i64 %12, 1
%14 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = getelementptr inbounds [30 x i8*], [30 x i8*]* %2, i64 0, i64 %13
store i8* %14, i8** %15
%16 = icmp eq i8* %14, null
br i1 %16, label %19, label %11
17: ; preds = %1
%18 = tail call i32 @free(i8* %5)
br label %63
19: ; preds = %11
%20 = tail call i32 (...) @fork()
store i32 %20, i32* %3
switch i32 %20, label %48 [
i32 0, label %21
i32 -1, label %43
]
21: ; preds = %19
%22 = load i32, i32* @_SC_OPEN_MAX
%23 = tail call i64 @sysconf(i32 %22)
%24 = load i64, i64* @INT_MAX
%25 = icmp sgt i64 %23, %24
%26 = select i1 %25, i64 %24, i64 %23
%27 = icmp sgt i64 %26, 3
br i1 %27, label %28, label %34
28: ; preds = %21, %28
%29 = phi i64 [ %32, %28 ], [ 3, %21 ]
%30 = trunc i64 %29 to i32
%31 = tail call i32 @close(i32 %30)
%32 = add nuw nsw i64 %29, 1
%33 = icmp eq i64 %32, %26
br i1 %33, label %34, label %28
34: ; preds = %28, %21
%35 = load i8*, i8** %9
%36 = call i32 @execvp(i8* %35, i8** %9)
%37 = load i32, i32* @LOG_ERR
%38 = load i8*, i8** %9
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 (i32, i8*, i8*, ...) @logmsg(i32 %37, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %38, i8* %40)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %19
%44 = load i32, i32* @LOG_ERR
%45 = load i32, i32* @errno
%46 = tail call i8* @strerror(i32 %45)
%47 = tail call i32 (i32, i8*, i8*, ...) @logmsg(i32 %44, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %46)
br label %61
48: ; preds = %19
%49 = call i32 @waitpid(i32 %20, i32* %3, i32 0)
%50 = load i32, i32* %3
%51 = call i64 @WIFEXITED(i32 %50)
%52 = icmp eq i64 %51, 0
br i1 %52, label %57, label %53
53: ; preds = %48
%54 = load i32, i32* %3
%55 = call i64 @WEXITSTATUS(i32 %54)
%56 = icmp eq i64 %55, 0
br i1 %56, label %61, label %57
57: ; preds = %53, %48
%58 = load i32, i32* @LOG_INFO
%59 = load i8*, i8** %9
%60 = call i32 (i32, i8*, i8*, ...) @logmsg(i32 %58, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %59)
br label %61
61: ; preds = %43, %57, %53
%62 = call i32 @free(i8* %5)
br label %63
63: ; preds = %61, %17
ret void
}