#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 void *uid_t;
typedef void *gid_t;

/* Variables and functions */
int EXIT_FAILURE;
void *atol(char const *);
scalar_t__ chdir(char *);
scalar_t__ chroot(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execve(char *, char **, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *getenv(char *);
scalar_t__ getgid();
scalar_t__ getuid();
int /*<<< orphan*/ perror(char *);
scalar_t__ setgid(void *);
scalar_t__ setuid(void *);
int /*<<< orphan*/ stderr;
int main() {
  const char *uid_s = getenv("INFOND_UID");
  const char *gid_s = getenv("INFOND_GID");
  if (!uid_s || !gid_s) {
    fprintf(stderr, "both INFOND_UID and INFOND_GID must be set\n");
    exit(EXIT_FAILURE);
  }
  int fd;
  for (fd = 3; fd < 1024; fd++)
    close(fd);
  uid_t uid = atol(uid_s);
  gid_t gid = atol(gid_s);
  do {
    if ((chroot(".")) != 0) {
      perror("chroot(\".\")"
             " failed");
      exit(EXIT_FAILURE);
    }
  } while (0);
  do {
    if ((chdir("/")) != 0) {
      perror("chdir(\"/\")"
             " failed");
      exit(EXIT_FAILURE);
    }
  } while (0);
  do {
    if ((setgid(gid)) != 0) {
      perror("setgid(gid)"
             " failed");
      exit(EXIT_FAILURE);
    }
  } while (0);
  do {
    if ((setuid(uid)) != 0) {
      perror("setuid(uid)"
             " failed");
      exit(EXIT_FAILURE);
    }
  } while (0);
  if (getuid() == 0) {
    fprintf(stderr, "uid is 0. won't continue\n");
    exit(EXIT_FAILURE);
  }
  if (getgid() == 0) {
    fprintf(stderr, "gid is 0. won't continue\n");
    exit(EXIT_FAILURE);
  }
  char *argv[] = {"infond-static", NULL};
  char *envp[] = {NULL};
  execve("/infond-static", argv, envp);
  perror("execve failed");
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe0a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
lea    0xe05(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1225 <main+0x45>
cmpq   $0x0,-0x18(%rbp)
jne    1248 <main+0x68>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
call   10a0 <fprintf@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
movl   $0x3,-0x1c(%rbp)
cmpl   $0x400,-0x1c(%rbp)
jge    1272 <main+0x92>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    124f <main+0x6f>
mov    -0x10(%rbp),%rdi
call   10d0 <atol@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   10d0 <atol@plt>
mov    %rax,-0x30(%rbp)
lea    0xdaf(%rip),%rdi
call   1040 <chroot@plt>
cmp    $0x0,%rax
je     12bc <main+0xdc>
lea    0xd9b(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
jmp    12c1 <main+0xe1>
jmp    12c6 <main+0xe6>
lea    0xd8a(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     12f6 <main+0x116>
lea    0xd76(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
jmp    12fb <main+0x11b>
jmp    1300 <main+0x120>
mov    -0x30(%rbp),%rdi
call   10c0 <setgid@plt>
cmp    $0x0,%rax
je     132d <main+0x14d>
lea    0xd51(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
jmp    1332 <main+0x152>
jmp    1337 <main+0x157>
mov    -0x28(%rbp),%rdi
call   1100 <setuid@plt>
cmp    $0x0,%rax
je     1364 <main+0x184>
lea    0xd2d(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2c7b(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
jmp    1369 <main+0x189>
mov    $0x0,%al
call   1060 <getuid@plt>
cmp    $0x0,%rax
jne    139d <main+0x1bd>
mov    0x2c5f(%rip),%rax
mov    (%rax),%edi
lea    0xd07(%rip),%rsi
call   10a0 <fprintf@plt>
mov    0x2c42(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    $0x0,%al
call   10b0 <getgid@plt>
cmp    $0x0,%rax
jne    13d1 <main+0x1f1>
mov    0x2c2b(%rip),%rax
mov    (%rax),%edi
lea    0xced(%rip),%rsi
call   10a0 <fprintf@plt>
mov    0x2c0e(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    0x2a28(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2a25(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
lea    -0x40(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    0xcbf(%rip),%rdi
call   1090 <execve@plt>
lea    0xcc2(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2bba(%rip),%rax
mov    (%rax),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
lea    0xe21(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r14
lea    0xe1d(%rip),%rdi
call   1030 <getenv@plt>
test   %r14,%r14
je     12ed <main+0x11d>
mov    %rax,%rbx
test   %rax,%rax
je     12ed <main+0x11d>
mov    $0x3,%ebp
nopl   (%rax)
mov    %ebp,%edi
call   1070 <close@plt>
add    $0x1,%ebp
cmp    $0x400,%ebp
jne    1210 <main+0x40>
mov    %r14,%rdi
call   10c0 <atol@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   10c0 <atol@plt>
mov    %rax,%rbx
lea    0xe03(%rip),%rdi
call   1040 <chroot@plt>
test   %rax,%rax
jne    12ff <main+0x12f>
lea    0xe03(%rip),%rdi
call   1050 <chdir@plt>
test   %rax,%rax
jne    130d <main+0x13d>
mov    %rbx,%rdi
call   10b0 <setgid@plt>
test   %rax,%rax
jne    131b <main+0x14b>
mov    %r14,%rdi
call   10f0 <setuid@plt>
test   %rax,%rax
jne    1329 <main+0x159>
xor    %eax,%eax
call   1060 <getuid@plt>
test   %rax,%rax
je     1337 <main+0x167>
xor    %eax,%eax
call   10a0 <getgid@plt>
test   %rax,%rax
je     1349 <main+0x179>
movaps 0x2b55(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xe05(%rip),%rdi
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   1080 <execve@plt>
lea    0xdfe(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2cf6(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
lea    0xd19(%rip),%rsi
jmp    1359 <main+0x189>
lea    0xd3e(%rip),%rdi
call   10d0 <perror@plt>
jmp    135e <main+0x18e>
lea    0xd45(%rip),%rdi
call   10d0 <perror@plt>
jmp    135e <main+0x18e>
lea    0xd49(%rip),%rdi
call   10d0 <perror@plt>
jmp    135e <main+0x18e>
lea    0xd4e(%rip),%rdi
call   10d0 <perror@plt>
jmp    135e <main+0x18e>
mov    0x2ca2(%rip),%rax
mov    (%rax),%edi
lea    0xd4a(%rip),%rsi
jmp    1359 <main+0x189>
mov    0x2c90(%rip),%rax
mov    (%rax),%edi
lea    0xd52(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2c73(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"INFOND_UID\00"
@.str.1 = [11 x i8] c"INFOND_GID\00"
@stderr = global i32 0
@.str.2 = [44 x i8] c"both INFOND_UID and INFOND_GID must be set\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [2 x i8] c".\00"
@.str.4 = [19 x i8] c"chroot(\22.\22) failed\00"
@.str.5 = [2 x i8] c"/\00"
@.str.6 = [18 x i8] c"chdir(\22/\22) failed\00"
@.str.7 = [19 x i8] c"setgid(gid) failed\00"
@.str.8 = [19 x i8] c"setuid(uid) failed\00"
@.str.9 = [26 x i8] c"uid is 0. won't continue\0A\00"
@.str.10 = [26 x i8] c"gid is 0. won't continue\0A\00"
@.str.11 = [14 x i8] c"infond-static\00"
@__const.main.argv = [2 x i8*] [i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.11, i32 0, i32 0), i8* null]
@.str.12 = [15 x i8] c"/infond-static\00"
@.str.13 = [14 x i8] c"execve failed\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [2 x i8*]
%8 = alloca [1 x i8*]
store i32 0, i32* %1
%9 = call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %2
%10 = call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i8* %10, i8** %3
%11 = load i8*, i8** %2
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = load i8*, i8** %3
%15 = icmp ne i8* %14, null
br i1 %15, label %21, label %16
16: ; preds = %13, %0
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %13
store i32 3, i32* %4
br label %22
22: ; preds = %28, %21
%23 = load i32, i32* %4
%24 = icmp slt i32 %23, 1024
br i1 %24, label %25, label %31
25: ; preds = %22
%26 = load i32, i32* %4
%27 = call i32 @close(i32 %26)
br label %28
28: ; preds = %25
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 1
store i32 %30, i32* %4
br label %22
31: ; preds = %22
%32 = load i8*, i8** %2
%33 = call i8* @atol(i8* %32)
store i8* %33, i8** %5
%34 = load i8*, i8** %3
%35 = call i8* @atol(i8* %34)
store i8* %35, i8** %6
br label %36
36: ; preds = %31
%37 = call i64 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %36
br label %44
44: ; preds = %43
br label %45
45: ; preds = %44
%46 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %45
%49 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %45
br label %53
53: ; preds = %52
br label %54
54: ; preds = %53
%55 = load i8*, i8** %6
%56 = call i64 @setgid(i8* %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %54
%59 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
%60 = load i32, i32* @EXIT_FAILURE
%61 = call i32 @exit(i32 %60)
unreachable
62: ; preds = %54
br label %63
63: ; preds = %62
br label %64
64: ; preds = %63
%65 = load i8*, i8** %5
%66 = call i64 @setuid(i8* %65)
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %72
68: ; preds = %64
%69 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
%70 = load i32, i32* @EXIT_FAILURE
%71 = call i32 @exit(i32 %70)
unreachable
72: ; preds = %64
br label %73
73: ; preds = %72
%74 = call i64 (...) @getuid()
%75 = icmp eq i64 %74, 0
br i1 %75, label %76, label %81
76: ; preds = %73
%77 = load i32, i32* @stderr
%78 = call i32 @fprintf(i32 %77, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0))
%79 = load i32, i32* @EXIT_FAILURE
%80 = call i32 @exit(i32 %79)
unreachable
81: ; preds = %73
%82 = call i64 (...) @getgid()
%83 = icmp eq i64 %82, 0
br i1 %83, label %84, label %89
84: ; preds = %81
%85 = load i32, i32* @stderr
%86 = call i32 @fprintf(i32 %85, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.10, i64 0, i64 0))
%87 = load i32, i32* @EXIT_FAILURE
%88 = call i32 @exit(i32 %87)
unreachable
89: ; preds = %81
%90 = bitcast [2 x i8*]* %7 to i8*
%91 = bitcast [1 x i8*]* %8 to i8*
%92 = getelementptr inbounds [2 x i8*], [2 x i8*]* %7, i64 0, i64 0
%93 = getelementptr inbounds [1 x i8*], [1 x i8*]* %8, i64 0, i64 0
%94 = call i32 @execve(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.12, i64 0, i64 0), i8** %92, i8** %93)
%95 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.13, i64 0, i64 0))
%96 = load i32, i32* @EXIT_FAILURE
ret i32 %96
}

LLVM IR (-O2):
@.str = [11 x i8] c"INFOND_UID\00"
@.str.1 = [11 x i8] c"INFOND_GID\00"
@stderr = global i32 0
@.str.2 = [44 x i8] c"both INFOND_UID and INFOND_GID must be set\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [2 x i8] c".\00"
@.str.4 = [19 x i8] c"chroot(\22.\22) failed\00"
@.str.5 = [2 x i8] c"/\00"
@.str.6 = [18 x i8] c"chdir(\22/\22) failed\00"
@.str.7 = [19 x i8] c"setgid(gid) failed\00"
@.str.8 = [19 x i8] c"setuid(uid) failed\00"
@.str.9 = [26 x i8] c"uid is 0. won't continue\0A\00"
@.str.10 = [26 x i8] c"gid is 0. won't continue\0A\00"
@.str.11 = [14 x i8] c"infond-static\00"
@__const.main.argv = [2 x i8*] [i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.11, i32 0, i32 0), i8* null]
@.str.12 = [15 x i8] c"/infond-static\00"
@.str.13 = [14 x i8] c"execve failed\00"
define i32 @main() {
%1 = alloca [2 x i8*]
%2 = alloca [1 x i8*]
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp ne i8* %3, null
%6 = icmp ne i8* %4, null
%7 = and i1 %5, %6
br i1 %7, label %13, label %8
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %0, %13
%14 = phi i32 [ %16, %13 ], [ 3, %0 ]
%15 = tail call i32 @close(i32 %14)
%16 = add nuw nsw i32 %14, 1
%17 = icmp eq i32 %16, 1024
br i1 %17, label %18, label %13
18: ; preds = %13
%19 = tail call i8* @atol(i8* %3)
%20 = tail call i8* @atol(i8* %4)
%21 = tail call i64 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %18
%24 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %18
%28 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %27
%31 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = tail call i32 @exit(i32 %32)
unreachable
34: ; preds = %27
%35 = tail call i64 @setgid(i8* %20)
%36 = icmp eq i64 %35, 0
br i1 %36, label %41, label %37
37: ; preds = %34
%38 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = tail call i32 @exit(i32 %39)
unreachable
41: ; preds = %34
%42 = tail call i64 @setuid(i8* %19)
%43 = icmp eq i64 %42, 0
br i1 %43, label %48, label %44
44: ; preds = %41
%45 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
%46 = load i32, i32* @EXIT_FAILURE
%47 = tail call i32 @exit(i32 %46)
unreachable
48: ; preds = %41
%49 = tail call i64 (...) @getuid()
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %48
%52 = load i32, i32* @stderr
%53 = tail call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
%55 = tail call i32 @exit(i32 %54)
unreachable
56: ; preds = %48
%57 = tail call i64 (...) @getgid()
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %56
%60 = load i32, i32* @stderr
%61 = tail call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.10, i64 0, i64 0))
%62 = load i32, i32* @EXIT_FAILURE
%63 = tail call i32 @exit(i32 %62)
unreachable
64: ; preds = %56
%65 = bitcast [2 x i8*]* %1 to i8*
%66 = bitcast [1 x i8*]* %2 to i8*
%67 = bitcast [1 x i8*]* %2 to i64*
store i64 0, i64* %67
%68 = getelementptr inbounds [2 x i8*], [2 x i8*]* %1, i64 0, i64 0
%69 = getelementptr inbounds [1 x i8*], [1 x i8*]* %2, i64 0, i64 0
%70 = call i32 @execve(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.12, i64 0, i64 0), i8** %68, i8** %69)
%71 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.13, i64 0, i64 0))
%72 = load i32, i32* @EXIT_FAILURE
ret i32 %72
}