#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*/ uint32_t;
typedef int /*<<< orphan*/ startup;
typedef int /*<<< orphan*/ selftest;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int errno;
int ntohl(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
char *strerror(int);
int write(int, char *, int);
int main() {
  char startup[] = {0, 193, 0, 0, 0, 12, 0, 0, 0, 153, 0, 1};
  char selftest[] = {0, 193, 0, 0, 0, 10, 0, 0, 0, 80};
  int fd;
  int err;
  int rc = 1;
  fd = open("/dev/tpm0", O_RDWR);
  if (fd < 0) {
    printf("Unable to open the device.\n");
    goto out_noclose;
  }
  err = write(fd, startup, sizeof(startup));
  if (err != sizeof(startup)) {
    printf("Error occured while writing the startup command: %d\n", errno);
    goto out;
  }
  err = read(fd, startup, sizeof(startup));
  if (err != 10) {
    printf("Error occured while reading the startup result: %d %d %s\n", err,
           errno, strerror(errno));
    goto out;
  }
  err = ntohl(*((uint32_t *)(startup + 6)));
  if (err == 38) {
    printf("TPM already started.\n");
    goto out;
  }
  if (err != 0) {
    printf("TPM Error occured: %d\n", err);
    goto out;
  }
  rc = 0;
  printf("Startup successful\n");
out:
  err = write(fd, selftest, sizeof(selftest));
  if (err != sizeof(selftest)) {
    printf("Error occured while writing the selftest command: %d\n", errno);
    goto out;
  }
  err = read(fd, selftest, sizeof(selftest));
  if (err != 10) {
    printf("Error occured while reading the selftest result: %d %d %s\n", err,
           errno, strerror(errno));
    goto out;
  }
  err = ntohl(*((uint32_t *)(selftest + 6)));
  if (err != 0) {
    printf("TPM Error occured: %d\n", err);
    goto out;
  } else {
    rc = 0;
    printf("Selftest successful\n");
  }
  close(fd);
out_noclose:
  return rc;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe7a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xe78(%rip),%eax
mov    %eax,-0x8(%rbp)
mov    0xe72(%rip),%rax
mov    %rax,-0x1a(%rbp)
mov    0xe6f(%rip),%ax
mov    %ax,-0x12(%rbp)
movl   $0x1,-0x28(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
lea    0xe56(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11e5 <main+0x75>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13ba <main+0x24a>
mov    -0x20(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    $0xc,%rax
je     1223 <main+0xb3>
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12da <main+0x16a>
mov    -0x20(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xa,-0x24(%rbp)
je     127d <main+0x10d>
mov    -0x24(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x30(%rbp),%esi
mov    -0x2c(%rbp),%edx
mov    %rax,%rcx
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12da <main+0x16a>
mov    -0xa(%rbp),%edi
call   1080 <ntohl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x26,-0x24(%rbp)
jne    12a5 <main+0x135>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12da <main+0x16a>
cmpl   $0x0,-0x24(%rbp)
je     12c5 <main+0x155>
mov    -0x24(%rbp),%esi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12da <main+0x16a>
movl   $0x0,-0x28(%rbp)
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0xa,%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    $0xa,%rax
je     1318 <main+0x1a8>
mov    0x2cd5(%rip),%rax
mov    (%rax),%esi
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12da <main+0x16a>
mov    -0x20(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0xa,%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xa,-0x24(%rbp)
je     1372 <main+0x202>
mov    -0x24(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2c95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2c89(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%edx
mov    %rax,%rcx
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12da <main+0x16a>
mov    -0x14(%rbp),%edi
call   1080 <ntohl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     139d <main+0x22d>
mov    -0x24(%rbp),%esi
lea    0xd30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12da <main+0x16a>
movl   $0x0,-0x28(%rbp)
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movabs $0xc000000c100,%rax
mov    %rax,0x8(%rsp)
movl   $0x1009900,0x10(%rsp)
01
movabs $0xa000000c100,%rax
mov    %rax,0x18(%rsp)
movw   $0x5000,0x20(%rsp)
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    0xe4b(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1223 <main+0xa3>
mov    %eax,%r13d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1040 <write@plt>
cmp    $0xc,%eax
jne    1239 <main+0xb9>
lea    0x8(%rsp),%rsi
mov    %r13d,%edi
mov    $0xc,%edx
call   1070 <read@plt>
cmp    $0xa,%eax
jne    1252 <main+0xd2>
mov    0xe(%rsp),%edi
call   1090 <ntohl@plt>
test   %eax,%eax
je     127b <main+0xfb>
cmp    $0x26,%eax
jne    1344 <main+0x1c4>
lea    0xf1d(%rip),%rdi
jmp    1282 <main+0x102>
lea    0xf29(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    1335 <main+0x1b5>
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
lea    0xdd7(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1287 <main+0x107>
mov    %eax,%ebx
mov    0x2d7d(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10a0 <strerror@plt>
lea    0xdea(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1287 <main+0x107>
lea    0xe95(%rip),%rdi
call   1030 <puts@plt>
lea    0x18(%rsp),%rbx
mov    0x2d45(%rip),%r15
lea    0xe0c(%rip),%r12
jmp    12c3 <main+0x143>
nopl   0x0(%rax)
mov    %eax,%ebp
mov    (%r15),%r14d
mov    %r14d,%edi
call   10a0 <strerror@plt>
lea    0xe28(%rip),%rdi
mov    %ebp,%esi
mov    %r14d,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
cmp    $0xa,%eax
jne    1310 <main+0x190>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1070 <read@plt>
cmp    $0xa,%eax
jne    12a0 <main+0x120>
mov    0x1e(%rsp),%edi
call   1090 <ntohl@plt>
test   %eax,%eax
je     131f <main+0x19f>
lea    0xd8e(%rip),%rdi
mov    %eax,%esi
jmp    1316 <main+0x196>
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r15),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12c3 <main+0x143>
lea    0xe04(%rip),%rdi
call   1030 <puts@plt>
mov    %r13d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd44(%rip),%rdi
mov    %eax,%esi
jmp    1249 <main+0xc9>

LLVM IR (-O0):
@__const.main.startup = [12 x i8] c"\00\C1\00\00\00\0C\00\00\00\99\00\01"
@__const.main.selftest = [10 x i8] c"\00\C1\00\00\00\0A\00\00\00P"
@.str = [10 x i8] c"/dev/tpm0\00"
@O_RDWR = global i32 0
@.str.1 = [28 x i8] c"Unable to open the device.\0A\00"
@.str.2 = [53 x i8] c"Error occured while writing the startup command: %d\0A\00"
@errno = global i32 0
@.str.3 = [58 x i8] c"Error occured while reading the startup result: %d %d %s\0A\00"
@.str.4 = [22 x i8] c"TPM already started.\0A\00"
@.str.5 = [23 x i8] c"TPM Error occured: %d\0A\00"
@.str.6 = [20 x i8] c"Startup successful\0A\00"
@.str.7 = [54 x i8] c"Error occured while writing the selftest command: %d\0A\00"
@.str.8 = [59 x i8] c"Error occured while reading the selftest result: %d %d %s\0A\00"
@.str.9 = [21 x i8] c"Selftest successful\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [12 x i8]
%3 = alloca [10 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = bitcast [12 x i8]* %2 to i8*
%8 = bitcast [10 x i8]* %3 to i8*
store i32 1, i32* %6
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %93
15: ; preds = %0
%16 = load i32, i32* %4
%17 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%18 = call i32 @write(i32 %16, i8* %17, i32 12)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = icmp ne i64 %20, 12
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i32, i32* @errno
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i32 %23)
br label %55
25: ; preds = %15
%26 = load i32, i32* %4
%27 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%28 = call i32 @read(i32 %26, i8* %27, i32 12)
store i32 %28, i32* %5
%29 = load i32, i32* %5
%30 = icmp ne i32 %29, 10
br i1 %30, label %31, label %37
31: ; preds = %25
%32 = load i32, i32* %5
%33 = load i32, i32* @errno
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0), i32 %32, i32 %33, i8* %35)
br label %55
37: ; preds = %25
%38 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%39 = getelementptr inbounds i8, i8* %38, i64 6
%40 = bitcast i8* %39 to i32*
%41 = load i32, i32* %40
%42 = call i32 @ntohl(i32 %41)
store i32 %42, i32* %5
%43 = load i32, i32* %5
%44 = icmp eq i32 %43, 38
br i1 %44, label %45, label %47
45: ; preds = %37
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %55
47: ; preds = %37
%48 = load i32, i32* %5
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i32, i32* %5
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %51)
br label %55
53: ; preds = %47
store i32 0, i32* %6
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %55
55: ; preds = %85, %71, %62, %53, %50, %45, %31, %22
%56 = load i32, i32* %4
%57 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%58 = call i32 @write(i32 %56, i8* %57, i32 10)
store i32 %58, i32* %5
%59 = load i32, i32* %5
%60 = sext i32 %59 to i64
%61 = icmp ne i64 %60, 10
br i1 %61, label %62, label %65
62: ; preds = %55
%63 = load i32, i32* @errno
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.7, i64 0, i64 0), i32 %63)
br label %55
65: ; preds = %55
%66 = load i32, i32* %4
%67 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%68 = call i32 @read(i32 %66, i8* %67, i32 10)
store i32 %68, i32* %5
%69 = load i32, i32* %5
%70 = icmp ne i32 %69, 10
br i1 %70, label %71, label %77
71: ; preds = %65
%72 = load i32, i32* %5
%73 = load i32, i32* @errno
%74 = load i32, i32* @errno
%75 = call i8* @strerror(i32 %74)
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.8, i64 0, i64 0), i32 %72, i32 %73, i8* %75)
br label %55
77: ; preds = %65
%78 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%79 = getelementptr inbounds i8, i8* %78, i64 6
%80 = bitcast i8* %79 to i32*
%81 = load i32, i32* %80
%82 = call i32 @ntohl(i32 %81)
store i32 %82, i32* %5
%83 = load i32, i32* %5
%84 = icmp ne i32 %83, 0
br i1 %84, label %85, label %88
85: ; preds = %77
%86 = load i32, i32* %5
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %86)
br label %55
88: ; preds = %77
store i32 0, i32* %6
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.9, i64 0, i64 0))
br label %90
90: ; preds = %88
%91 = load i32, i32* %4
%92 = call i32 @close(i32 %91)
br label %93
93: ; preds = %90, %13
%94 = load i32, i32* %6
ret i32 %94
}

LLVM IR (-O2):
@__const.main.startup = [12 x i8] c"\00\C1\00\00\00\0C\00\00\00\99\00\01"
@__const.main.selftest = [10 x i8] c"\00\C1\00\00\00\0A\00\00\00P"
@.str = [10 x i8] c"/dev/tpm0\00"
@O_RDWR = global i32 0
@.str.2 = [53 x i8] c"Error occured while writing the startup command: %d\0A\00"
@errno = global i32 0
@.str.3 = [58 x i8] c"Error occured while reading the startup result: %d %d %s\0A\00"
@.str.5 = [23 x i8] c"TPM Error occured: %d\0A\00"
@.str.7 = [54 x i8] c"Error occured while writing the selftest command: %d\0A\00"
@.str.8 = [59 x i8] c"Error occured while reading the selftest result: %d %d %s\0A\00"
@str = [19 x i8] c"Startup successful\00"
@str.10 = [20 x i8] c"Selftest successful\00"
@str.11 = [21 x i8] c"TPM already started.\00"
@str.12 = [27 x i8] c"Unable to open the device.\00"
define i32 @main() {
%1 = alloca [12 x i8]
%2 = alloca [10 x i8]
%3 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.12, i64 0, i64 0))
br label %60
10: ; preds = %0
%11 = call i32 @write(i32 %6, i8* %3, i32 12)
%12 = icmp eq i32 %11, 12
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = load i32, i32* @errno
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i32 %14)
br label %34
16: ; preds = %10
%17 = call i32 @read(i32 %6, i8* %3, i32 12)
%18 = icmp eq i32 %17, 10
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = load i32, i32* @errno
%21 = tail call i8* @strerror(i32 %20)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0), i32 %17, i32 %20, i8* %21)
br label %34
23: ; preds = %16
%24 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 6
%25 = bitcast i8* %24 to i32*
%26 = load i32, i32* %25
%27 = tail call i32 @ntohl(i32 %26)
switch i32 %27, label %30 [
i32 38, label %28
i32 0, label %32
]
28: ; preds = %23
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.11, i64 0, i64 0))
br label %34
30: ; preds = %23
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %27)
br label %34
32: ; preds = %23
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %34
34: ; preds = %13, %19, %28, %30, %32
%35 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 6
%36 = bitcast i8* %35 to i32*
br label %37
37: ; preds = %43, %34
%38 = call i32 @write(i32 %6, i8* %4, i32 10)
%39 = icmp eq i32 %38, 10
br i1 %39, label %44, label %40
40: ; preds = %37
%41 = load i32, i32* @errno
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.7, i64 0, i64 0), i32 %41)
br label %43
43: ; preds = %40, %47, %55
br label %37
44: ; preds = %37
%45 = call i32 @read(i32 %6, i8* %4, i32 10)
%46 = icmp eq i32 %45, 10
br i1 %46, label %51, label %47
47: ; preds = %44
%48 = load i32, i32* @errno
%49 = tail call i8* @strerror(i32 %48)
%50 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @.str.8, i64 0, i64 0), i32 %45, i32 %48, i8* %49)
br label %43
51: ; preds = %44
%52 = load i32, i32* %36
%53 = tail call i32 @ntohl(i32 %52)
%54 = icmp eq i32 %53, 0
br i1 %54, label %57, label %55
55: ; preds = %51
%56 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %53)
br label %43
57: ; preds = %51
%58 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.10, i64 0, i64 0))
%59 = tail call i32 @close(i32 %6)
br label %60
60: ; preds = %57, %8
%61 = phi i32 [ 1, %8 ], [ 0, %57 ]
ret i32 %61
}