#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;

/* Variables and functions */
int /*<<< orphan*/ execl(char *, char *, char *, char *, char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getpid();
int /*<<< orphan*/ setgid(unsigned short);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write_track_xid(int, scalar_t__, char *);
pid_t fork_actual_command(int type, unsigned short id, char *stats_path,
                          char *command) {
  pid_t pid;
  if ((pid = fork()) < 0)
    fprintf(stderr, "Error: Fork error\n");
  if (pid == 0) {
    switch (type) {
    case 0:
      if (setgid(id) < 0) {
        fprintf(stderr, "Error: failed to"
                        " setgid\n");
        exit(1);
      }
      pid = id;
      break;
    case 2:
    case 1:
      pid = getpid();
      break;
    case 3:
      pid = 0;
      type = 2;
      break;
    }
    write_track_xid(type, pid, stats_path);
    execl("/bin/sh", "sh", "-c", command, (char *)0);
    exit(0);
  }
  return (pid);
}


x86 assembly (clang, -O0):
<fork_actual_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %edi,-0x4(%rbp)
mov    %ax,-0x6(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    11b4 <fork_actual_command+0x44>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
call   1050 <fprintf@plt>
cmpq   $0x0,-0x20(%rbp)
jne    128e <fork_actual_command+0x11e>
mov    -0x4(%rbp),%eax
mov    %eax,-0x24(%rbp)
test   %eax,%eax
je     11f7 <fork_actual_command+0x87>
jmp    11d2 <fork_actual_command+0x62>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
sub    $0x2,%eax
jb     1235 <fork_actual_command+0xc5>
jmp    11e6 <fork_actual_command+0x76>
mov    -0x24(%rbp),%eax
sub    $0x3,%eax
je     1245 <fork_actual_command+0xd5>
jmp    1254 <fork_actual_command+0xe4>
movzwl -0x6(%rbp),%edi
call   1060 <setgid@plt>
cmp    $0x0,%eax
jge    1228 <fork_actual_command+0xb8>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movzwl -0x6(%rbp),%eax
mov    %rax,-0x20(%rbp)
jmp    1254 <fork_actual_command+0xe4>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x20(%rbp)
jmp    1254 <fork_actual_command+0xe4>
movq   $0x0,-0x20(%rbp)
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <write_track_xid@plt>
mov    -0x18(%rbp),%rcx
lea    0xdbd(%rip),%rdi
lea    0xdbb(%rip),%rsi
lea    0xdb7(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1080 <execl@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_actual_command>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%ebp
mov    %edi,%r13d
xor    %eax,%eax
call   1090 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
js     11da <fork_actual_command+0x6a>
jne    11ef <fork_actual_command+0x7f>
lea    -0x1(%r13),%eax
cmp    $0x2,%eax
jb     1201 <fork_actual_command+0x91>
cmp    $0x3,%r13d
je     120d <fork_actual_command+0x9d>
test   %r13d,%r13d
jne    1213 <fork_actual_command+0xa3>
movzwl %bp,%r12d
mov    %r12d,%edi
call   1060 <setgid@plt>
test   %eax,%eax
jns    1218 <fork_actual_command+0xa8>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe48(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
call   1050 <fprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%r12
jmp    121b <fork_actual_command+0xab>
mov    $0x2,%r13d
xor    %r12d,%r12d
jmp    121b <fork_actual_command+0xab>
xor    %r13d,%r13d
mov    %r13d,%edi
mov    %r12,%rsi
mov    %r15,%rdx
call   1040 <write_track_xid@plt>
lea    0xdfc(%rip),%rdi
lea    0xdfa(%rip),%rsi
lea    0xdf6(%rip),%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
call   1080 <execl@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [19 x i8] c"Error: Fork error\0A\00"
@.str.1 = [25 x i8] c"Error: failed to setgid\0A\00"
@.str.2 = [8 x i8] c"/bin/sh\00"
@.str.3 = [3 x i8] c"sh\00"
@.str.4 = [3 x i8] c"-c\00"
define i64 @fork_actual_command(i32 %0, i16 zeroext %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i16
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
store i32 %0, i32* %5
store i16 %1, i16* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%10 = call i64 (...) @fork()
store i64 %10, i64* %9
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %4
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %12, %4
%16 = load i64, i64* %9
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %42
18: ; preds = %15
%19 = load i32, i32* %5
switch i32 %19, label %34 [
i32 0, label %20
i32 2, label %31
i32 1, label %31
i32 3, label %33
]
20: ; preds = %18
%21 = load i16, i16* %6
%22 = call i32 @setgid(i16 zeroext %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %20
%29 = load i16, i16* %6
%30 = zext i16 %29 to i64
store i64 %30, i64* %9
br label %34
31: ; preds = %18, %18
%32 = call i64 (...) @getpid()
store i64 %32, i64* %9
br label %34
33: ; preds = %18
store i64 0, i64* %9
store i32 2, i32* %5
br label %34
34: ; preds = %18, %33, %31, %28
%35 = load i32, i32* %5
%36 = load i64, i64* %9
%37 = load i8*, i8** %7
%38 = call i32 @write_track_xid(i32 %35, i64 %36, i8* %37)
%39 = load i8*, i8** %8
%40 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %39, i8* null)
%41 = call i32 @exit(i32 0)
unreachable
42: ; preds = %15
%43 = load i64, i64* %9
ret i64 %43
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"Error: Fork error\0A\00"
@.str.1 = [25 x i8] c"Error: failed to setgid\0A\00"
@.str.2 = [8 x i8] c"/bin/sh\00"
@.str.3 = [3 x i8] c"sh\00"
@.str.4 = [3 x i8] c"-c\00"
define i64 @fork_actual_command(i32 %0, i16 zeroext %1, i8* %2, i8* %3) {
%5 = tail call i64 (...) @fork()
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %31
10: ; preds = %4
%11 = icmp eq i64 %5, 0
br i1 %11, label %12, label %31
12: ; preds = %10
switch i32 %0, label %25 [
i32 0, label %13
i32 2, label %22
i32 1, label %22
i32 3, label %24
]
13: ; preds = %12
%14 = tail call i32 @setgid(i16 zeroext %1)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = zext i16 %1 to i64
br label %25
22: ; preds = %12, %12
%23 = tail call i64 (...) @getpid()
br label %25
24: ; preds = %12
br label %25
25: ; preds = %12, %24, %22, %20
%26 = phi i32 [ %0, %12 ], [ 2, %24 ], [ %0, %22 ], [ 0, %20 ]
%27 = phi i64 [ 0, %12 ], [ 0, %24 ], [ %23, %22 ], [ %21, %20 ]
%28 = tail call i32 @write_track_xid(i32 %26, i64 %27, i8* %2)
%29 = tail call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %3, i8* null)
%30 = tail call i32 @exit(i32 0)
unreachable
31: ; preds = %7, %10
ret i64 %5
}