#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ dup(scalar_t__);
int /*<<< orphan*/ execve(char *, char **, char **);
int /*<<< orphan*/ fatal(char *);
scalar_t__ fileno(int /*<<< orphan*/ *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int geteuid();
int getuid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
int wait(int *);
int main() {
  FILE *data;
  int pid;
  int wait_stat;
  const char *args[4];
  char *env[2];
  fprintf(stderr, "tester: UID(%d), EUID(%d)\n", getuid(), geteuid());
  args[0] = "/bin/sh";
  args[1] = "-c";
  args[2] = "./child";
  args[3] = 0;
  env[0] = "HOME=/home";
  env[1] = 0;
  data = stdin;
  if ((pid = fork()) < 0) {
    fatal("Can't fork()\n");
  }
  if (pid) {
    if (fileno(data) != 0) {
      close(0);
      dup(fileno(data));
    } else {
      fprintf(stderr, "stdin -> stdin\n");
    }
    execve("/bin/sh", (char **)args, env);
    fatal("child failed to exec.\n");
  }
  while (wait(&wait_stat) != pid)
    ;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %eax,-0x54(%rbp)
mov    $0x0,%al
call   1050 <geteuid@plt>
mov    -0x58(%rbp),%edi
mov    -0x54(%rbp),%edx
mov    %eax,%ecx
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
lea    0xe32(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe2f(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xe27(%rip),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
lea    0xe1c(%rip),%rax
mov    %rax,-0x50(%rbp)
movq   $0x0,-0x48(%rbp)
mov    0x2dc3(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    124b <main+0xab>
lea    0xdf3(%rip),%rdi
call   10a0 <fatal@plt>
cmpl   $0x0,-0x14(%rbp)
je     12bc <main+0x11c>
mov    -0x10(%rbp),%rdi
call   1090 <fileno@plt>
cmp    $0x0,%rax
je     1285 <main+0xe5>
xor    %edi,%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rdi
call   1090 <fileno@plt>
mov    %rax,%rdi
call   1040 <dup@plt>
jmp    129c <main+0xfc>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xdb2(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
lea    -0x40(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    0xd70(%rip),%rdi
call   1070 <execve@plt>
lea    0xda0(%rip),%rdi
call   10a0 <fatal@plt>
jmp    12c1 <main+0x121>
lea    -0x18(%rbp),%rdi
call   10b0 <wait@plt>
cmp    -0x14(%rbp),%eax
je     12d8 <main+0x138>
jmp    12c1 <main+0x121>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    0x2e27(%rip),%r15
mov    (%r15),%ebx
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <geteuid@plt>
lea    0xe35(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1080 <fprintf@plt>
lea    0xe3c(%rip),%rax
mov    %rax,0x20(%rsp)
lea    0xe38(%rip),%rax
mov    %rax,0x28(%rsp)
lea    0xe2f(%rip),%rax
mov    %rax,0x30(%rsp)
movq   $0x0,0x38(%rsp)
lea    0xe22(%rip),%rax
mov    %rax,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%r14
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1235 <main+0x95>
jne    1241 <main+0xa1>
jmp    129a <main+0xfa>
lea    0xdfd(%rip),%rdi
call   10a0 <fatal@plt>
mov    %r14,%rdi
call   1090 <fileno@plt>
test   %rax,%rax
je     1267 <main+0xc7>
xor    %edi,%edi
call   1060 <close@plt>
mov    %r14,%rdi
call   1090 <fileno@plt>
mov    %rax,%rdi
call   1040 <dup@plt>
jmp    1278 <main+0xd8>
mov    (%r15),%edi
lea    0xdd6(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
lea    0xd9c(%rip),%rdi
lea    0x20(%rsp),%rsi
lea    0x10(%rsp),%rdx
call   1070 <execve@plt>
lea    0xdc2(%rip),%rdi
call   10a0 <fatal@plt>
lea    0xc(%rsp),%rbx
nop
mov    %rbx,%rdi
call   10b0 <wait@plt>
cmp    %ebp,%eax
jne    12a0 <main+0x100>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"tester: UID(%d), EUID(%d)\0A\00"
@.str.1 = [8 x i8] c"/bin/sh\00"
@.str.2 = [3 x i8] c"-c\00"
@.str.3 = [8 x i8] c"./child\00"
@.str.4 = [11 x i8] c"HOME=/home\00"
@stdin = global i32* null
@.str.5 = [14 x i8] c"Can't fork()\0A\00"
@.str.6 = [16 x i8] c"stdin -> stdin\0A\00"
@.str.7 = [23 x i8] c"child failed to exec.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [4 x i8*]
%6 = alloca [2 x i8*]
store i32 0, i32* %1
%7 = load i32, i32* @stderr
%8 = call i32 (...) @getuid()
%9 = call i32 (...) @geteuid()
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %9)
%11 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %11
%12 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %12
%13 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 2
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8** %13
%14 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 3
store i8* null, i8** %14
%15 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 0
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8** %15
%16 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 1
store i8* null, i8** %16
%17 = load i32*, i32** @stdin
store i32* %17, i32** %2
%18 = call i32 (...) @fork()
store i32 %18, i32* %3
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %0
%21 = call i32 @fatal(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %22
22: ; preds = %20, %0
%23 = load i32, i32* %3
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %42
25: ; preds = %22
%26 = load i32*, i32** %2
%27 = call i64 @fileno(i32* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %25
%30 = call i32 @close(i32 0)
%31 = load i32*, i32** %2
%32 = call i64 @fileno(i32* %31)
%33 = call i32 @dup(i64 %32)
br label %37
34: ; preds = %25
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
br label %37
37: ; preds = %34, %29
%38 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 0
%39 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 0
%40 = call i32 @execve(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %38, i8** %39)
%41 = call i32 @fatal(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
br label %42
42: ; preds = %37, %22
br label %43
43: ; preds = %47, %42
%44 = call i32 @wait(i32* %4)
%45 = load i32, i32* %3
%46 = icmp ne i32 %44, %45
br i1 %46, label %47, label %48
47: ; preds = %43
br label %43
48: ; preds = %43
%49 = load i32, i32* %1
ret i32 %49
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"tester: UID(%d), EUID(%d)\0A\00"
@.str.1 = [8 x i8] c"/bin/sh\00"
@.str.2 = [3 x i8] c"-c\00"
@.str.3 = [8 x i8] c"./child\00"
@.str.4 = [11 x i8] c"HOME=/home\00"
@stdin = global i32* null
@.str.5 = [14 x i8] c"Can't fork()\0A\00"
@.str.6 = [16 x i8] c"stdin -> stdin\0A\00"
@.str.7 = [23 x i8] c"child failed to exec.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [4 x i8*]
%3 = alloca [2 x i8*]
%4 = bitcast i32* %1 to i8*
%5 = bitcast [4 x i8*]* %2 to i8*
%6 = bitcast [2 x i8*]* %3 to i8*
%7 = load i32, i32* @stderr
%8 = tail call i32 (...) @getuid()
%9 = tail call i32 (...) @geteuid()
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %9)
%11 = getelementptr inbounds [4 x i8*], [4 x i8*]* %2, i64 0, i64 0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %11
%12 = getelementptr inbounds [4 x i8*], [4 x i8*]* %2, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %12
%13 = getelementptr inbounds [4 x i8*], [4 x i8*]* %2, i64 0, i64 2
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8** %13
%14 = getelementptr inbounds [4 x i8*], [4 x i8*]* %2, i64 0, i64 3
store i8* null, i8** %14
%15 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8** %15
%16 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 1
store i8* null, i8** %16
%17 = load i32*, i32** @stdin
%18 = tail call i32 (...) @fork()
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %0
%21 = tail call i32 @fatal(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %24
22: ; preds = %0
%23 = icmp eq i32 %18, 0
br i1 %23, label %37, label %24
24: ; preds = %20, %22
%25 = tail call i64 @fileno(i32* %17)
%26 = icmp eq i64 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %24
%28 = tail call i32 @close(i32 0)
%29 = tail call i64 @fileno(i32* %17)
%30 = tail call i32 @dup(i64 %29)
br label %34
31: ; preds = %24
%32 = load i32, i32* @stderr
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
br label %34
34: ; preds = %31, %27
%35 = call i32 @execve(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %11, i8** %15)
%36 = call i32 @fatal(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
br label %37
37: ; preds = %34, %22
br label %38
38: ; preds = %37, %38
%39 = call i32 @wait(i32* %1)
%40 = icmp eq i32 %39, %18
br i1 %40, label %41, label %38
41: ; preds = %38
ret i32 0
}