#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 */
struct passwd {
  int /*<<< orphan*/ pw_uid;
  int /*<<< orphan*/ pw_gid;
  int /*<<< orphan*/ pw_name;
};

/* Variables and functions */
scalar_t__ errno;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct passwd *getpwnam(char *);
scalar_t__ initgroups(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int main(int argc, char **argv) {
  struct passwd *user;
  if (argc < 3) {
    fprintf(stderr, "no command (usage: setuidgid user cmd args...)\n");
    return 78;
  }
  --argc;
  ++argv;
  errno = 0;
  if ((user = getpwnam(*argv)) == NULL) {
    if (errno == 0) {
      fprintf(stderr, "unknown user:%s\n", *argv);
      return 78;
    } else {
      perror("getpwnam");
      return 71;
    }
  }
  --argc;
  ++argv;
  if (setgid(user->pw_gid) != 0) {
    perror("setgid failed");
    return 71;
  }
  if (initgroups(user->pw_name, user->pw_gid) != 0) {
    perror("initgroups failed");
    return 71;
  }
  if (setuid(user->pw_uid) != 0) {
    perror("setuid failed");
    return 71;
  }
  execvp(*argv, argv);
  fprintf(stderr, "execvp failed to launch file:%s:%s\n", *argv,
          strerror(errno));
  return 71;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11c3 <main+0x43>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x4e,-0x4(%rbp)
jmp    134d <main+0x1cd>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    0x2df9(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1253 <main+0xd3>
mov    0x2dd1(%rip),%rax
cmpq   $0x0,(%rax)
jne    123b <main+0xbb>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe08(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x4e,-0x4(%rbp)
jmp    134d <main+0x1cd>
lea    0xdff(%rip),%rdi
call   1060 <perror@plt>
movl   $0x47,-0x4(%rbp)
jmp    134d <main+0x1cd>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   1050 <setgid@plt>
cmp    $0x0,%rax
je     1296 <main+0x116>
lea    0xdc5(%rip),%rdi
call   1060 <perror@plt>
movl   $0x47,-0x4(%rbp)
jmp    134d <main+0x1cd>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
call   10a0 <initgroups@plt>
cmp    $0x0,%rax
je     12cb <main+0x14b>
lea    0xd9e(%rip),%rdi
call   1060 <perror@plt>
movl   $0x47,-0x4(%rbp)
jmp    134d <main+0x1cd>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1080 <setuid@plt>
cmp    $0x0,%rax
je     12f8 <main+0x178>
lea    0xd83(%rip),%rdi
call   1060 <perror@plt>
movl   $0x47,-0x4(%rbp)
jmp    134d <main+0x1cd>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <execvp@plt>
mov    0x2cd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2cb2(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xd39(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x47,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
cmp    $0x2,%edi
jg     11a6 <main+0x26>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    125e <main+0xde>
mov    %rsi,%r14
mov    0x2e28(%rip),%r15
movq   $0x0,(%r15)
mov    0x8(%rsi),%rdi
call   1030 <getpwnam@plt>
test   %rax,%rax
je     123d <main+0xbd>
mov    %rax,%rbx
mov    0x4(%rax),%edi
call   1050 <setgid@plt>
test   %rax,%rax
jne    1269 <main+0xe9>
mov    0x4(%rbx),%esi
mov    0x8(%rbx),%edi
call   10a0 <initgroups@plt>
test   %rax,%rax
jne    1277 <main+0xf7>
mov    (%rbx),%edi
call   1080 <setuid@plt>
test   %rax,%rax
jne    1285 <main+0x105>
add    $0x10,%r14
mov    (%r14),%rdi
mov    %r14,%rsi
call   1070 <execvp@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%r14
mov    (%r15),%rdi
call   1090 <strerror@plt>
lea    0xe51(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x47,%eax
jmp    1263 <main+0xe3>
cmpq   $0x0,(%r15)
jne    1293 <main+0x113>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xdd9(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x4e,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdda(%rip),%rdi
call   1060 <perror@plt>
jmp    1236 <main+0xb6>
lea    0xdda(%rip),%rdi
call   1060 <perror@plt>
jmp    1236 <main+0xb6>
lea    0xdde(%rip),%rdi
call   1060 <perror@plt>
jmp    1236 <main+0xb6>
lea    0xda7(%rip),%rdi
call   1060 <perror@plt>
jmp    1236 <main+0xb6>

LLVM IR (-O0):
%struct.passwd = type { i32, i32, i32 }
@stderr = global i32 0
@.str = [48 x i8] c"no command (usage: setuidgid user cmd args...)\0A\00"
@errno = global i64 0
@.str.1 = [17 x i8] c"unknown user:%s\0A\00"
@.str.2 = [9 x i8] c"getpwnam\00"
@.str.3 = [14 x i8] c"setgid failed\00"
@.str.4 = [18 x i8] c"initgroups failed\00"
@.str.5 = [14 x i8] c"setuid failed\00"
@.str.6 = [36 x i8] c"execvp failed to launch file:%s:%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.passwd*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 3
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
store i32 78, i32* %3
br label %73
12: ; preds = %2
%13 = load i32, i32* %4
%14 = add nsw i32 %13, -1
store i32 %14, i32* %4
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i32 1
store i8** %16, i8*** %5
store i64 0, i64* @errno
%17 = load i8**, i8*** %5
%18 = load i8*, i8** %17
%19 = call %struct.passwd* @getpwnam(i8* %18)
store %struct.passwd* %19, %struct.passwd** %6
%20 = icmp eq %struct.passwd* %19, null
br i1 %20, label %21, label %31
21: ; preds = %12
%22 = load i64, i64* @errno
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = load i8**, i8*** %5
%27 = load i8*, i8** %26
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %27)
store i32 78, i32* %3
br label %73
29: ; preds = %21
%30 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
store i32 71, i32* %3
br label %73
31: ; preds = %12
%32 = load i32, i32* %4
%33 = add nsw i32 %32, -1
store i32 %33, i32* %4
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i32 1
store i8** %35, i8*** %5
%36 = load %struct.passwd*, %struct.passwd** %6
%37 = getelementptr inbounds %struct.passwd, %struct.passwd* %36, i32 0, i32 1
%38 = load i32, i32* %37
%39 = call i64 @setgid(i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %31
%42 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 71, i32* %3
br label %73
43: ; preds = %31
%44 = load %struct.passwd*, %struct.passwd** %6
%45 = getelementptr inbounds %struct.passwd, %struct.passwd* %44, i32 0, i32 2
%46 = load i32, i32* %45
%47 = load %struct.passwd*, %struct.passwd** %6
%48 = getelementptr inbounds %struct.passwd, %struct.passwd* %47, i32 0, i32 1
%49 = load i32, i32* %48
%50 = call i64 @initgroups(i32 %46, i32 %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %43
%53 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
store i32 71, i32* %3
br label %73
54: ; preds = %43
%55 = load %struct.passwd*, %struct.passwd** %6
%56 = getelementptr inbounds %struct.passwd, %struct.passwd* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i64 @setuid(i32 %57)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %54
%61 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
store i32 71, i32* %3
br label %73
62: ; preds = %54
%63 = load i8**, i8*** %5
%64 = load i8*, i8** %63
%65 = load i8**, i8*** %5
%66 = call i32 @execvp(i8* %64, i8** %65)
%67 = load i32, i32* @stderr
%68 = load i8**, i8*** %5
%69 = load i8*, i8** %68
%70 = load i64, i64* @errno
%71 = call i8* @strerror(i64 %70)
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i8* %69, i8* %71)
store i32 71, i32* %3
br label %73
73: ; preds = %62, %60, %52, %41, %29, %24, %9
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32, i32 }
@stderr = global i32 0
@.str = [48 x i8] c"no command (usage: setuidgid user cmd args...)\0A\00"
@errno = global i64 0
@.str.1 = [17 x i8] c"unknown user:%s\0A\00"
@.str.2 = [9 x i8] c"getpwnam\00"
@.str.3 = [14 x i8] c"setgid failed\00"
@.str.4 = [18 x i8] c"initgroups failed\00"
@.str.5 = [14 x i8] c"setuid failed\00"
@.str.6 = [36 x i8] c"execvp failed to launch file:%s:%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
br label %52
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
store i64 0, i64* @errno
%9 = load i8*, i8** %8
%10 = tail call %struct.passwd* @getpwnam(i8* %9)
%11 = icmp eq %struct.passwd* %10, null
br i1 %11, label %12, label %21
12: ; preds = %7
%13 = load i64, i64* @errno
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %8
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %17)
br label %52
19: ; preds = %12
%20 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %52
21: ; preds = %7
%22 = getelementptr inbounds i8*, i8** %1, i64 2
%23 = getelementptr inbounds %struct.passwd, %struct.passwd* %10, i64 0, i32 1
%24 = load i32, i32* %23
%25 = tail call i64 @setgid(i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %21
%28 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %52
29: ; preds = %21
%30 = getelementptr inbounds %struct.passwd, %struct.passwd* %10, i64 0, i32 2
%31 = load i32, i32* %30
%32 = load i32, i32* %23
%33 = tail call i64 @initgroups(i32 %31, i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %29
%36 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %52
37: ; preds = %29
%38 = getelementptr inbounds %struct.passwd, %struct.passwd* %10, i64 0, i32 0
%39 = load i32, i32* %38
%40 = tail call i64 @setuid(i32 %39)
%41 = icmp eq i64 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %37
%43 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %52
44: ; preds = %37
%45 = load i8*, i8** %22
%46 = tail call i32 @execvp(i8* %45, i8** %22)
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %22
%49 = load i64, i64* @errno
%50 = tail call i8* @strerror(i64 %49)
%51 = tail call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i8* %48, i8* %50)
br label %52
52: ; preds = %44, %42, %35, %27, %19, %15, %4
%53 = phi i32 [ 78, %4 ], [ 78, %15 ], [ 71, %19 ], [ 71, %27 ], [ 71, %35 ], [ 71, %42 ], [ 71, %44 ]
ret i32 %53
}