#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 execve(char *, char **, char **);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setgid(int);
int /*<<< orphan*/ setuid(int);
int create_proc_as_user(int gid, int uid, char *argv[], char *envp[]) {
  pid_t pid = fork();
  if (0 == pid) {
    setgid(gid);
    setuid(uid);
    int ret = execve(argv[0], &argv[0], envp);
    if (ret != 0) {
      perror("create_proc_as_user(), in child process");
      return ret;
    }
  } else if (pid > 0) {
  } else {
    perror("create_proc_as_user, fork()");
    return pid;
  }
  return 0;
}


x86 assembly (clang, -O0):
<create_proc_as_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    11ca <create_proc_as_user+0x7a>
mov    -0x8(%rbp),%edi
call   1040 <setgid@plt>
mov    -0xc(%rbp),%edi
call   1060 <setuid@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <execve@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11c5 <create_proc_as_user+0x75>
lea    0xe4b(%rip),%rdi
call   1050 <perror@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fe <create_proc_as_user+0xae>
jmp    11f7 <create_proc_as_user+0xa7>
cmpq   $0x0,-0x28(%rbp)
jle    11da <create_proc_as_user+0x8a>
jmp    11f2 <create_proc_as_user+0xa2>
lea    0xe47(%rip),%rdi
call   1050 <perror@plt>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11fe <create_proc_as_user+0xae>
jmp    11f7 <create_proc_as_user+0xa7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_proc_as_user>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r13
mov    %esi,%r15d
mov    %edi,%ebx
xor    %r12d,%r12d
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     118b <create_proc_as_user+0x3b>
jg     11ad <create_proc_as_user+0x5d>
mov    %rax,%rbp
lea    0xea7(%rip),%rdi
call   1050 <perror@plt>
mov    %ebp,%r12d
jmp    11ad <create_proc_as_user+0x5d>
mov    %ebx,%edi
call   1040 <setgid@plt>
mov    %r15d,%edi
call   1060 <setuid@plt>
mov    0x0(%r13),%rdi
mov    %r13,%rsi
mov    %r14,%rdx
call   1030 <execve@plt>
test   %eax,%eax
jne    11bf <create_proc_as_user+0x6f>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebx
lea    0xe38(%rip),%rdi
call   1050 <perror@plt>
mov    %ebx,%r12d
jmp    11ad <create_proc_as_user+0x5d>

LLVM IR (-O0):
@.str = [40 x i8] c"create_proc_as_user(), in child process\00"
@.str.1 = [28 x i8] c"create_proc_as_user, fork()\00"
define i32 @create_proc_as_user(i32 %0, i32 %1, i8** %2, i8** %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8**
%9 = alloca i8**
%10 = alloca i64
%11 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8** %2, i8*** %8
store i8** %3, i8*** %9
%12 = call i64 (...) @fork()
store i64 %12, i64* %10
%13 = load i64, i64* %10
%14 = icmp eq i64 0, %13
br i1 %14, label %15, label %33
15: ; preds = %4
%16 = load i32, i32* %6
%17 = call i32 @setgid(i32 %16)
%18 = load i32, i32* %7
%19 = call i32 @setuid(i32 %18)
%20 = load i8**, i8*** %8
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = load i8**, i8*** %8
%24 = getelementptr inbounds i8*, i8** %23, i64 0
%25 = load i8**, i8*** %9
%26 = call i32 @execve(i8* %22, i8** %24, i8** %25)
store i32 %26, i32* %11
%27 = load i32, i32* %11
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %15
%30 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%31 = load i32, i32* %11
store i32 %31, i32* %5
br label %43
32: ; preds = %15
br label %42
33: ; preds = %4
%34 = load i64, i64* %10
%35 = icmp sgt i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %33
br label %41
37: ; preds = %33
%38 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%39 = load i64, i64* %10
%40 = trunc i64 %39 to i32
store i32 %40, i32* %5
br label %43
41: ; preds = %36
br label %42
42: ; preds = %41, %32
store i32 0, i32* %5
br label %43
43: ; preds = %42, %37, %29
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
@.str = [40 x i8] c"create_proc_as_user(), in child process\00"
@.str.1 = [28 x i8] c"create_proc_as_user, fork()\00"
define i32 @create_proc_as_user(i32 %0, i32 %1, i8** %2, i8** %3) {
%5 = tail call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %15
7: ; preds = %4
%8 = tail call i32 @setgid(i32 %0)
%9 = tail call i32 @setuid(i32 %1)
%10 = load i8*, i8** %2
%11 = tail call i32 @execve(i8* %10, i8** %2, i8** %3)
%12 = icmp eq i32 %11, 0
br i1 %12, label %20, label %13
13: ; preds = %7
%14 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %20
15: ; preds = %4
%16 = icmp sgt i64 %5, 0
br i1 %16, label %20, label %17
17: ; preds = %15
%18 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%19 = trunc i64 %5 to i32
br label %20
20: ; preds = %15, %7, %13, %17
%21 = phi i32 [ %11, %13 ], [ %19, %17 ], [ 0, %7 ], [ 0, %15 ]
ret i32 %21
}