#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*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ STDOUT_FILENO;
int dup2(int, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ execlp(char *, char *, char *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
void get_name(char *buffer) {
  int pipe_stdout[2];
  int status;
  int n;
  memset(buffer, 0, 1024);
  pipe(pipe_stdout);
  pid_t pid = fork();
  if (!pid) {
    if (dup2(pipe_stdout[1], STDOUT_FILENO) == -1) {
      perror("exec: couldn't duplicate STDOUT handle");
      exit(1);
    }
    execlp("uname", "uname", "-n", (char *)NULL);
    printf("exec: execlp failed (%d)", errno);
    exit(1);
  }
  waitpid(pid, &status, 0);
  n = read(pipe_stdout[0], buffer, 1024);
  buffer[n - 1] = '\0';
}


x86 assembly (clang, -O0):
<get_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
lea    -0x10(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1242 <get_name+0xa2>
mov    -0xc(%rbp),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1209 <get_name+0x69>
lea    0xe06(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe17(%rip),%rsi
lea    0xe16(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   10b0 <execlp@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
lea    0xdfd(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_name>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
lea    0x4(%rsp),%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     11f5 <get_name+0x55>
lea    0xc(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
add    $0x10,%rsp
pop    %rbx
ret
mov    0x8(%rsp),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1222 <get_name+0x82>
lea    0xded(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdfe(%rip),%rdi
lea    0xdfd(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
call   10b0 <execlp@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
lea    0xde6(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@.str = [39 x i8] c"exec: couldn't duplicate STDOUT handle\00"
@.str.1 = [6 x i8] c"uname\00"
@.str.2 = [3 x i8] c"-n\00"
@.str.3 = [25 x i8] c"exec: execlp failed (%d)\00"
@errno = global i32 0
define void @get_name(i8* %0) {
%2 = alloca i8*
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @memset(i8* %7, i32 0, i32 1024)
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = call i32 (...) @fork()
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp ne i32 %12, 0
br i1 %13, label %28, label %14
14: ; preds = %1
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%16 = load i32, i32* %15
%17 = load i32, i32* @STDOUT_FILENO
%18 = call i32 @dup2(i32 %16, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %14
%24 = call i32 @execlp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* null)
%25 = load i32, i32* @errno
%26 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %1
%29 = load i32, i32* %6
%30 = call i32 @waitpid(i32 %29, i32* %4, i32 0)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%32 = load i32, i32* %31
%33 = load i8*, i8** %2
%34 = call i32 @read(i32 %32, i8* %33, i32 1024)
store i32 %34, i32* %5
%35 = load i8*, i8** %2
%36 = load i32, i32* %5
%37 = sub nsw i32 %36, 1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
store i8 0, i8* %39
ret void
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str = [39 x i8] c"exec: couldn't duplicate STDOUT handle\00"
@.str.1 = [6 x i8] c"uname\00"
@.str.2 = [3 x i8] c"-n\00"
@.str.3 = [25 x i8] c"exec: execlp failed (%d)\00"
@errno = global i32 0
define void @get_name(i8* %0) {
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = bitcast [2 x i32]* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = tail call i32 @memset(i8* %0, i32 0, i32 1024)
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = call i32 (...) @fork()
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %25
11: ; preds = %1
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%13 = load i32, i32* %12
%14 = load i32, i32* @STDOUT_FILENO
%15 = call i32 @dup2(i32 %13, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %11
%21 = call i32 @execlp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* null)
%22 = load i32, i32* @errno
%23 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %1
%26 = call i32 @waitpid(i32 %9, i32* %3, i32 0)
%27 = load i32, i32* %7
%28 = call i32 @read(i32 %27, i8* %0, i32 1024)
%29 = add nsw i32 %28, -1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %0, i64 %30
store i8 0, i8* %31
ret void
}