#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ errno;
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(void) {
  int sock;
  if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    fprintf(stderr, "socket() failed: %s\n.", strerror(errno));
    return (EXIT_FAILURE);
  }
  pid_t pid;
  pid = fork();
  if (pid < 0)
    return (EXIT_FAILURE);
  if (pid == 0) {
    int sock;
    if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
      fprintf(stderr, "socket() failed: %s\n.", strerror(errno));
      return (EXIT_FAILURE);
    }
  } else {
    int status;
    waitpid(pid, &status, 0);
  }
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11c8 <main+0x78>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe4e(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1283 <main+0x133>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11ef <main+0x9f>
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1283 <main+0x133>
cmpq   $0x0,-0x10(%rbp)
jne    1268 <main+0x118>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1263 <main+0x113>
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xdb3(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1283 <main+0x133>
jmp    1277 <main+0x127>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e8a(%rip),%r14
mov    (%r14),%edi
mov    0x2e60(%rip),%r15
mov    (%r15),%esi
mov    0x2e6e(%rip),%rbp
mov    0x0(%rbp),%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11a4 <main+0x54>
xor    %eax,%eax
call   1060 <fork@plt>
mov    0x2e2c(%rip),%rbx
test   %rax,%rax
js     11ee <main+0x9e>
je     11d5 <main+0x85>
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1040 <waitpid@plt>
jmp    11e7 <main+0x97>
mov    0x2e25(%rip),%rax
mov    (%rax),%ebx
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe3e(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1030 <fprintf@plt>
mov    0x2de5(%rip),%rbx
jmp    11ee <main+0x9e>
mov    (%r14),%edi
mov    (%r15),%esi
mov    0x0(%rbp),%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11fb <main+0xab>
mov    0x2dc2(%rip),%rbx
mov    (%rbx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dce(%rip),%rax
mov    (%rax),%ebp
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xde7(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1030 <fprintf@plt>
jmp    11ee <main+0x9e>

LLVM IR (-O0):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"socket() failed: %s\0A.\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = load i32, i32* @IPPROTO_TCP
%9 = call i32 @socket(i32 %6, i32 %7, i32 %8)
store i32 %9, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
store i32 %16, i32* %1
br label %44
17: ; preds = %0
%18 = call i64 (...) @fork()
store i64 %18, i64* %3
%19 = load i64, i64* %3
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = load i32, i32* @EXIT_FAILURE
store i32 %22, i32* %1
br label %44
23: ; preds = %17
%24 = load i64, i64* %3
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %39
26: ; preds = %23
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_STREAM
%29 = load i32, i32* @IPPROTO_TCP
%30 = call i32 @socket(i32 %27, i32 %28, i32 %29)
store i32 %30, i32* %4
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %35)
%37 = load i32, i32* @EXIT_FAILURE
store i32 %37, i32* %1
br label %44
38: ; preds = %26
br label %42
39: ; preds = %23
%40 = load i64, i64* %3
%41 = call i32 @waitpid(i64 %40, i32* %5, i32 0)
br label %42
42: ; preds = %39, %38
%43 = load i32, i32* @EXIT_SUCCESS
store i32 %43, i32* %1
br label %44
44: ; preds = %42, %32, %21, %11
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"socket() failed: %s\0A.\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = load i32, i32* @AF_INET
%3 = load i32, i32* @SOCK_STREAM
%4 = load i32, i32* @IPPROTO_TCP
%5 = tail call i32 @socket(i32 %2, i32 %3, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %31
12: ; preds = %0
%13 = tail call i64 (...) @fork()
%14 = icmp slt i64 %13, 0
br i1 %14, label %31, label %15
15: ; preds = %12
%16 = icmp eq i64 %13, 0
br i1 %16, label %17, label %28
17: ; preds = %15
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = load i32, i32* @IPPROTO_TCP
%21 = tail call i32 @socket(i32 %18, i32 %19, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %26)
br label %31
28: ; preds = %15
%29 = bitcast i32* %1 to i8*
%30 = call i32 @waitpid(i64 %13, i32* %1, i32 0)
br label %31
31: ; preds = %28, %17, %12, %23, %7
%32 = phi i32* [ @EXIT_FAILURE, %7 ], [ @EXIT_FAILURE, %23 ], [ @EXIT_FAILURE, %12 ], [ @EXIT_SUCCESS, %17 ], [ @EXIT_SUCCESS, %28 ]
%33 = load i32, i32* %32
ret i32 %33
}