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

/* Variables and functions */
int /*<<< orphan*/ asprintf(char **, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int getopt(int, char **, char *);
struct passwd *getpwnam(char *);
char *optarg;
int /*<<< orphan*/ perror(char *);
int pg_db_reset(char *, int);
int pg_dump_exec(char *);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  struct passwd *passwd_entry;
  char *user = NULL, *db_name = NULL;
  int db_dump = 0;
  int opt;
  while ((opt = getopt(argc, argv, "d:u:b")) != -1) {
    switch (opt) {
    case 'b':
      db_dump = 1;
      break;
    case 'd':
      db_name = optarg;
      break;
    case 'u':
      user = optarg;
      break;
    default:
      fprintf(stderr, "Usage: %s [-b] [-d database] [-u user]\n", argv[0]);
    }
  }
  if (!user)
    asprintf(&user, "postgres");
  if (!db_name)
    asprintf(&db_name, "improveesation");
  passwd_entry = getpwnam(user);
  if (!passwd_entry) {
    perror("getpwnam");
    return 1;
  }
  if (setuid(passwd_entry->pw_uid) || setgid(passwd_entry->pw_gid)) {
    perror("set[u|g]id");
    return 1;
  }
  if (db_dump) {
    if (fork())
      return pg_dump_exec(db_name);
    else
      wait(NULL);
  }
  if (fork())
    return pg_db_reset(db_name, db_dump);
  else
    wait(NULL);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe15(%rip),%rdx
call   10a0 <getopt@plt>
mov    %eax,-0x30(%rbp)
cmp    $0xffffffff,%eax
je     1285 <main+0xd5>
mov    -0x30(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0x62,%eax
je     1232 <main+0x82>
jmp    1210 <main+0x60>
mov    -0x34(%rbp),%eax
sub    $0x64,%eax
je     123e <main+0x8e>
jmp    1221 <main+0x71>
mov    -0x34(%rbp),%eax
sub    $0x75,%eax
je     1251 <main+0xa1>
jmp    1264 <main+0xb4>
movl   $0x1,-0x2c(%rbp)
jmp    1280 <main+0xd0>
mov    0x2db3(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    1280 <main+0xd0>
mov    0x2da0(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1280 <main+0xd0>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd8b(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11dd <main+0x2d>
cmpq   $0x0,-0x20(%rbp)
jne    12a0 <main+0xf0>
lea    -0x20(%rbp),%rdi
lea    0xd93(%rip),%rsi
call   1060 <asprintf@plt>
cmpq   $0x0,-0x28(%rbp)
jne    12bb <main+0x10b>
lea    -0x28(%rbp),%rdi
lea    0xd81(%rip),%rsi
call   1060 <asprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12eb <main+0x13b>
lea    0xd6c(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    139d <main+0x1ed>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   10b0 <setuid@plt>
cmp    $0x0,%rax
jne    1316 <main+0x166>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1080 <setgid@plt>
cmp    $0x0,%rax
je     132e <main+0x17e>
lea    0xd32(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    139d <main+0x1ed>
cmpl   $0x0,-0x2c(%rbp)
je     1368 <main+0x1b8>
mov    $0x0,%al
call   10d0 <fork@plt>
cmp    $0x0,%rax
je     135a <main+0x1aa>
mov    -0x28(%rbp),%rdi
call   1030 <pg_dump_exec@plt>
mov    %eax,-0x4(%rbp)
jmp    139d <main+0x1ed>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
jmp    1368 <main+0x1b8>
mov    $0x0,%al
call   10d0 <fork@plt>
cmp    $0x0,%rax
je     138d <main+0x1dd>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1070 <pg_db_reset@plt>
mov    %eax,-0x4(%rbp)
jmp    139d <main+0x1ed>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
mov    %rax,0x18(%rsp)
lea    0xe2f(%rip),%r15
mov    0x2e00(%rip),%r14
lea    0xe27(%rip),%r12
xor    %r13d,%r13d
movl   $0x0,0xc(%rsp)
jmp    11fe <main+0x4e>
nopl   0x0(%rax)
mov    (%r14),%edi
mov    (%rbx),%rdx
mov    %r12,%rsi
call   1050 <fprintf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   10a0 <getopt@plt>
cmp    $0x63,%eax
jg     1230 <main+0x80>
cmp    $0xffffffff,%eax
je     1257 <main+0xa7>
cmp    $0x62,%eax
jne    11f0 <main+0x40>
movl   $0x1,0xc(%rsp)
jmp    11fe <main+0x4e>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    $0x75,%eax
je     124b <main+0x9b>
cmp    $0x64,%eax
jne    11f0 <main+0x40>
mov    0x2db7(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x18(%rsp)
jmp    11fe <main+0x4e>
mov    0x2da6(%rip),%rax
mov    (%rax),%r13
jmp    11fe <main+0x4e>
mov    %r13,0x20(%rsp)
mov    0x18(%rsp),%rbx
mov    %rbx,0x10(%rsp)
test   %r13,%r13
jne    127c <main+0xcc>
lea    0xdbc(%rip),%rsi
lea    0x20(%rsp),%rdi
call   1060 <asprintf@plt>
test   %rbx,%rbx
mov    0xc(%rsp),%ebp
jne    1296 <main+0xe6>
lea    0xdab(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1060 <asprintf@plt>
mov    0x20(%rsp),%rdi
call   1040 <getpwnam@plt>
test   %rax,%rax
je     1316 <main+0x166>
mov    %rax,%rbx
mov    0x4(%rax),%edi
call   10b0 <setuid@plt>
test   %rax,%rax
jne    130d <main+0x15d>
mov    (%rbx),%edi
call   1080 <setgid@plt>
test   %rax,%rax
jne    130d <main+0x15d>
test   %ebp,%ebp
je     12e6 <main+0x136>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     12df <main+0x12f>
mov    0x10(%rsp),%rdi
call   1030 <pg_dump_exec@plt>
mov    %eax,%ebx
jmp    1327 <main+0x177>
xor    %edi,%edi
call   10c0 <wait@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     1304 <main+0x154>
mov    0x10(%rsp),%rdi
mov    %ebp,%esi
call   1070 <pg_db_reset@plt>
mov    %eax,%ebx
jmp    1327 <main+0x177>
xor    %edi,%edi
call   10c0 <wait@plt>
jmp    1327 <main+0x177>
lea    0xd3b(%rip),%rdi
jmp    131d <main+0x16d>
lea    0xd29(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@.str = [6 x i8] c"d:u:b\00"
@optarg = global i8* null
@stderr = global i32 0
@.str.1 = [40 x i8] c"Usage: %s [-b] [-d database] [-u user]\0A\00"
@.str.2 = [9 x i8] c"postgres\00"
@.str.3 = [15 x i8] c"improveesation\00"
@.str.4 = [9 x i8] c"getpwnam\00"
@.str.5 = [11 x i8] c"set[u|g]id\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.passwd*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %7
store i8* null, i8** %8
store i32 0, i32* %9
br label %11
11: ; preds = %29, %2
%12 = load i32, i32* %4
%13 = load i8**, i8*** %5
%14 = call i32 @getopt(i32 %12, i8** %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 %14, i32* %10
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %30
16: ; preds = %11
%17 = load i32, i32* %10
switch i32 %17, label %23 [
i32 98, label %18
i32 100, label %19
i32 117, label %21
]
18: ; preds = %16
store i32 1, i32* %9
br label %29
19: ; preds = %16
%20 = load i8*, i8** @optarg
store i8* %20, i8** %8
br label %29
21: ; preds = %16
%22 = load i8*, i8** @optarg
store i8* %22, i8** %7
br label %29
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 0
%27 = load i8*, i8** %26
%28 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %23, %21, %19, %18
br label %11
30: ; preds = %11
%31 = load i8*, i8** %7
%32 = icmp ne i8* %31, null
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %30
%36 = load i8*, i8** %8
%37 = icmp ne i8* %36, null
br i1 %37, label %40, label %38
38: ; preds = %35
%39 = call i32 @asprintf(i8** %8, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %38, %35
%41 = load i8*, i8** %7
%42 = call %struct.passwd* @getpwnam(i8* %41)
store %struct.passwd* %42, %struct.passwd** %6
%43 = load %struct.passwd*, %struct.passwd** %6
%44 = icmp ne %struct.passwd* %43, null
br i1 %44, label %47, label %45
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %83
47: ; preds = %40
%48 = load %struct.passwd*, %struct.passwd** %6
%49 = getelementptr inbounds %struct.passwd, %struct.passwd* %48, i32 0, i32 1
%50 = load i32, i32* %49
%51 = call i64 @setuid(i32 %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %59, label %53
53: ; preds = %47
%54 = load %struct.passwd*, %struct.passwd** %6
%55 = getelementptr inbounds %struct.passwd, %struct.passwd* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i64 @setgid(i32 %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %53, %47
%60 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %83
61: ; preds = %53
%62 = load i32, i32* %9
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %73
64: ; preds = %61
%65 = call i64 (...) @fork()
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %64
%68 = load i8*, i8** %8
%69 = call i32 @pg_dump_exec(i8* %68)
store i32 %69, i32* %3
br label %83
70: ; preds = %64
%71 = call i32 @wait(i32* null)
br label %72
72: ; preds = %70
br label %73
73: ; preds = %72, %61
%74 = call i64 (...) @fork()
%75 = icmp ne i64 %74, 0
br i1 %75, label %76, label %80
76: ; preds = %73
%77 = load i8*, i8** %8
%78 = load i32, i32* %9
%79 = call i32 @pg_db_reset(i8* %77, i32 %78)
store i32 %79, i32* %3
br label %83
80: ; preds = %73
%81 = call i32 @wait(i32* null)
br label %82
82: ; preds = %80
store i32 0, i32* %3
br label %83
83: ; preds = %82, %76, %67, %59, %45
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@.str = [6 x i8] c"d:u:b\00"
@optarg = global i8* null
@stderr = global i32 0
@.str.1 = [40 x i8] c"Usage: %s [-b] [-d database] [-u user]\0A\00"
@.str.2 = [9 x i8] c"postgres\00"
@.str.3 = [15 x i8] c"improveesation\00"
@.str.4 = [9 x i8] c"getpwnam\00"
@.str.5 = [11 x i8] c"set[u|g]id\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = bitcast i8** %3 to i8*
%6 = bitcast i8** %4 to i8*
br label %7
7: ; preds = %18, %2
%8 = phi i8* [ %19, %18 ], [ null, %2 ]
%9 = phi i8* [ %12, %18 ], [ null, %2 ]
%10 = phi i32 [ %15, %18 ], [ 0, %2 ]
br label %11
11: ; preds = %7, %20
%12 = phi i8* [ %9, %7 ], [ %21, %20 ]
%13 = phi i32 [ %10, %7 ], [ %15, %20 ]
br label %14
14: ; preds = %16, %11
%15 = phi i32 [ %13, %11 ], [ 1, %16 ]
br label %16
16: ; preds = %14, %22
%17 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
switch i32 %17, label %22 [
i32 -1, label %26
i32 98, label %14
i32 100, label %18
i32 117, label %20
]
18: ; preds = %16
%19 = load i8*, i8** @optarg
br label %7
20: ; preds = %16
%21 = load i8*, i8** @optarg
br label %11
22: ; preds = %16
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %1
%25 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %16
26: ; preds = %16
store i8* %12, i8** %3
store i8* %8, i8** %4
%27 = icmp eq i8* %12, null
br i1 %27, label %28, label %30
28: ; preds = %26
%29 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %26
%31 = icmp eq i8* %8, null
br i1 %31, label %32, label %34
32: ; preds = %30
%33 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %32, %30
%35 = load i8*, i8** %3
%36 = call %struct.passwd* @getpwnam(i8* %35)
%37 = icmp eq %struct.passwd* %36, null
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %70
40: ; preds = %34
%41 = getelementptr inbounds %struct.passwd, %struct.passwd* %36, i64 0, i32 1
%42 = load i32, i32* %41
%43 = call i64 @setuid(i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %40
%46 = getelementptr inbounds %struct.passwd, %struct.passwd* %36, i64 0, i32 0
%47 = load i32, i32* %46
%48 = call i64 @setgid(i32 %47)
%49 = icmp eq i64 %48, 0
br i1 %49, label %52, label %50
50: ; preds = %45, %40
%51 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %70
52: ; preds = %45
%53 = icmp eq i32 %15, 0
br i1 %53, label %62, label %54
54: ; preds = %52
%55 = call i64 (...) @fork()
%56 = icmp eq i64 %55, 0
br i1 %56, label %60, label %57
57: ; preds = %54
%58 = load i8*, i8** %4
%59 = call i32 @pg_dump_exec(i8* %58)
br label %70
60: ; preds = %54
%61 = call i32 @wait(i32* null)
br label %62
62: ; preds = %60, %52
%63 = call i64 (...) @fork()
%64 = icmp eq i64 %63, 0
br i1 %64, label %68, label %65
65: ; preds = %62
%66 = load i8*, i8** %4
%67 = call i32 @pg_db_reset(i8* %66, i32 %15)
br label %70
68: ; preds = %62
%69 = call i32 @wait(i32* null)
br label %70
70: ; preds = %68, %65, %57, %50, %38
%71 = phi i32 [ 1, %50 ], [ %59, %57 ], [ %67, %65 ], [ 0, %68 ], [ 1, %38 ]
ret i32 %71
}