#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_gid;
  int /*<<< orphan*/ pw_uid;
  int /*<<< orphan*/ pw_dir;
};

/* Variables and functions */
int asprintf(char *const *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ execve(char *const, char *const *, char *const *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int geteuid();
struct passwd *getpwuid(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setregid(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ setreuid(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int main(int argc, char *argv[], char *envp[]) {
  char *const smart_argv[] = {"/usr/bin/smart", "update", NULL, NULL};
  char *const smart_envp[] = {"PATH=/bin:/usr/bin", "HOME=", NULL};
  struct passwd *pwd = getpwuid(geteuid());
  if (!pwd) {
    fprintf(stderr, "error: Unable to find passwd entry for uid %d\n",
            geteuid());
    exit(1);
  }
  if (asprintf(&smart_envp[1], "HOME=%s", pwd->pw_dir) == -1) {
    fprintf(stderr, "error: Unable to create HOME environment variable\n");
    exit(1);
  }
  if (argc == 3 && strcmp(argv[1], "--after") == 0) {
    if (asprintf(&smart_argv[2], "--after=%d", atoi(argv[2])) == -1) {
      fprintf(stderr, "error: Unable to create argument variable\n");
      exit(1);
    }
  }
  setreuid(pwd->pw_uid, pwd->pw_uid);
  setregid(pwd->pw_gid, pwd->pw_gid);
  execve(smart_argv[0], smart_argv, smart_envp);
  perror("error: Unable to execute smart");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2c0c(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2c09(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0x2c06(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2c03(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2c00(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    0x2bfd(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    0x2bfa(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jne    1267 <main+0xb7>
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    -0x6c(%rbp),%edi
mov    %eax,%edx
lea    0xdd9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x60(%rbp),%rdi
add    $0x8,%rdi
mov    -0x68(%rbp),%rax
mov    0x8(%rax),%edx
lea    0xde1(%rip),%rsi
call   10a0 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    12ac <main+0xfc>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
cmpl   $0x3,-0x8(%rbp)
jne    132e <main+0x17e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdd4(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    132e <main+0x17e>
lea    -0x40(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x78(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    -0x78(%rbp),%rdi
mov    %eax,%edx
lea    0xda7(%rip),%rsi
call   10a0 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    1329 <main+0x179>
mov    0x2cd1(%rip),%rax
mov    (%rax),%edi
lea    0xd94(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    132e <main+0x17e>
mov    -0x68(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x68(%rbp),%rax
mov    0x4(%rax),%esi
call   1080 <setreuid@plt>
mov    -0x68(%rbp),%rax
mov    (%rax),%edi
mov    -0x68(%rbp),%rax
mov    (%rax),%esi
call   1090 <setregid@plt>
mov    -0x40(%rbp),%rdi
lea    -0x40(%rbp),%rsi
lea    -0x60(%rbp),%rdx
call   1050 <execve@plt>
lea    0xd6d(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
movaps 0x2c2c(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0x2c10(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
mov    0x2c34(%rip),%rax
mov    %rax,0x10(%rsp)
movaps 0x2c18(%rip),%xmm0
movaps %xmm0,(%rsp)
xor    %eax,%eax
call   1040 <geteuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
test   %rax,%rax
je     12a4 <main+0xf4>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
mov    0x8(%rax),%edx
lea    0xe49(%rip),%rsi
call   10a0 <asprintf@plt>
cmp    $0xffffffff,%eax
je     12d0 <main+0x120>
cmp    $0x3,%ebp
jne    1265 <main+0xb5>
mov    0x8(%r14),%rdi
lea    0xe66(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    1265 <main+0xb5>
lea    0x30(%rsp),%rbp
mov    0x10(%r14),%rdi
call   10c0 <atoi@plt>
lea    0xe4f(%rip),%rsi
mov    %rbp,%rdi
mov    %eax,%edx
call   10a0 <asprintf@plt>
cmp    $0xffffffff,%eax
je     12f1 <main+0x141>
mov    0x4(%rbx),%edi
mov    %edi,%esi
call   1080 <setreuid@plt>
mov    (%rbx),%edi
mov    %edi,%esi
call   1090 <setregid@plt>
mov    0x20(%rsp),%rdi
lea    0x20(%rsp),%rsi
mov    %rsp,%rdx
call   1050 <execve@plt>
lea    0xe46(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d35(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1040 <geteuid@plt>
lea    0xd74(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
lea    0xd86(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
lea    0xdab(%rip),%rsi
jmp    12e0 <main+0x130>

LLVM IR (-O0):
%struct.passwd = type { i32, i32, i32 }
@.str = [15 x i8] c"/usr/bin/smart\00"
@.str.1 = [7 x i8] c"update\00"
@__const.main.smart_argv = [4 x i8*] [i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i32 0, i32 0), i8* null, i8* null]
@.str.2 = [19 x i8] c"PATH=/bin:/usr/bin\00"
@.str.3 = [6 x i8] c"HOME=\00"
@__const.main.smart_envp = [3 x i8*] [i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i32 0, i32 0), i8* null]
@stderr = global i32 0
@.str.4 = [47 x i8] c"error: Unable to find passwd entry for uid %d\0A\00"
@.str.5 = [8 x i8] c"HOME=%s\00"
@.str.6 = [51 x i8] c"error: Unable to create HOME environment variable\0A\00"
@.str.7 = [8 x i8] c"--after\00"
@.str.8 = [11 x i8] c"--after=%d\00"
@.str.9 = [43 x i8] c"error: Unable to create argument variable\0A\00"
@.str.10 = [31 x i8] c"error: Unable to execute smart\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca [4 x i8*]
%9 = alloca [3 x i8*]
%10 = alloca %struct.passwd*
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%11 = bitcast [4 x i8*]* %8 to i8*
%12 = bitcast [3 x i8*]* %9 to i8*
%13 = call i32 (...) @geteuid()
%14 = call %struct.passwd* @getpwuid(i32 %13)
store %struct.passwd* %14, %struct.passwd** %10
%15 = load %struct.passwd*, %struct.passwd** %10
%16 = icmp ne %struct.passwd* %15, null
br i1 %16, label %22, label %17
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = call i32 (...) @geteuid()
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %3
%23 = getelementptr inbounds [3 x i8*], [3 x i8*]* %9, i64 0, i64 1
%24 = load %struct.passwd*, %struct.passwd** %10
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %24, i32 0, i32 2
%26 = load i32, i32* %25
%27 = call i32 @asprintf(i8** %23, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %22
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.6, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %22
%34 = load i32, i32* %5
%35 = icmp eq i32 %34, 3
br i1 %35, label %36, label %55
36: ; preds = %33
%37 = load i8**, i8*** %6
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = load i8*, i8** %38
%40 = call i64 @strcmp(i8* %39, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %55
42: ; preds = %36
%43 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 2
%44 = load i8**, i8*** %6
%45 = getelementptr inbounds i8*, i8** %44, i64 2
%46 = load i8*, i8** %45
%47 = call i32 @atoi(i8* %46)
%48 = call i32 @asprintf(i8** %43, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0), i32 %47)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %54
50: ; preds = %42
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.9, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %42
br label %55
55: ; preds = %54, %36, %33
%56 = load %struct.passwd*, %struct.passwd** %10
%57 = getelementptr inbounds %struct.passwd, %struct.passwd* %56, i32 0, i32 1
%58 = load i32, i32* %57
%59 = load %struct.passwd*, %struct.passwd** %10
%60 = getelementptr inbounds %struct.passwd, %struct.passwd* %59, i32 0, i32 1
%61 = load i32, i32* %60
%62 = call i32 @setreuid(i32 %58, i32 %61)
%63 = load %struct.passwd*, %struct.passwd** %10
%64 = getelementptr inbounds %struct.passwd, %struct.passwd* %63, i32 0, i32 0
%65 = load i32, i32* %64
%66 = load %struct.passwd*, %struct.passwd** %10
%67 = getelementptr inbounds %struct.passwd, %struct.passwd* %66, i32 0, i32 0
%68 = load i32, i32* %67
%69 = call i32 @setregid(i32 %65, i32 %68)
%70 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 0
%71 = load i8*, i8** %70
%72 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 0
%73 = getelementptr inbounds [3 x i8*], [3 x i8*]* %9, i64 0, i64 0
%74 = call i32 @execve(i8* %71, i8** %72, i8** %73)
%75 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.10, i64 0, i64 0))
ret i32 1
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32, i32 }
@.str = [15 x i8] c"/usr/bin/smart\00"
@.str.1 = [7 x i8] c"update\00"
@__const.main.smart_argv = [4 x i8*] [i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i32 0, i32 0), i8* null, i8* null]
@.str.2 = [19 x i8] c"PATH=/bin:/usr/bin\00"
@.str.3 = [6 x i8] c"HOME=\00"
@__const.main.smart_envp = [3 x i8*] [i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i32 0, i32 0), i8* null]
@stderr = global i32 0
@.str.4 = [47 x i8] c"error: Unable to find passwd entry for uid %d\0A\00"
@.str.5 = [8 x i8] c"HOME=%s\00"
@.str.6 = [51 x i8] c"error: Unable to create HOME environment variable\0A\00"
@.str.7 = [8 x i8] c"--after\00"
@.str.8 = [11 x i8] c"--after=%d\00"
@.str.9 = [43 x i8] c"error: Unable to create argument variable\0A\00"
@.str.10 = [31 x i8] c"error: Unable to execute smart\00"
define i32 @main(i32 %0, i8** nocapture %1, i8** nocapture readnone %2) {
%4 = alloca [4 x i8*]
%5 = alloca [3 x i8*]
%6 = bitcast [4 x i8*]* %4 to i8*
%7 = bitcast [3 x i8*]* %5 to i8*
%8 = tail call i32 (...) @geteuid()
%9 = tail call %struct.passwd* @getpwuid(i32 %8)
%10 = icmp eq %struct.passwd* %9, null
br i1 %10, label %11, label %16
11: ; preds = %3
%12 = load i32, i32* @stderr
%13 = tail call i32 (...) @geteuid()
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %13)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %3
%17 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 1
%18 = getelementptr inbounds %struct.passwd, %struct.passwd* %9, i64 0, i32 2
%19 = load i32, i32* %18
%20 = call i32 @asprintf(i8** %17, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %16
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.6, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %16
%27 = icmp eq i32 %0, 3
br i1 %27, label %28, label %44
28: ; preds = %26
%29 = getelementptr inbounds i8*, i8** %1, i64 1
%30 = load i8*, i8** %29
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %44
33: ; preds = %28
%34 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 2
%35 = getelementptr inbounds i8*, i8** %1, i64 2
%36 = load i8*, i8** %35
%37 = call i32 @atoi(i8* %36)
%38 = call i32 @asprintf(i8** %34, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0), i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %44
40: ; preds = %33
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.9, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %33, %28, %26
%45 = getelementptr inbounds %struct.passwd, %struct.passwd* %9, i64 0, i32 1
%46 = load i32, i32* %45
%47 = call i32 @setreuid(i32 %46, i32 %46)
%48 = getelementptr inbounds %struct.passwd, %struct.passwd* %9, i64 0, i32 0
%49 = load i32, i32* %48
%50 = call i32 @setregid(i32 %49, i32 %49)
%51 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 0
%52 = load i8*, i8** %51
%53 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 0
%54 = call i32 @execve(i8* %52, i8** %51, i8** %53)
%55 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.10, i64 0, i64 0))
ret i32 1
}