#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*/ sig_t;
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIGQUIT;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ _PATH_BSHELL;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ execl(int /*<<< orphan*/, char *, char *, char *, char *);
int /*<<< orphan*/ setuid(int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int vfork();
scalar_t__ waitpid(int, int *, int /*<<< orphan*/);
int susystem(char *s, int userid) {
  sig_t istat, qstat;
  int status;
  pid_t pid;
  pid = vfork();
  switch (pid) {
  case -1:
    return (127);
  case 0:
    (void)setuid(userid);
    execl(_PATH_BSHELL, "sh", "-c", s, (char *)NULL);
    _exit(127);
  }
  istat = signal(SIGINT, SIG_IGN);
  qstat = signal(SIGQUIT, SIG_IGN);
  if (waitpid(pid, &status, 0) < 0)
    status = -1;
  (void)signal(SIGINT, istat);
  (void)signal(SIGQUIT, qstat);
  return (status);
}


x86 assembly (clang, -O0):
<susystem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
call   1060 <vfork@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0xffffffff,%eax
je     119b <susystem+0x3b>
jmp    118b <susystem+0x2b>
mov    -0x28(%rbp),%eax
test   %eax,%eax
je     11a7 <susystem+0x47>
jmp    11de <susystem+0x7e>
movl   $0x7f,-0x4(%rbp)
jmp    1259 <susystem+0xf9>
mov    -0x14(%rbp),%edi
call   1070 <setuid@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rcx
lea    0xe3d(%rip),%rsi
lea    0xe39(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1080 <execl@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
call   1040 <signal@plt>
mov    %eax,-0x18(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
call   1040 <signal@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0x0,%rax
jge    1231 <susystem+0xd1>
movl   $0xffffffff,-0x20(%rbp)
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%esi
call   1040 <signal@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%esi
call   1040 <signal@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<susystem>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    $0x7f,%ebx
call   1060 <vfork@plt>
mov    %ebx,%ecx
cmp    $0xffffffff,%eax
je     11ee <susystem+0x8e>
test   %eax,%eax
je     11fd <susystem+0x9d>
mov    %eax,%ebx
mov    0x2e59(%rip),%r15
mov    (%r15),%edi
mov    0x2e37(%rip),%rbp
mov    0x0(%rbp),%esi
call   1040 <signal@plt>
mov    %eax,%r14d
mov    0x2e35(%rip),%r12
mov    (%r12),%edi
mov    0x0(%rbp),%esi
call   1040 <signal@plt>
mov    %eax,%ebp
lea    0xc(%rsp),%rsi
xor    %edx,%edx
mov    %ebx,%edi
call   1050 <waitpid@plt>
test   %rax,%rax
jns    11d4 <susystem+0x74>
movl   $0xffffffff,0xc(%rsp)
ff
mov    (%r15),%edi
mov    %r14d,%esi
call   1040 <signal@plt>
mov    (%r12),%edi
mov    %ebp,%esi
call   1040 <signal@plt>
mov    0xc(%rsp),%ecx
mov    %ecx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
call   1070 <setuid@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
lea    0xde8(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %r14,%rcx
call   1080 <execl@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@_PATH_BSHELL = global i32 0
@.str = [3 x i8] c"sh\00"
@.str.1 = [3 x i8] c"-c\00"
@SIGINT = global i32 0
@SIG_IGN = global i32 0
@SIGQUIT = global i32 0
define i32 @susystem(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = call i32 @vfork()
store i32 %10, i32* %9
%11 = load i32, i32* %9
switch i32 %11, label %20 [
i32 -1, label %12
i32 0, label %13
]
12: ; preds = %2
store i32 127, i32* %3
br label %39
13: ; preds = %2
%14 = load i32, i32* %5
%15 = call i32 @setuid(i32 %14)
%16 = load i32, i32* @_PATH_BSHELL
%17 = load i8*, i8** %4
%18 = call i32 @execl(i32 %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %17, i8* null)
%19 = call i32 @_exit(i32 127)
unreachable
20: ; preds = %2
%21 = load i32, i32* @SIGINT
%22 = load i32, i32* @SIG_IGN
%23 = call i32 @signal(i32 %21, i32 %22)
store i32 %23, i32* %6
%24 = load i32, i32* @SIGQUIT
%25 = load i32, i32* @SIG_IGN
%26 = call i32 @signal(i32 %24, i32 %25)
store i32 %26, i32* %7
%27 = load i32, i32* %9
%28 = call i64 @waitpid(i32 %27, i32* %8, i32 0)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %20
store i32 -1, i32* %8
br label %31
31: ; preds = %30, %20
%32 = load i32, i32* @SIGINT
%33 = load i32, i32* %6
%34 = call i32 @signal(i32 %32, i32 %33)
%35 = load i32, i32* @SIGQUIT
%36 = load i32, i32* %7
%37 = call i32 @signal(i32 %35, i32 %36)
%38 = load i32, i32* %8
store i32 %38, i32* %3
br label %39
39: ; preds = %31, %12
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@_PATH_BSHELL = global i32 0
@.str = [3 x i8] c"sh\00"
@.str.1 = [3 x i8] c"-c\00"
@SIGINT = global i32 0
@SIG_IGN = global i32 0
@SIGQUIT = global i32 0
define i32 @susystem(i8* %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @vfork()
switch i32 %5, label %11 [
i32 -1, label %27
i32 0, label %6
]
6: ; preds = %2
%7 = call i32 @setuid(i32 %1)
%8 = load i32, i32* @_PATH_BSHELL
%9 = call i32 @execl(i32 %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* null)
%10 = call i32 @_exit(i32 127)
unreachable
11: ; preds = %2
%12 = load i32, i32* @SIGINT
%13 = load i32, i32* @SIG_IGN
%14 = call i32 @signal(i32 %12, i32 %13)
%15 = load i32, i32* @SIGQUIT
%16 = load i32, i32* @SIG_IGN
%17 = call i32 @signal(i32 %15, i32 %16)
%18 = call i64 @waitpid(i32 %5, i32* %3, i32 0)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %11
store i32 -1, i32* %3
br label %21
21: ; preds = %20, %11
%22 = load i32, i32* @SIGINT
%23 = call i32 @signal(i32 %22, i32 %14)
%24 = load i32, i32* @SIGQUIT
%25 = call i32 @signal(i32 %24, i32 %17)
%26 = load i32, i32* %3
br label %27
27: ; preds = %2, %21
%28 = phi i32 [ %26, %21 ], [ 127, %2 ]
ret i32 %28
}